I need to create a usercontrol “Console”.
I was faced with such problems:
-
If I use a TextBox, how do I prevent removal of an already recruited command?
-
If I use a ListBox/ListView, how do I select all the text?
Please tell me what to do from the Console.
The console should be able to complete the command (by pressing Tab), allow selection of text, and prevent the entry of already established commands.
You could consider deriving from the RichTextBox control, as Tigran suggested.
Depending on what you want the user to be able to do, you will have to put some logic in there that restricts what they can and cannot select. (For example, if you don’t want them selecting previous commands). You can obtain the text that they’ve selected via the SelectedText property. And then put in your custom logic, for example, Ctrl+C will copy the text into a variable.
You may consider having a
MaximumSizeproperty so that old commands will be erased after the console becomes so large.Winforms already has a type of Autocomplete that you could use, or simply keep a list of keywords and when the user presses
TAB, fill in the first word in your list that starts with what they’ve already typed.To obtain the command itself, and not any of the previous text that was entered, you will probably want to take everything from the LAST newline to the end.
The code may look something like this:
And of course when the user presses
RETURN, the command will be sent to your code and executed.