【问题标题】:Make a label or button behave as a checkbox?使标签或按钮表现为复选框?
【发布时间】:2016-02-08 04:53:42
【问题描述】:

我想要代码,当您单击标签时,它会将Boolean 更改为True,如果它是False,如果它是True,则更改为False。 它可以循环,就像一个复选框。

Private Sub Label3_Click(sender As System.Object, e As System.EventArgs) Handles Label3.Click
    Dim clickedLabel = TryCast(sender, Label)
    If bool1 = True Then
        If clickedLabel IsNot Nothing Then
            bool1 = False
    End If
    If bool1 = False Then
        If clickedLabel IsNot Nothing Then
            bool1 = True
    End If

有什么办法吗?

【问题讨论】:

    标签: vb.net checkbox boolean label


    【解决方案1】:

    好的,只需处理标签的Click 事件即可。

    Public Class Form1
    
        Private _MyBool As Boolean
    
        Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
            _MyBool = Not _MyBool
            Label1.Text = IIf(_MyBool, "MyBool is true", "MyBool is false")
        End Sub
    End Class
    

    使用not 运算符是切换布尔值的一种简单方法 - 无论它是什么,not 运算符都会翻转它。否则,您拥有的代码的唯一问题是您缺少 End If 部分的 if bool1 = ... 语句。

    【讨论】:

      猜你喜欢
      • 2022-12-22
      • 2012-10-16
      • 2018-06-26
      • 2013-07-17
      • 2012-01-21
      • 1970-01-01
      • 2019-11-26
      • 1970-01-01
      • 2023-02-06
      相关资源
      最近更新 更多