I have a derived TextBox where I intercept the userinput to manipulate it. However I have to preserve the original typed input. So my idea was to hold an inner TextBox within my derived class and send the user's input to that TextBox before manipulating it.
The reason for this approach is that I do not want to take care of all this special actions like: typing something, ctrl+a, [del], type something else, [backspace] and so on ...
However I do not know how to send a single keystroke (keycode, ascii, char) to a TextBox. Maybe you have another idea without an inner TextBox at all? Thank you!
If I understand you correctly, you could just let the inner text box have keyboard focus and handle the input. Then you could handle its KeyDown/KeyUp/KeyPress events in your container class to "intercept" the input.
You probably don't want to use keyboard-level manipulation because there are ways to change the text without the keyboard (in particular, with copy, cut, and paste). Why not use the Text property to get the text from the original textbox and then just save that?
Related
I'm attempting to make a command prompt clone in C# so I can get familiar with using IO. However, instead of just one text body, I created two text boxes (one for the command and another for the "parameters") and a rich text box to view the result of the commands. It may sound confusing and the title may be misleading, but I didn't know how else to word it. Anyway, my question is- how do you make only the current line editable and the rest read-only? And how would I be able to combine the commands and parameters in the two text boxes so I wouldn't need two separate text boxes? I have spent 6 days trying to figure out the logic to implement this but I got nothing.
Here's a picture of the form:
And here's what I want to make it similar to:
I'm not sure if you can do that, but if it was me, and this was a "get it done now" situation (and this is just off the top of my head), I would create a user control to contain the "screen". This user control would have the RTF or list box as the top, and a textbox flush under it.
I would remove the borders and wrap both these controls in a panel that has borders. This would simulate a single control.
The textbox would check for the enter key in one of the key-press events, and the control itself would have events that could be handled by the parent control.
This may be hacky, but it would probably be what I'd do in a last minute situation.
Oh as far as the command and parameter stuff, if you read the textbox as a single value, then split the string into an List or array, you could then define a switch or some other conditional code that would know what to do with parameters (index 1+), based on the value of the first item/index.
I already did some research and ended up with several autocomplete boxes which have one thing in common: they all match the whole expression which has been entered. Sometimes, they seem to be easily expandable, but at the end, they aren't.
However, I need a Textbox which allows the user to enter a word (e.g. "tag1"), displays a popup with some suggestions based on this single word, accept the suggestion with the return key and type in a new word (e.g "tag1 tag2") in the same textbox, with the popup popping up again. (I like the way CintaNotes handles this)
I need this for a tagging interface. It's often faster for the user to write the tags into a simple box, but sometimes, he needs assistance. This is what the autocomplete is for.
I've found some results, which don't work for my purpose (imho):
http://www.wpfpedia.com/item/details/743/wpf-autocomplete-textbox-control
http://www.codeproject.com/KB/WPF/WPF_Autocomplete.aspx
http://www.codeproject.com/KB/WPF/autocomplete_textbox.aspx
http://weblogs.thinktecture.com/cnagel/2011/01/autocomplete-textbox-with-wpf.html
Btw, I really like the way the Tag-Box for SO operates.
Does anyone have an idea? Is there a "out-of-the-box" - solution somewhere, which suits my needs but I didn't find? Or do I have to build one myself?
Thanks! :)
I think you mean a textbox which autocomplete for multiple words.
Like TokenizedTexbox on WPF Extended Toolkit.
this is the page: http://wpftoolkit.codeplex.com/wikipage?title=TokenizedTextBox&referringTitle=Home
Probably you would need to create your own Dictionary object of Key and Value pairs and hook that Dictionary to the textbox events and popup a suggestions dialog that displays the Value(s) from your Dictionary
Check this implementation out: http://code.google.com/p/kocontrols/downloads/list
You may inject your own Search algorithm and your own converter which converts the selected element to text, which you display in the TextBox. You will have to modify it slightly but I think that you might be able to solve your problem by basing your control on this implementation.
I never thought about this type of use case.
Can't you use different textboxes for the different tags? Something similar to how goole code does it?
If you have time, you can use the RichEditControl or TextBox and apply the same pattern used in Intellisense engine or Code Completation enabled editors: Hook the text changes events, Recogize context (last/current word) and display the options list on popup control. And optionally, on commit (detect acceptation or space key), apply the format to the word.
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.
Here is a start:
http://ansiconsole.codeplex.com
I used a bitmap, and render text to it. This way I have complete control over the input and output.
If you need some "simple" console application: insert commands, I presume in some DSL language, view result of execution, and other stuff, you can try to programm on RichTextBox base, which can give also some styling to content.
Reuse some already ready (complicated) editors, like for example:
Scintilla
And work to limit possibilities of that kind of component to fit your needs.
Regards.
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 MaximumSize property 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:
String allText = this.richTextBox1.Text; // All the text from the rich text box
Int32 lastIndex = allText.LastIndexOf("\n"); // Find the position of the last newline
String command = allText.Substring(lastIndex + 1); // Substring starting at the character after the last newline
And of course when the user presses RETURN, the command will be sent to your code and executed.
I've got a dialog box that pops up with a dynamic list of numbers, and I'd like to get the box to wrap the text because at the moment it displayed up to screen width and then cuts the rest off.
I know I can use \n to declare a new line, but the list is dynamic - it could be one item, it could be 20.
Is there any way to tell the dialog box to wrap text?
Edit: clarification + example code
I'm not using MessageBox.Show() - our code uses its own defined message box class, but the guts of it calls System.Windows.Forms.Form.ShowDialog(parent). Maybe this isn't as well-behaved (i.e., doesn't wrap) as MessageBox.Show()?
Create your own simple form and add a label. Do the wrapping there... You cannot do that much things with Dialog boxes.
In this way you have much more flexibility to show your information to the user.
Are you using the System.Windows.Forms.TextBox? It has a property WordWrap that you can set to true
No other way for a standart MessageBox. Only creating your own form.
You could programmatically format the text by restricting each line to a specific number of words then inserting a \n or Envoronment.NewLine
I want that a user only enters numeric data into the textbox in a windows form. How can I achieve this?
The easiest way would be to use a MaskedTextBox (use the Mask property) or a NumericUpDown control. If you really need fine-grained control that these controls do not provide, handle the KeyPress and other appropriate events of the TextBox control as required.
EDIT: Clarified that KeyPress is not the only relevant event, as mentioned by ho1.
What do you want to happen when the user attempts to enter a non-numeric value? Does this matter before a data submission attempt? Without entering your code block every time a key is pressed, I think it'd be cleaner to just let the user enter non-numerics and validate either when the form is submitted and/or when the text box loses focus. Then if validation fails, notify the user of the strict numeric format. This would be less invasive to a user, rather than interrupting.
As for validation, just use either regular expressions or try to parse the text box's text as an integer.
Regular Expression
System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), #"^\d$");
Integer Parse
int validNumbers = 0;
bool isValid = int.TryParse(myTextBox.Text, out validNumbers);
First it depends on what kind of numeric data you want to allow. (Integer, Double, ..., or something app-specific? Like an ISBN or something?)
Short:
The easiest way would be like Ani said, but if you need a more specific way, you should subcribe an matching Event. For Example TextBox.Validating, TextBox.OnLeave, TextBox.OnTextChange... depends on when you want to test the matching.
Than you can test in the Eventhandler whatever you want, even RegEx would be possible for complex alphanumeric data.
PS: You should really have a look at http://msdn.microsoft.com/en-us/library/system.windows.forms.control.validating.aspx