By Vinod Maurya


2010-12-19 16:40:46 8 Comments

I am working on an application which add objects (basically Windows Forms controls) at run time from an XML file. The application needs to access the objects that have been added.

The objects are added in a panel or in a groupbox. For the panel and groupbox, I have Panel.Controls["object_name"] to access the objects. This is only helpful when the object is directly added on the same panel. In my case the main panel [pnlMain, I have access to this panel only] may contain another panel and this panel [pnlChild] again contains a groupbox[gbPnlChild] and the groupbox contains a button [button1, I want to access this button]. I have the following method for this:

Panel childPanel = pnlMain.Controls["pnlChild"];
GroupBox childGP = childPanel.Controls["gbPnlChild"];
Button buttonToAccess = childGP["button1"];

The above method is helpful when parents are known. In my scenario, only the name of the object is known that is to be accessed [button1] and not its parents. So how do I access this object by its name, irrelevant of its parent?

Is there a method like GetObject("objName") or something similar?

4 comments

@Hans Passant 2010-12-19 16:47:59

You can use the form's Controls.Find() method to retrieve a reference back:

        var matches = this.Controls.Find("button2", true);

Beware that this returns an array, the Name property of a control can be ambiguous, there is no mechanism that ensures that a control has a unique name. You'll have to enforce that yourself.

@Shaun Luttin 2013-09-28 06:10:05

The will not work in the .NET Compact Framework.

@Peter Mortensen 2017-03-01 19:09:56

Is it a case-sensitive search?

@Shekhar 2015-03-05 22:29:14

If you are in a user control and don't have direct access to container form you can do the following

var parent = this.FindForm(); // returns the object of the form containing the current usercontrol.
var findButton = parent.Controls.Find("button1",true).FirstOfDefault();
if(findButton!=null)
{
    findButton.Enabled =true; // or whichever property you want to change.
}

@Harry 2013-11-27 20:40:47

  TextBox txtAmnt = (TextBox)this.Controls.Find("txtAmnt" + (i + 1), false).FirstOrDefault();

This works when you know what you are loking for.

@shipr 2015-08-09 15:30:29

The +1 in the expression saved me from a bug!

@Shaun Luttin 2013-09-28 06:08:02

The .NET Compact Framework does not support Control.ControlCollection.Find.

See Control.ControlCollection Methods and note that there is no little phone icon beside the Find method.

In that case, define the following method:

// Return all controls by name 
// that are descendents of a specified control. 

List<T> GetControlByName<T>(
    Control controlToSearch, string nameOfControlsToFind, bool searchDescendants) 
    where T : class
{
    List<T> result;
    result = new List<T>();
    foreach (Control c in controlToSearch.Controls)
    {
        if (c.Name == nameOfControlsToFind && c.GetType() == typeof(T))
        {
            result.Add(c as T);
        }
        if (searchDescendants)
        {
            result.AddRange(GetControlByName<T>(c, nameOfControlsToFind, true));
        }
    }
    return result;
}

Then use it like this:

// find all TextBox controls
// that have the name txtMyTextBox
// and that are descendents of the current form (this)

List<TextBox> targetTextBoxes = 
    GetControlByName<TextBox>(this, "txtMyTextBox", true);

Related Questions

Sponsored Content

10 Answered Questions

[SOLVED] IEnumerable vs List - What to Use? How do they work?

13 Answered Questions

17 Answered Questions

[SOLVED] Best way to access a control on another form in Windows Forms?

  • 2008-08-12 07:32:32
  • Dylan Bennett
  • 166520 View
  • 47 Score
  • 17 Answer
  • Tags:   c# winforms controls

20 Answered Questions

24 Answered Questions

[SOLVED] Multiple types were found that match the controller named 'Home'

1 Answered Questions

[SOLVED] Access child form group control elements from mdi parent

3 Answered Questions

1 Answered Questions

Restrict the numbers of controls in a winform

2 Answered Questions

3 Answered Questions

[SOLVED] adding controls to a Windows Forms Form

  • 2013-12-03 23:07:34
  • user2307236
  • 131 View
  • 1 Score
  • 3 Answer
  • Tags:   c# winforms

Sponsored Content