Click here to Skip to main content
15,910,877 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a winform with several buttons. I need to find button by its name and make it visible or hidden after the button is found. Any suggestions for solutions?

What I have tried:

I tried Controls.Find and some other options but none of them was succesful….
Posted
Updated 3-Apr-19 23:42pm

You can iterate through control collection.

foreach (Control control in Controls)
{
      Button button = control as Button;
      if (!ReferenceEqual(null, button))
      {
        button.Visible = someConditionIfYoWant;
      }
}
 
Share this answer
 
If you're trying to find the button directly in the code of your window you can just reference the button by it's name. All controls in WinForms are given a name, so if your button has been called button1 then accessing the button for visibility is just button1.Visible

If it's from outside the window you may need to change your button to be public.
 
Share this answer
 
Comments
Member 12370240 4-Apr-19 2:16am    
How to call a button from outside the window? Do I just need to change PRIVATE VOID to PUBLIC VOID? I am familiar with calling from inside the window.

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