0

这有点简单,而且非常愚蠢。但我看不到我错过了什么..

    Private Sub btnmode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnmode.Click
    If Me.btnmode.Text = "COMMAND MODE" Then
        Me.btnmode.Text = "DATA MODE"
        sPort.Write("$$$")
    End If
    If Me.btnmode.Text = "DATA MODE" Then
        Me.btnmode.Text = "COMMAND MODE"
        sPort.Write("$$$" & vbCr)
        sPort.Write("exit" & vbCr)
    End If

End Sub

在此处输入图像描述

使用此代码,我应该能够在单击后更改其文本。但为什么不是呢?什么都没有发生。

4

2 回答 2

1

在这种情况下使用选择案例,它会更具可读性。

select case ucase(Me.btnmode.Text)

case "COMMAND MODE"
    Me.btnmode.Text = "DATA MODE"
    sPort.Write("$$$")
case "DATA MODE"
    Me.btnmode.Text = "COMMAND MODE"
    sPort.Write("$$$" & vbCr)
    sPort.Write("exit" & vbCr)

End Select

在您的代码中发现的问题:

    If Me.btnmode.Text = "COMMAND MODE" Then
        Me.btnmode.Text = "DATA MODE" '--------> Text been set as DATA MODE
        sPort.Write("$$$")            '    |
    End If                            '    |
                                      '    |
   If Me.btnmode.Text = "DATA MODE" Then ' ---------> Here again                                       
       Me.btnmode.Text = "COMMAND MODE"  '    you are checking the Text with 
       sPort.Write("$$$" & vbCr)         '    DATA MODE and you are assigning
       sPort.Write("exit" & vbCr)        '    the text back to COMMAND MODE
   End If  

所以我猜最初你的按钮文本是用“命令模式”设置的。结果,您的按钮文本拒绝还原。

于 2013-11-15T07:38:37.017 回答
0
If Me.btnLoad.Text = "COMMAND MODE" Then
    Me.btnLoad.Text = "DATA MODE"
    sPort.Write("$$$")
ElseIf Me.btnLoad.Text = "DATA MODE" Then
    Me.btnLoad.Text = "COMMAND MODE"
    sPort.Write("$$$" & vbCr)
    sPort.Write("exit" & vbCr)
End If
于 2013-11-15T07:32:35.870 回答