单击 vb.net 后更改按钮的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19995709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:49:52  来源:igfitidea点击:

changing button's text after click vb.net

vb.net

提问by AdorableVB

This is kinda simple, and really silly. but I can't see what I am missing..

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

    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

enter image description here

在此处输入图片说明

with this code, I should be able to change its text after clicking. but why is it not? nothing is happening.

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

采纳答案by coder

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

回答by Rajaprabhu Aravindasamy

Use a select casein this situation, It will be more readable.

在这种情况使用select case,它会更具可读性。

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

Problemsfound in your code:

在您的代码中发现的问题

    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  

So i guess initially your button's text is set with "COMMAND MODE". As a result, your button's text is refusing to revert back.

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