(VB)打开/关闭标签

时间:2013-05-18 14:56:15

标签: vb.net toggle

我有一个按钮,当点击标签时会显示,但是我用了什么代码来获取它,所以当再次点击按钮时它们会被隐藏:

Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Label4.Hide()
        Label5.Hide()
        Label6.Hide()
    End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MsgBox("All shortcuts require Shift+(Letter) combination", vbOKOnly, "Shortcuts Active")
        Label4.Show()
        Label5.Show()
        Label6.Show()
    End Sub

5 个答案:

答案 0 :(得分:1)

只需查看Visible属性

即可
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
   if Label4.Visible = false then 
        MsgBox("All shortcuts require Shift+(Letter) combination", vbOKOnly, "Shortcuts Active")
        Label4.Show()
        Label5.Show()
        Label6.Show()
   else
        Label4.Hide()
        Label5.Hide()
        Label6.Hide()
   End if
End Sub

或者更简单地阅读并反转可见属性

    Dim SetVisible = Not Label4.Visible
    if Label4.Visible = False then
        MsgBox("All shortcuts require Shift+(Letter) combination", vbOKOnly, "Shortcuts Active")
    End If
    Label4.Visible = SetVisible
    Label5.Visible = SetVisible
    Label6.Visible = SetVisible
End Sub

答案 1 :(得分:0)

你应该定义一个bool变量来打开/关闭。 像这样:当点击Button1时,变量必须为true,并且两次单击必须为false

dim isClicked as bool = false
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        if !isClicked then

         Label4.Show()
         Label5.Show()
         Label6.Show()
         isClicked = true

       else

         Label4.Hide()
         Label5.Hide()
         Label6.Hide()
         isClicked = false
end if
    End Sub

答案 2 :(得分:0)

另一个......

Private Labels As New List(Of Label)

Private Sub Form3_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Shown
    Labels.AddRange({Label4, Label5, Label6})
    ToggleLabelVisibility()
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    MsgBox("All shortcuts require Shift+(Letter) combination", vbOKOnly, "Shortcuts Active")
    ToggleLabelVisibility()
End Sub

Private Sub ToggleLabelVisibility()
    For Each lbl As Label In Labels
        lbl.Visible = Not lbl.Visible
    Next
End Sub

答案 3 :(得分:0)

很容易..

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

  Label4.Visible = not Label4.Visible
  Label5.Visible = not Label5.Visible
  Label6.Visible = not Label6.Visible

End Sub

答案 4 :(得分:0)

或更常用的功能,不仅可以切换标签,还可以切换任何控件

Private Sub Toggle(ByVal ParamArray controls() As Control)
    For Each c As Control In controls
        c.Visible = Not c.Visible
    Next
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Toggle(Label4, Label5, Label6)
End Sub