如何使我的计算器应用程序响应Visual Basic中的键盘键? [英] How to make my calculator app to respond to the keyboard keys in Visual basic?

查看:64
本文介绍了如何使我的计算器应用程序响应Visual Basic中的键盘键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在visual basic中开发了一个简单的计算器代码。

I have developed a simple calculator code in visual basic.

Dim num As Double
 Dim op As String 
Dim dotindicator As Integer 
Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress TextBox1.Text = "0" dotindicator = 0 End Sub Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click TextBox1.Text = TextBox1.Text & "0" End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If TextBox1.Text = "0" Then TextBox1.Text = "1" Else TextBox1.Text = TextBox1.Text & "1" End If End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click If TextBox1.Text = "0" Then TextBox1.Text = "2" Else TextBox1.Text = TextBox1.Text & "2" End If End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click If TextBox1.Text = "0" Then TextBox1.Text = "3" Else TextBox1.Text = TextBox1.Text & "3" End If End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click If TextBox1.Text = "0" Then TextBox1.Text = "4" Else TextBox1.Text = TextBox1.Text & "4" End If End Sub Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click If TextBox1.Text = "0" Then TextBox1.Text = "5" Else TextBox1.Text = TextBox1.Text & "5" End If End Sub Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click If TextBox1.Text = "0" Then TextBox1.Text = "6" Else TextBox1.Text = TextBox1.Text & "6" End If End Sub Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click If TextBox1.Text = "0" Then TextBox1.Text = "7" Else TextBox1.Text = TextBox1.Text & "7" End If End Sub Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click If TextBox1.Text = "0" Then TextBox1.Text = "8" Else TextBox1.Text = TextBox1.Text & "8" End If End Sub Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click If TextBox1.Text = "0" Then TextBox1.Text = "9" Else TextBox1.Text = TextBox1.Text & "9" End If End Sub Private Sub plus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles plus.Click dotindicator = 0 num = TextBox1.Text op = "+" TextBox1.Text = "0" End Sub Private Sub subraction_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles subraction.Click dotindicator = 0 num = TextBox1.Text op = "-" TextBox1.Text = "0" End Sub Private Sub multiplication_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles multiplication.Click dotindicator = 0 num = TextBox1.Text op = "*" TextBox1.Text = "0" End Sub Private Sub division_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles division.Click dotindicator = 0 num = TextBox1.Text op = "/" TextBox1.Text = "0" End Sub Private Sub clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clear.Click TextBox1.Text = "0" dotindicator = 0 End Sub Private Sub equal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles equal.Click If op = "+" Then TextBox1.Text = Val(num) + Val(TextBox1.Text) ElseIf op = "-" Then TextBox1.Text = Val(num) - Val(TextBox1.Text) ElseIf op = "*" Then TextBox1.Text = Val(num) * Val(TextBox1.Text) ElseIf op = "/" Then TextBox1.Text = Val(num) / Val(TextBox1.Text) End If dotindicator = 0 End Sub Private Sub dot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dot.Click If dotindicator = 0 Then TextBox1.Text = TextBox1.Text & "." dotindicator = 1 End If End Sub Private Sub off_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles off.Click Me.Close() End Sub Private Sub back_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles back.Click If Not TextBox1.Text.Length() = 1 Then TextBox1.Text = TextBox1.Text.Substring(0, TextBox1.Text.Length() - 1) Else TextBox1.Text = "0" End If If TextBox1.Text.Chars(TextBox1.Text.Length() - 1) = "." Then dotindicator = 0 End If End Sub End Class



使用按钮时工作正常。但是,当我尝试使用键盘键来解决我的计算器问题时。 (即):当我键入23然后使用我的键盘按+时,我将显示为23+。我想要的是+应该作为操作员。我应该添加什么才能使用键盘进行操作。请帮忙。

.
It worked fine when i use the buttons. But when i try to use the keyboard keys for my calculator problem arises. (i.e): when i type 23 then pressing "+" using my keyboard, I am getting the display as "23+" . What i want is that "+" should be taken as operator. What should i add inorder to use the keyboard for my operations. Please help.

推荐答案

我想你可以在KeyPress事件中处理它。测试操作员的密钥,如果你看到一个Handled为true并执行相应的点击事件。



此外,你可以让操作员按钮响应热键 - 但是用户必须输入Alt- +,Alt- *等
I suppose you could handle it in the KeyPress event. Test the key for operators and if you see one set Handled to true and execute the appropriate click event.

Additionally, you could make the operator buttons respond to hotkeys -- but the user would have to type Alt-+, Alt-*, etc.


这篇关于如何使我的计算器应用程序响应Visual Basic中的键盘键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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