0

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

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
4

5 回答 5

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
于 2013-05-18T15:01:50.223 回答
0

您应该定义一个布尔变量来打开/关闭。像这样:当点击 Button1 时,变量必须为真,两次点击必须为假

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
于 2013-05-18T15:03:42.603 回答
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
于 2013-05-18T15:12:47.460 回答
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
于 2013-05-18T15:20:24.413 回答
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
于 2013-05-18T15:22:34.217 回答