使用一个按钮切换启用和禁用所有按钮

时间:2019-01-29 01:08:46

标签: vb.net winforms

我想通过使用另一个Button的Click事件来禁用我的所有Button,然后如果再次单击该按钮,请重新启用它们。

我尝试了此代码,但失败了:

 Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click 
    If Button3.Enabled = True Then
        Button1.Enabled = False
        Button6.Enabled = False
        Button7.Enabled = False
        Button2.Enabled = False
        Button9.Enabled = False
    ElseIf Button3.Enabled = True Then
        Button1.Enabled = True
        Button6.Enabled = True
        Button7.Enabled = True
        Button2.Enabled = True
        Button9.Enabled = True
    End If
End Sub

2 个答案:

答案 0 :(得分:0)

fileLines = [x.strip() for x in file.readlines()]

这是我的修改内容

 Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click    

'use button1 or button6 or button 7 or button2 or button9 for the condition not the button 3 because it is the button that will trigger for enabling and disabling
        If Button1.Enabled = True Then
                    Button1.Enabled = False
                    Button6.Enabled = False
                    Button7.Enabled = False
                    Button2.Enabled = False
                    Button9.Enabled = False

                'in the elseif statement use the disabled buttons like button1 or button6 or button 7 or button2 or button9 for your condition to detect if it is disabled so that it will change its state from being disabled to enable
          ElseIf Button1.Enabled = false Then
                    Button1.Enabled = True
                    Button6.Enabled = True
                    Button7.Enabled = True
                    Button2.Enabled = True
                    Button9.Enabled = True

                End If
    End Sub

View image here for better understanding

答案 1 :(得分:0)

您有很多方法可以实现这一目标。
例如,您可以保留要切换的Button的索引的集合,并在Button.Click事件上使用for each循环来更改其 Enabled < / strong>状态:

  

请注意,此代码假定您的按钮位于窗体上。   如果它们在另一个容器(例如面板)中,请使用   容器的名称,而不是 Me 参考。   

Private toggleButtons As Integer() = {1, 2, 6, 7, 9}

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click    
    For Each idx As Integer In toggleButtons
        Me.Controls("Button" & idx.ToString).Enabled = Not Me.Controls("Button" & idx.ToString).Enabled
    Next
End Sub

与前面的示例非常相似,预先选择了需要切换的按钮:

Private toggleButtons As Button()

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    toggleButtons = New Button() {Button1, Button2, Button6, Button7, Button9}
End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click    
    For Each btn As Button In toggleButtons
        btn.Enabled = Not btn.Enabled
    Next
End Sub

您还可以将这些按钮的Tag属性设置为某个特定值,然后使用此属性值来区分它们。

例如,设置要切换为Tag的按钮的"toggleButton"属性:

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click    
    For Each btn As Button In Me.Controls.OfType(Of Button)
        If btn.Tag IsNot Nothing AndAlso btn.Tag.ToString().Equals("ToggleButton") Then
            btn.Enabled = Not btn.Enabled
        End If
    Next
End Sub

或专门的类,知道其按钮以及如何/何时切换它们。
或实现特定的接口(...)