How to send a char to TextBox

G

Guest

Hi
How can I send a char to Textbox so that KeyPress event is called? I have a button and with a click I would like to send a char, e.g. "C", to the Textbox. But Textbox1_KeyPress is not catching the event (I can step thru KeyPress ok but nothing goes to TextBox). KeyPress itself works fine with key stroke via keyboard

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Clic
Dim e2 As New System.Windows.Forms.KeyPressEventArgs(CType("C", Char)
TextBox1_KeyPress(TextBox1, e2
End Su

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPres
If Not Char.IsLetter(e.KeyChar) And Not Char.IsControl(e.KeyChar) The
e.Handled = True ' ignor
End I
End Su

TIA.
 
H

Herfried K. Wagner [MVP]

* "=?Utf-8?B?c2NvdHRpZWRvZw==?= said:
How can I send a char to Textbox so that KeyPress event is called?

Maybe by using 'SendKeys.Send' -- nevertheless, why do you want to do
that?!
 
G

Guest

I'm creating a calculator type application. Plain SendKeys won't work. Or perhaps I don't know how to use SendKeys. Any suggestions?
 
C

Chris Barrell

use texbox1_change or somthing around that.

scottiedog said:
I'm creating a calculator type application. Plain SendKeys won't work.
Or perhaps I don't know how to use SendKeys. Any suggestions?
 
T

Tom Leylan

Ummm... I'll offer the suggestion that you are going about this "backwards"
(tm)

Rather than designing "intelligent systems" in the UI you might try build a
Calculator Class, then databind to the textbox. As you enter data the
Calculator object will validate not a keypress or some other UI event. That
way if the calculator "totals" and the accumulator property is changed in
the Calculator object it will automatically (through binding) be shown in
the textbox also.

Tom
 
G

Guest

Actually, SendKey worked. I had to give focus to the textbox first. Thanks
About the class, it is interesting approach. This means that I won't need textbox after all
I would just print out the result to, say label as I capture the key stroke (or button click), calculate, and then to label
Not bad at all

Thanks guys.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top