Click here to Skip to main content
14,551,234 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello All,

I have a Button control inside a GroupBox controls which is again inside a Tab control.
Now, i want to dynamically find that Button control.
For this, is it necessary to iterate through all container controls and then find Button controls?
I read this article[^] on CodeProject in which author has mentioned that we need to iterate.
Is there any new functionality available in c#4.0 using which i can directly find Button control without iterating through all container contols?

<--update-->
Actually, i have 15 buttons on my form and i am creating a Control Array of it just like in VB, so that all buttons have a common Click Event.
So for this manipulation, i need to find control dynamically, cast it to a Button control and use its reference.
I am trying to find the control using its name only.
<--update-->


<--update>
I want to change Text and Visibility of my all 15 buttons, for this i need to find button by passing its name to a function.
I don't want to write same code for all 15 buttons.
I am just passing button name to the function and change its text and visibility.
Not necessariliy for all 15 buttons but based on certain condition.
<--update>


Thanks,
Nagendra.
Posted
Updated 24-Apr-11 21:36pm
v4
Rate this:
Please Sign up or sign in to vote.

Solution 1

You could just use it's name...The one you gave it in the designer...

Or, if you dynamically added the button, you could save it in a class variable when you create it.


"Actually, i have 15 buttons on my form and i am creating a Control Array of it just like in VB, so that all buttons have a common Click Event.
So for this manipulation, i need to find control dynamically, cast it to a Button control and use its reference.
I am trying to find the control using its name only."


Oh! That's even easier: every event includes a "sender" parameter which is the control that raised the event. Just cast it and you are off:
private void button1_Click(object sender, EventArgs e)
    {
    Button b = sender as Button;
    if (b != null)
        {
        ...
        }
    }
   
v2
Comments
nagendrathecoder 25-Apr-11 3:07am
   
Actually, i have 15 buttons on my form and i am creating a Control Array of it just like in VB, so that all buttons have a common Click Event.
So for this manipulation, i need to find control dynamically, cast it to a Button control and use its reference.
I am trying to find the control using its name only.
   
You can (my 5) but the whole design is problematic.
Please see my answer.
--SA
OriginalGriff 25-Apr-11 3:20am
   
Answer updated
nagendrathecoder 25-Apr-11 3:30am
   
Thanks for your efforts.
Ofcourse i am using same code for Click event.
But i want to change the Text and Visibility of all 15 buttons based on some other events, for this i need to find Button controls dynamically.
Rate this:
Please Sign up or sign in to vote.

Solution 2

The task of dynamically finding of a control of your own Form is a good sign of wrong design.

The code you reference is not very good (see some criticism in comments), but the idea is working. Major problem here is: why finding it by the property Name? Nothing guarantees it is unique in the scope of the Form. You can create and abstract predicate (int the form of method, for example) and pass this predicate as a delegate instance. In particular, you can use combination of the Control's Type (using "is" operator) and something else (Name again).

There is nothing new in v.4.0 which could help you compared to v.3.5. If you compare with v.2.0, the new feature is lambda expression with would help you to shorted up syntax of the anonymous method you would use for the predicate (delegate instance). This is really minor feature; you can use anonymous method in v.2.0 using delegate keyword and syntax.

More important thing would be using alternative design. If you can explain your ultimate goal of finding of the control, perhaps I would be able to direct you in right direction.

[UPDATE]

Additional concerns based on OP's clarification:

Just writing some code for a button is a very poor justification for the search (even though the search will work, it is completely pointless).

You need to apply different criteria based on names. Why? (I explained the issue with names.) You can use instances themselves in compile time. You have all instances as the Form field during compile time. You will not shorten your code using names. Instead, create N Button arrays our of M buttons, N < M. Group those buttons by the code used to setup them — the Buttons requiring identical code goes to the same array. Loop you code through each array.

Another approach: 15 buttons is already too many for Designer! It makes your code not maintainable. Remove them all from the designer, design just panels and layout. Add the buttons during run-time (use MyPanel.Controls.Add), design proper initialization, calculate coordinates of each Button in the array. Call your setup method from you Form constructor, at the very end. I would prefer this method.

Also, having so many buttons at the same form will drive your user crazy. Anything more than 5-6 is too many. Consider merging all buttons in one (some "Execute command" button) combined with the ListBox of commands. There are many other designs based on selection: TreeView, ListView, DataGrid and more. Menu, after all. Many buttons is a sign of bad design. Look at Visual Studio. How many buttons can you see? And this is quite good design.

—SA
   
v4
Comments
nagendrathecoder 25-Apr-11 3:11am
   
Please check update in question. Thanks.
   
No this is does not answer my question. What you say was more or less clear in first place.
Now I'm asking, what are your trying to achieve with the search? Why not knowing the right control without any search. The answer this, you should explain what your application is supposed to do without mixing it with "how".
--SA
nagendrathecoder 25-Apr-11 3:35am
   
Please check Update2
   
I did, thank you. See my update now.
--SA
nagendrathecoder 26-Apr-11 3:09am
   
Your suggestions are good, don't know who gave u 1, mine 5 to counter it.
Sergey Alexandrovich Kryukov 26-Apr-11 11:31am
   
Thank you very much.
--SA
Rate this:
Please Sign up or sign in to vote.

Solution 3

Hi,
Try the following methods to get all the control in a winform.

public static List<Control> GetControls(Control form)
{
    var controlList = new List<Control>();
    foreach (Control childControl in form.Controls)
    {
        // Recurse child controls.
        controlList.AddRange(GetControls(childControl));
        controlList.Add(childControl);
    }
    return controlList;
}
   
Rate this:
Please Sign up or sign in to vote.

Solution 4

You can use following code to find any control
                   //"this" is Form control
Control [] buttons= this.Controls.Find("btnabc", true);
  foreach (var btn in buttons)
  {
      btn.Text = "new text";
  }
   
Comments
nagendrathecoder 25-Apr-11 4:41am
   
This won't work, i already tested.
EmersioN 26-Apr-11 4:55am
   
This works !!
I have copied here from my working code

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




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