Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 17-Feb-13 5:02am
mahmii273
Comments
Sandeep Mewara at 17-Feb-13 10:16am
   
And what is the issue it?
Sergey Alexandrovich Kryukov at 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
source.compiler at 17-Feb-13 15:47pm
   
is it winapp? or web?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi,
 
Try this:
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.
  Permalink  
v3
Comments
_Maxxx_ at 18-Feb-13 0:16am
   
Great answer given the OP's original question!
ProgramFOX at 18-Feb-13 12:07pm
   
Thank you!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

of course it has a name..
Button someButton = new Button();
//...

someButton.Enabled = false;
  Permalink  
v2
Comments
Sergey Alexandrovich Kryukov at 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)



Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 17 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100