C # Crew: How can I respond to a text box ENTER? [text changes]

Q: I want an event only when Enter is pressed throw. Not when one of the changes to the text. Any ideas? Thanks!


Re:Originally posted by: diegoalcatraz
If this is for what I think it is (Axel) then … what I did was listen for the text change event, and attempted to validate the contents of the text box (make sure they're numbers). int.Parse works. If that passes, I use that int value to set the counter. If the parse fails, then they entered invalid characters, so I rollback to a previous value.

I would do validation instead.

Validation of Control Data on Windows Forms (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbconvalidationofcontroldataonwindowsforms.asp)


Re:Originally posted by: guy

Originally posted by: diegoalcatraz
If this is for what I think it is (Axel) then … what I did was listen for the text change event, and attempted to validate the contents of the text box (make sure they're numbers). int.Parse works. If that passes, I use that int value to set the counter. If the parse fails, then they entered invalid characters, so I rollback to a previous value.

Yes it is for Axel's class. :shocked:

I tried to use the text change event, but it gets triggered as soon as they enter one digit, say "4". But we don't want it to trigger until an entire number is entered, such as "435".

You can answer here or email me at jmm0257@cs.rit.edu.

Thanks! – Justin

There was also the deal of masking the textbox, to make sure that it wouldn't let any invalid characters (non-numeral in). VS 2005 Beta didn't seem to have a masked text box for just "number", but that's the effect I wanted. I suppose you could wait until the enter key to see, but it just seemed easier this way. I think his specs said that you didn't -have to- check on each key.


Re:Originally posted by: guy
I don't think there is support for a filter on when the event should be fired. The keypress will fire with each keypress. You can put in an if statement to test for the Enter key and filter the input yourself. Alternately, you could subclass the textbox and expose an event that is only fired if the Enter key is pressed.

I ended up filtering it after the event is fired. I just didn't know that I had to change EventArgs e into KeyPressEventArgs e.


Re:I don't think there is support for a filter on when the event should be fired. The keypress will fire with each keypress. You can put in an if statement to test for the Enter key and filter the input yourself. Alternately, you could subclass the textbox and expose an event that is only fired if the Enter key is pressed.

Re:Originally posted by: diegoalcatraz
If this is for what I think it is (Axel) then … what I did was listen for the text change event, and attempted to validate the contents of the text box (make sure they're numbers). int.Parse works. If that passes, I use that int value to set the counter. If the parse fails, then they entered invalid characters, so I rollback to a previous value.

Yes it is for Axel's class. :shocked:

I tried to use the text change event, but it gets triggered as soon as they enter one digit, say "4". But we don't want it to trigger until an entire number is entered, such as "435".

You can answer here or email me at jmm0257@cs.rit.edu.

Thanks! – Justin


Re:If this is for what I think it is (Axel) then … what I did was listen for the text change event, and attempted to validate the contents of the text box (make sure they're numbers). int.Parse works. If that passes, I use that int value to set the counter. If the parse fails, then they entered invalid characters, so I rollback to a previous value.

Re:attached is the VB code, it should give you an idea of howto do it in c#

Re:Originally posted by: guy
What event do you want it to throw? Your own? You have control over when you throw your events.

I want it to throw some "KeyPressed" event. Inside that I make a couple of function calls.


Re:Originally posted by: diegoalcatraz
I suppose you could just listen for the KeyPress events, and then filter those so you respond to the Enter key?

That's what I tried doing but I don't know how to test if for the Enter key.


Re:What event do you want it to throw? Your own? You have control over when you throw your events.

Re:I suppose you could just listen for the KeyPress events, and then filter those so you respond to the Enter key?

Related posts

Leave a comment

0 Comments.

Leave a Reply


click to changeSecurity Code

[ Ctrl + Enter ]