Reputation: 1055
I am trying to show the square root of the number entered in the textbox. when the user presses Q (Uppercase), the square Root should be shown (in the textbox). I did manage to get the sqaure root, but the problem is that when I press Q the Letter Q is also typed in. For example, if I enter 25 in the textbox and press Q then I get Q5 as the result. Is there any work around to this problem ? Below is the code that I have used.
Private Sub Textbox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = Chr(81) Then '81 for Letter Q
Dim root As Double = Math.Sqrt(Val(TextBox1.Text))
TextBox1.Text = root
End If
End Sub
Upvotes: 0
Views: 551
Reputation: 129
Catch the whole thing via event:
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.q_pushed);
public void q_pushed(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Q)
{
//DoStuff
}
}
This should do what you need :)
Upvotes: 0
Reputation: 26
You can just add this line
e.KeyChar = ""
Inside your if statement, it would look like this:
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = Chr(81) Then '81 for Letter Q
Dim root As Double = Math.Sqrt(Val(TextBox1.Text))
TextBox1.Text = root
e.KeyChar = ""
End If
End Sub
Upvotes: 1
Reputation: 8636
Why you are accepting characters block all the letters using ASCII values
Upvotes: 0