Click here to Skip to main content
15,887,821 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
If i have N amount of button on my form. How do i disable all the buttons on my form without wrting
VB
btn1.enable = false ... btnN.enable = false

thanks in advance.
Posted
Updated 28-Oct-13 12:30pm
v2
Comments
Kenneth Haugland 28-Oct-13 18:24pm    
WPF or WinForm?
Member 8525993 28-Oct-13 18:28pm    
sorry about that. WinForm

There are a couple of ways, but the easiest way is probably just to use a For Each loop on the Form.Controls collection. Of you examine each control and check if it is a button, it's a simple matter to enable / disable all of them without referencing the names at all.


"Can you provide me a simple code please ? for winform not WPF.."

VB
Public Sub EnableControls(controls As Control.ControlCollection, enable As Boolean)
	For Each c As Control In controls
		c.Enabled = enable
		EnableControls(c.Controls, enable)
	Next
End Sub
Then call it from your Form class with:
VB
EnableControls(Controls, True)
 
Share this answer
 
v2
Comments
Member 8525993 29-Oct-13 9:16am    
Can you provide me a simple code please ? for winform not WPF..
OriginalGriff 29-Oct-13 9:33am    
Answer updated
Member 8525993 29-Oct-13 10:58am    
Thanks you so much for answering my question. This is exactly what i wanted. also how do i exclude certain control from being effect. For instance label1

Public Sub EnableControls(controls As Control.ControlCollection, enable As Boolean)
For Each c As Control In controls

If c <> label1 Then
c.Enabled = enable
EnableControls(c.Controls, enable)
End If

Next
End Sub
Member 8525993 29-Oct-13 11:27am    
Below code works fine. if you have better ideas of doing it please share. thanks again for answering my question

Public Sub EnableControls(controls As Control.ControlCollection, enable As Boolean)
Dim clabel As Control = Label1

For Each c As Control In controls

If Not (c Is clabel) Then
EnableControls(c.Controls, enable)
c.Enabled = enable
End If
Next
End Sub
OriginalGriff 29-Oct-13 11:53am    
Either of your methods will work fine, or you could add
If c Is Button Then
instead.
Put all your Buttons in a Panel (or any other Container, like GroupeBox, etc) and just enable this one.
(if its possible in your Design)
 
Share this answer
 
Comments
Member 8525993 29-Oct-13 10:59am    
Thanks that is a interesting way of doing it. I did not think of that. very simple too. thanks
C#
private void Form1_Load(object sender, EventArgs e)
{
 System.Windows.Forms.Control.ControlCollection controls = this.Controls;

  foreach(Control ctrl in controls)
  {
     if (ctrl is Button)
     {
        ctrl.Enabled = false;
     }
  }
}
 
Share this answer
 
v2
Put them (the N Buttons) to an array and provide a method BtnEnabled(bool Value). BtnEnabled loops then over the array and sets btnary[xIx].enable= value;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900