I have a Windows Forms application containing a textbox used to provide input to the application from a handheld barcode scanner. The barcodes we are using may contain special control characters like ESC whose default behavior is to clear out the input line. In order to be able to read such barcode string into the application I use the following method which is hooked into the textbox on key up event:
private void OnKeyUp(object sender, KeyEventArgs e){barcodestr += (char)e.KeyValue;}
The problem is that the line feed characters are being ignored by OnKeyUp method and I am not sure how I could capture those.
Please note that setting the Multiline/AcceptsReturn textbox attributes in any combination of true and false does not seem to make any difference.
In the end it worked after switching to using the KeyPress event instead of the KeyUp.
Related
private void NameVal_TextChanged(object sender, EventArgs e)
{
String text = NameVal.Text;
}
As soon as I enter the first letter of my Name this program gets executed . How do I make the program wait until I finish entering the whole string for the field (such as Name ex: James) is entered.
If you want to determine when the user has finished typing, you can catch the leave event instead - this is fired when the focus is lost on that text box - ie the user clicks outside of the textbox:
private void NameVal_Leave(object sender, EventArgs e)
{
//Do your stuff
String text = NameVal.Text;
}
If you are working with VS2019 there is a correlation between the TextChanged event and the Focus Leave event. You must have a TextChanged Event then when you search the Events and find Focus select Leave then there will be a drop down box with the TextChanged event for the same object as the Focus Event, which you will select. That will add the Leave Event to the Form1 definition area but it will not generate the code in the code area of Form1 like when you normally double click the TextBox. That you must create yourself. This article helped me do that part.
Later that day.
I was mistaken. You don't have to pick the TextChanged event. It was the only one available in the drop down list at the time. I had to add both events again in the form1 definition area since I had deleted the TextChanged event and manuallu added the Focus_Leave event, then went back in to add the TextChanged Event. I'm experimenting here. But when I deleted them both and re-entered the events again in the form1.design file there was a possibility to pick the correct one. I had another bug, but now they are in sync again. It's all the code that is generated behind the scene with MS VS that makes it difficult to change this area. And of course they do tell you not to change it, but what do you do if you make a mistake like this, you don't start over, you dive right in and keep going and learn how they are doing what they do.
I am using WPF, MVVM-Light.
In my UI I have a textbox, and I want to prevent the user from typing certain characters in the textbox.
I know if we use code-behind I could handle the key down keyPress events, can I achieve it through MVVM?
Can we use some behaviors or some interactivity triggers?
Using code-behind is perfectly OK with MVVM providing the code-behind is related to your View only.
So if you have some view-specific logic that says "User can only type numbers in this box", then it's perfectly OK to write a KeyPress event for the TextBox that only allows numeric keys to be processed. You could even throw this into a UserControl so it can be reusable.
However if your allowed character logic is based on application logic, such as "User can only use the characters defined in the app.config file for this string value", then you'd be better off validating that in the ViewModel.
Also note that restriction is different from validation.
If you want to validate a user's entry, then I would do so using IDataErrorInfo from the ViewModel layer, and possibly a binding with a mode of UpdateSourceTrigger=PropertyChanged so the validation is checked after every key press.
If you want to restrict what characters can be typed into a TextBox, then I would probably do that from the View layer in the code behind, as that is a functionality of the View.
Yes, to filter input the MVVM way, I would suggest either using a custom control (such as a masked TextBox control) or a Behavior.
I was recently looking for a good masked TextBox and there is a free one out there from Xceed which you can find here. I can't speak to this one, as I haven't used it, but I've been happy with other Xceed components I've used in the past.
However I didn't want to go third party and include a bunch of controls I didn't need, so I ended up creating a behavior that simply attaches to the TextBox and filters the input based on a FilterType. The behavior is pretty easy to create, and you simply use the PreviewTextInput event to filter out characters that you don't want.
This SO Answer has a number of suggestions and links to how to filter/mask the input and if you're not familiar with creating Attached Behaviors, this example shows how to create an Attached Behavior for a Masked Text Box.
There is a TextChanged event for the RichTextBox, what I require is a TextChanging event so I have chance to perform an action before the text is changed. The KeyDown event is not enough as my application uses a speech recognition engine which means it is possible to enter text without using the keyboard.
I was hoping I could intercept something in the WndProc method but nothing stands out.
Any ideas or help would be much appreciated. Thanks.
Try using the TextChanged Event from the RichTextBox class. Based on the description from MSDN
This event is raised if the Text property is changed by either a programmatic modification or user interaction.
It should be able to handle what you are trying to do.
Edit: You could have some sort of intermediary storage of the text so that when the text changes it is stored somewhere else first and after the text changed event is done, you can put the text back into the RichTextBox. But without knowing specifically what you are trying to accomplish, this would be my recommendation.
I'm making my own commentbox control that inherits from a winforms textbox. One of the things it does is prevent users from entering any characters if the limit has been hit. So on the keypress event I just grab the incoming key (excepting delete and backspace) and add it on to what's in the textbox already, then check and see what the length is. If it is over, then I just set the e.Handled = true and the keypress is halted. This scheme fails me though on an incoming ctrl-v paste event. It registers as a single keychar coming in. So the paste won't come through if I'm right at the limit, but it will go over if there is one character of room left and the paste has two or more characters for instance.
I just need a way to detect that this paste is coming in, and when its coming in I need to know what the length of the string is so I can either stop it from happening or paste as much of it as possible in there.
Thanks!
This is such an idle question. You can just set the MaxLength property of the textbox and it prevents all of this. Duh.
I have a WPF textbox, and perform the following actions
Enter text as "12345"
Move cursor between 3 and 4 (using arrow or
mouseclick)
Enter 0 (so Text is now "123045")
Which event/eventargs can tell me that 0 was typed at location 4.
I need to know this at Preview level so that I can reject the character 0 based on the prefixed and suffixed digits.
In PreviewTextInput event you can use the TextBox's CaretIndex property to know the location where input is being typed.
You can use the PreviewTextInput event. (see here for a complete example)