Click here to Skip to main content
15,885,998 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi , i have a button in my winForm ,, and it has a name and tag !
i want to find it and disable it!
how can i do it ?
Posted
Comments
Sandeep Mewara 17-Feb-13 10:16am    
And what is the issue it?
Sergey Alexandrovich Kryukov 17-Feb-13 14:19pm    
What do you mean "by name or tag", exactly? Why using tags? Please clarify.
Most likely, this is just a bad idea leading to unsupportable code. You can always use member names, not property "Name", which you should not rely upon.
—SA
ali_heidari_ 17-Feb-13 15:47pm    
is it winapp? or web?

Hi,

Try this:
C#
IEnumerable<Button> buttons = this.Controls.OfType<Button>();
foreach (Button b in buttons)
{
      if (b.Name == "name" || b.Tag == "tag")
      {
          b.Enabled = false;
      }
}

If you've also containers on your form, such as Panels, then you should use recursion to disable also some buttons on the panel.

Hope this helps.
 
Share this answer
 
v3
Comments
_Maxxx_ 18-Feb-13 0:16am    
Great answer given the OP's original question!
Thomas Daniels 18-Feb-13 12:07pm    
Thank you!
of course it has a name..
C#
Button someButton = new Button();
//...

someButton.Enabled = false;
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 17-Feb-13 14:17pm    
I up-voted this answer with my 4. Your naming for the button was somewhat confusing, so I clarified the code sample a bit. Would you mind? I usually show such simple code samples this way, and they are always understood correctly.

The problem is that OP's "name or tag" is confusing... Your answer is just the idea, which should be enough.
—SA

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