Answered by:
keypress event problem

Question
-
Hi all,
I have two textboxes in design namely txtcode and txtnumber.When im entering some number into txtcode, txtnumber should also have that number.for this I tried a simple logic that
txtnumber.text=txtcode.text
inside keypress event of txtcode. But the issue is that, if i presses any number first that is the first digit of the textbox its not appearing in txtnumber but appearing in txtcode only.After i presses second number then only the first number is appearing in txtnumber.Because of this conflict,there is a single number lagging in txtnumber while compare to txtcode.
how to solve this issue???
Monday, April 15, 2013 12:13 PM
Answers
-
The problem is that the KeyPress event fires before the text in the TextBox has been updated, so the key that is being pressed is not yet in txtcode.Text at the time you are copying it. Use the TextChanged event instead.
- Edited by Blackwood Monday, April 15, 2013 12:57 PM
- Proposed as answer by Cor Ligthert Monday, April 15, 2013 4:18 PM
- Marked as answer by Mike Feng Wednesday, April 17, 2013 12:14 PM
Monday, April 15, 2013 12:50 PM
All replies
-
The problem is that the KeyPress event fires before the text in the TextBox has been updated, so the key that is being pressed is not yet in txtcode.Text at the time you are copying it. Use the TextChanged event instead.
- Edited by Blackwood Monday, April 15, 2013 12:57 PM
- Proposed as answer by Cor Ligthert Monday, April 15, 2013 4:18 PM
- Marked as answer by Mike Feng Wednesday, April 17, 2013 12:14 PM
Monday, April 15, 2013 12:50 PM -
try like this.. .
public void txtcode_keyDownEvent ( object sender , EventArgs e )
{
if ( e.KeyCode == keys.Enter)
{
txtnumber.Text = txtcode.Text;
}
}
//////////////////////// or /////////////////////////////
public void txtcode_TextChangedEvent ( object sender , EventArgs e )
{
txtnumber.Text = txtcode.Text;
}
Monday, April 15, 2013 3:59 PM -
In addition to Blackwood, you can also use the KeyUp of course.
Success
CorMonday, April 15, 2013 4:17 PM -
Hey thanks for ur idea. Textchanged event is really working cool.Tuesday, April 16, 2013 4:11 AM
-
Hey thanks for ur idea. Textchanged event is really working cool.Tuesday, April 16, 2013 4:12 AM
-
Hey thanks for ur idea. Textchanged event itself is really working cool.Tuesday, April 16, 2013 4:12 AM