How to disable all controls on the form except for a button?

后端 未结 5 1514
孤街浪徒
孤街浪徒 2020-12-13 21:17

My form has hundreds of controls: menus, panels, splitters, labels, text boxes, you name it.

Is there a way to disable every control except for a single button?

5条回答
  •  無奈伤痛
    2020-12-13 22:05

    For a better, more elegant solution, which would be easy to maintain - you probably need to reconsider your design, such as put your button aside from other controls. Then assuming other controls are in a panel or a groupbox, just do Panel.Enabled = False.

    If you really want to keep your current design, you can Linearise ControlCollection tree into array of Control to avoid recursion and then do the following:

    Array.ForEach(Me.Controls.GetAllControlsOfType(Of Control), Sub(x As Control) x.Enabled = False)
    yourButton.Enabled = True
    

提交回复
热议问题