Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
1.67/5 (3 votes)
See more:
Dear all,

I created a usercontrol called Basecontrol and then add some basecontrols into form. Then try to find all the usercontrols:
foreach (BaseControl c in this.Controls)
{
  if (c.Name == "test1")
  {
    ///////My code;         
  }

However it fails. Please help me!

Thanks in advance.
Posted
Updated 6-Jan-11 18:11pm
v2

You should do it this way:
C#
foreach (Control c in this.Controls)
{
    if(c is BaseControl)
    {
        if (c.Name == "test1")
        {
            ///////My code;
        }
    }
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 7-Jan-11 1:33am    
Sure, the combined predicate may work this way. I don't see big reason to use Name property at all, because it means nothing (see also my Answer; also recursion is needed). And the type should better be generic...
jerrykid 7-Jan-11 2:01am    
Good, this is another way for the answer, 5+
This code is not wrong if there are only BaseControl on the form. If there is other control such as TextBox, it will raise error "Cannot convert TextBox to BaseControl".

Please try:
MIDL
foreach (Control c in this.Controls)
{
if (c.Name == "test1")
{
  //Your code;
}
 
Share this answer
 
v4
Comments
Sergey Alexandrovich Kryukov 7-Jan-11 1:30am    
Yes, first and foremost. My 5.

(Frankly, I did not even try to look at the code in this Question to find some sense in it :-), just written the way it should work -- see.)
Everyone forgot a control should be searched recursively:

By name:

C#
Control FindControl(Control control, string name) {
    if (control.Name == name)
        return control;
    foreach(Control child in control.Controls) {
        Control found = FindControl(control, name);
        if (found != null)
            return found;
    } //loop
    return null; //SA!!! important!
} //FindControl


The predicate is pretty bad: why by name? (Only because it was so in the Question.) Also, nobody will guarantee the name is unique in any scope.
Maybe, it's better to search by type.

The easiest way is generic. Here is how:

csl
CONTROL_TYPE FindControl<CONTROL_TYPE>(Control control)
            where CONTROL_TYPE : Control {
    if (control is CONTROL_TYPE)
        return (CONTROL_TYPE)control; //save due to the check above
    foreach (Control child in control.Controls) {
        CONTROL_TYPE found =
            FindControl<CONTROL_TYPE>(control);
        if (found != null)
            return found;
    } //loop
    return null; //SA!!! important!
} //FindControl


Generally whole design relying on search of control is pretty bad.
In some rare cases it can be useful. Anyway, it answers the question.

Good luck!
 
Share this answer
 
v2
Comments
jerrykid 7-Jan-11 2:03am    
Good suggestion, 5+
Hope User Control[^] will help you.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 7-Jan-11 1:11am    
Sorry, Kasson. Read the question. It is about the search of control, no matter of what type. The question reads: "I created a usercontrol...".

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