Click here to Skip to main content
15,881,839 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to combine these codes into one.

VB
Private Sub btnOne_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles btnOne.KeyPress
        TextBox1.Text = TextBox1.Text & 1

    End Sub

Private Sub btnOne_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOne.Click
       TextBox1.Text = TextBox1.Text & 1

   End Sub

I have no idea to write this. Could you help me?
Posted
Updated 18-Dec-13 22:12pm
v2
Comments
Maciej Los 19-Dec-13 4:12am    
Why?
CHM021 19-Dec-13 4:17am    
If e.KeyChar = Chr(Keys.btnOne) Then
btnOne_Click(Nothing, Nothing)
End If
When I search it in google, I found it. Is it correct?

One of the best programming practice is: when some part of code is used many times, exclude it into separate procedure/function.

By The Way: Are you sure you want to concatenate text on each button click:
1. click - 1
2. click - 11
3. click - 111

or do you want to add values
1. click - 1
2. click - 1 + 1 = 2
3. click - 2 + 1 = 3


Operator Precedence in Visual Basic[^]
Concatenation Operators in Visual Basic[^]
Arithmetic Operators in Visual Basic[^]
 
Share this answer
 
Comments
CHM021 19-Dec-13 4:30am    
I want to show the number in text box when I press number key buttons on form and number keypad
on keyboard.
Here:
VB
Private Sub btnOne_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles btnOne.KeyPress, btnOne.Click
       TextBox1.Text = TextBox1.Text & 1 
   End Sub
 
Share this answer
 
I found out the solution but its not very perfect. Thanks to all my friends for your help.

If e.KeyCode = Keys.NumPad1 Then
TextBox1.Text = TextBox1.Text

ElseIf e.KeyCode = Keys.NumPad0 Then
TextBox1.Text = TextBox1.Text
Endif
 
Share this answer
 

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