提升事件后的预期表达

时间:2015-08-01 00:06:04

标签: vb.net

Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
    If RaiseEvent Button1.Click = True Then
        Dim result As Integer
        Dim Fnum As Integer = TextBox1.Text
        Dim Snum As Integer = TextBox2.Text
        result = Fnum + Snum
        Label1.Text = result

你好,我这个代码有问题。我试图让它说按下按钮,它激活这个其他按钮的方法。我在RaiseEvent部分收到错误。它在R上放了一条曲线,并说“预期的表达。抱歉我对VB有点新,需要一点帮助。谢谢

1 个答案:

答案 0 :(得分:0)

RaiseEvent用于引发事件,而不是查明事件是否已被引发。

根据您在其他帖子中的评论,您可能需要保留一个标记,以便在点击操作员按钮时进行跟踪。

像这样,

Dim OperatorClicked As Boolean

Private Sub OperatorButton_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click
    OperatorClicked = True
End Sub

Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
    If OperatorClicked Then
        Dim result As Integer
        Dim Fnum As Integer = TextBox1.Text
        Dim Snum As Integer = TextBox2.Text
        result = Fnum + Snum '-- you need to modify this too to specify correct operator
        Label1.Text = result
    End If
    OperatorClicked = False
End Sub

修改

我们不使用布尔标志来跟踪是否单击了运算符,而是使用字符串来跟踪单击的运算符。这里我假设按钮Text分别有+, - ,/,*。 (但理想的方法是使用Tag属性。这将帮助您保持显示文本与代码不相关。)

Dim OperatorClicked As String

Private Sub OperatorButton_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click
    OperatorClicked = CType(sender, Button).Text.Trim
End Sub

Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
    If OperatorClicked <> "" Then
        Dim result As Integer
        Dim Fnum As Integer = TextBox1.Text
        Dim Snum As Integer = TextBox2.Text
        Select Case OperatorClicked
        Case "+"
            result = Fnum + Snum 
        Case "-"
            result = Fnum - Snum 

        '-- similarly other cases

        End Select

        Label1.Text = result
    End If
    OperatorClicked = ""
End Sub