我有两个问题
我想在result
子
Button15_ click
当我在第op = sender.Text
行输入内容时,我在intellisense中看到了发件人对象的Text
方法吗?
这是我的代码
Public Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click, Button11.Click, Button12.Click, Button13.Click, Button14.Click, Button14.Click, Button16.Click
Dim op As Char
op = sender.Text
result = Convert.ToInt32(TextBox1.Text)
End Sub
Public Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
Select Case op
Case "+"
result += Convert.ToInt32(TextBox1.Text)
TextBox1.Text = result
Case "-"
result -= Convert.ToInt32(TextBox1.Text)
TextBox1.Text = result
Case "*"
result *= Convert.ToInt32(TextBox1.Text)
TextBox1.Text = result
Case "\"
result \= Convert.ToInt32(TextBox1.Text)
TextBox1.Text = result
End Select
End Sub
答案 0 :(得分:1)
将op
声明为类级别:
Dim op As Char
Public Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click, Button11.Click, Button12.Click, Button13.Click, Button14.Click, Button14.Click, Button16.Click
op = sender.Text
result = Convert.ToInt32(TextBox1.Text)
End Sub
Public Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
Select Case op
Case "+"
result += Convert.ToInt32(TextBox1.Text)
TextBox1.Text = result
Case "-"
result -= Convert.ToInt32(TextBox1.Text)
TextBox1.Text = result
Case "*"
result *= Convert.ToInt32(TextBox1.Text)
TextBox1.Text = result
Case "\"
result \= Convert.ToInt32(TextBox1.Text)
TextBox1.Text = result
End Select
End Sub
您在IntelliSense中看不到Text
的原因是sender
是Object
,而不是TextBox
,并且没有Text
属性。我建议您打开Option Strict
,因为它会显示几个潜在的代码错误。要获得它,请投射sender
:
op = DirectCast(sender, Button).Text(0)
Button15_Click
中还有很多共同的逻辑。我把它重写为:
Dim rightOperand As Integer = Integer.Parse(TextBox1.Text)
Select Case op
Case "+"c
result += rightOperand
Case "-"c
result -= rightOperand
Case "*"c
result *= rightOperand
Case "\"c
result \= rightOperand
End Select
TextBox1.Text = result.ToString()
答案 1 :(得分:0)
Private op as Char
Public Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click, Button11.Click, Button12.Click, Button13.Click, Button14.Click, Button14.Click, Button16.Click
op = DirectCast(sender, button).Text
result = Convert.ToInt32(TextBox1.Text)
End Sub