2013-05-18 86 views
0

我有一個按鈕,點擊時標籤將顯示,但做什麼樣的代碼我用得到它,點擊該按鈕時,他們再次被隱藏:(VB)切換標籤的開/關

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 

回答

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 

或者更簡單地閱讀和反轉的Visible屬性

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 
+0

謝謝,先試試:D – Boats

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 
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 
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 
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