Click here to Skip to main content
16,012,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi.
I have a keyboard event KeyDown and uses standard delegate:
C#
using System.Windows.Forms;

textbox.KeyDown += new KeyEventHandler(KeyHandler);

This delegate referenses my keybord event handler:
C#
public void KeyHandler(object sender, KeyEvnetArgs e)
{
   TextBox textbox=(TextBox)sender;
   //some code
   Int I = textbox.Text.Length;//this shows the number of characters before it updates with key
   Int y = textbox.Text.Length; //this shows the correct number of characters.
}

When the keyboard event fires, i have this code in the keyboard handler:
C#
textbox.Text.Length;

It delivers the number of characters that was in the textbox before the event updates it. If I make the same call again it delivers the correct number of character in the textbox.

What causes this behavior?
Posted
Updated 7-Nov-13 19:37pm
v11
Comments
BillWoodruff 8-Nov-13 10:39am    
Please post code that actually compiles.

What is your goal here ? What type of characters do you need to handle, and what are you going to do when you handle them ? Do you ever need to cancel the keyboard event (if so, as SAK points out, use 'Keypress rather than 'KeyDown).
pluss400 16-Nov-13 6:24am    
SAK solution did not work at all. As I pointed out to him. If I use KeyDown or KeyPress doesn’t change anything.
The question is easy to understand.
The point is what happens width the key running from the keyboard all the way to the control. What the solution in the end will be when I intercept at deferent stages.
Ive solved it by remembering the keyboard message queue system. It explain the control behavior very well and I shall be handling the key code accordingly.
Anyway. Thank you for answering.
Sergey Alexandrovich Kryukov 10-Nov-13 15:55pm    
I removed my answer, which was perfectly adequate to the first version of the question, due to inadequate behavior of the inquirer.
My answer is not applicable anymore, as the question was edited. Moreover, in comment to my answer, OP informed me that the problem is resolved.
Very good, case closed.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900