如何在一个代码中编写按键和按钮单击事件. [英] How to write key press and button click event in one code.

查看:98
本文介绍了如何在一个代码中编写按键和按钮单击事件.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将这些代码合并为一个.

I want to combine these codes into one.

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?

推荐答案

最佳编程实践之一是:当多次使用某部分代码时,请将其排除在单独的过程/函数之外.

顺便说一句:确定要在每个按钮上串联文本吗?单击:
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



Visual Basic中的运算符优先级 [ Visual Basic中的串联运算符 [ Visual Basic中的算术运算符 [



Operator Precedence in Visual Basic[^]
Concatenation Operators in Visual Basic[^]
Arithmetic Operators in Visual Basic[^]


此处:
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


我找到了解决方案但它不是很完美.感谢所有朋友的帮助.

如果e.KeyCode = Keys.NumPad1然后
TextBox1.Text = TextBox1.Text

ElseIf e.KeyCode = Keys.NumPad0然后
TextBox1.Text = TextBox1.Text
Endif
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


这篇关于如何在一个代码中编写按键和按钮单击事件.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆