C# Crew: how do I have a textbox respond to ENTER?

Jumpem

Lifer
Sep 21, 2000
10,757
3
81
I want it to throw an event only when Enter is pressed. Not whenever any of the text changes. Any ideas? Thanks!
 

mundane

Diamond Member
Jun 7, 2002
5,603
8
81
I suppose you could just listen for the KeyPress events, and then filter those so you respond to the Enter key?
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
What event do you want it to throw? Your own? You have control over when you throw your events.
 

Jumpem

Lifer
Sep 21, 2000
10,757
3
81
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.
 

Jumpem

Lifer
Sep 21, 2000
10,757
3
81
Originally posted by: oog
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.
 

mundane

Diamond Member
Jun 7, 2002
5,603
8
81
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.
 

Jumpem

Lifer
Sep 21, 2000
10,757
3
81
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
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
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.
 

Jumpem

Lifer
Sep 21, 2000
10,757
3
81
Originally posted by: oog
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.
 

mundane

Diamond Member
Jun 7, 2002
5,603
8
81
Originally posted by: Jumpem
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.
 

HJB417

Senior member
Dec 31, 2000
763
0
0
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