1

When i am pressing any key in the textbox it will return "A" Character.

Code sample:

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    e.KeyChar = "A"
End Sub

so i want to get multiple character. how to get?

code sample:

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    e.KeyChar = "AB"
End Sub

the above coding not working. the e.keychar get only one character that is "A" only return. How to get all the characters.

1
  • 2
    KeyPress reports that the user pressed a single key. "AB" is not a key, so this won't work. What is it you're trying to accomplish? It is likely that TextChanged is what you're looking for. Commented May 23, 2011 at 17:27

2 Answers 2

3

You could append to the TextBox that "AB" and stop the event

  var text = sender as TextBox; 
  text.Text = text.Text + "AB";
  e.Handled = true;

Or you just can use TextChanged

Sign up to request clarification or add additional context in comments.

1 Comment

ok thanks but when you assign the text to textbox that time the cursor position move to first location (i.e) the selectionstart property is zero.
0

Inside the text box's keypress event, you're substituting whatever key was captured (e.keychar) with the keyChar 'A'. If you want all keys to come through the text box, remove the code that substitutes 'A' for whatever key was pressed.

Comment out your line of code, in other words, and all keys will pass through to the text box.

Did you want to limit their entry to keys A-Z?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.