Question Sending a KeyEvent to text box through CODE does not put character in Text Box...

ByteCarp

Member
Joined
Dec 10, 2008
Messages
9
Programming Experience
5-10
Seems like this should be doable, but I'm having no luck -

I have an Array of buttons with Japanese kana characters to act as a user input. Pressing a button (using a mouse) raises an event with the character as the data. My thought was just to use the same handler as the textbox key events, but no luck...

I've tried to call the TextBox_KeyUp/DOWN/Press events directly by creating a new "e As System.Windows.Forms.KeyEventArgs" and then passing this to the TextBox_KeyDown(TextBox,e) method.

Stepping through the code shows that the data IS passed, but no character is placed in the textbox - it remains blank, yet pressing a keyboard key enters text fine.

How come?

So briefly:

in the button pressed event:

Dim e as new e As System.Windows.Forms.KeyEventArgs("ぬ") 'set the character in to the keydata for the event argument
TextBox_KeyDown(TextBox, e) call the textbox keydown method - key data passed and fires event - no text.

I've tried for all three events, KeyUp,Down and Press, same result.

I have a workaround using the Textbox.Text and SelectionStart, etc., but I'm just looking for a more elegant solution - I would like the user to be able to click on a button for Japanese, but ALSO use the keyboard for TYPING English and not have to worry about where the cursor is, but because of special keys like Backspace, etc. this starts to involve checking a lot of key data unnecessarily...

Any thoughts appreciated...

Carp
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,171
Location
Sydney, Australia
Programming Experience
10+
An event handler is not an event, it's a method. Just because you make a direct call to a method that is executed when the KeyDown event is raised does not mean that the KeyDown event was raised. You're not sending anything to the TextBox. You're just calling a method in a form.

What you should be doing is setting the SelectedText property. That will insert the text at the caret location, replacing any text that is currently selected, exactly as typing or pasting would do.

Just keep in mind that the Font of the TextBox will have to be set to something that is capable of displaying the character(s) that you want.
 

ByteCarp

Member
Joined
Dec 10, 2008
Messages
9
Programming Experience
5-10
The SelectedText is what I'm using for the workaround, but thanks for the clarification on method vs. event...

The issue is actually more complicated because it also has to do with access to the Japanese IME -

ULTIMATELY, what I'm trying to do is "select Japanese kana" to put in a text box, while duplicating the SAME text in a second text box. Then, using the IME, converting the FIRST text (with kana) to the correct Kanji character (all handled by the IME) while leaving the original kana in the second text box.

(It's basically a Japanese/English dictionary, but since the same kana can have multiple Kanji, I needed a way to able to keep the original "text" while simultaneously modifying it...)

Where it all get's complicated is mistyping something and then trying to correct it in BOTH textboxes - it's doable, just wondering before I wrote extra code whether or not there was something already built in...

(Using SelectText, etc. works fine as a workaround - just thought I might make my life easier if there was a way to treat a button click as a KeyPress...)

Thanks for the quick response!

Carp
 

ByteCarp

Member
Joined
Dec 10, 2008
Messages
9
Programming Experience
5-10
Ah, thanks.

So something along the lines of subclassed API SendMessage?
 
Top Bottom