We help IT Professionals succeed at work.
Get Started

C# Access UserControl from main form

jetbet asked
Last Modified: 2015-10-08
I have a Form on my application that contains several UserControls.
Each user control contains several custom buttons that I have derived from the button class.

I am trying to get at a button from the UserControl where the name of both the Button and UserControl names are stored in a config (XML) file. If I hard code the control getting the button is easy.
I cannot work out how to get the UserControl itself.

parent is my main form and this is called from an instance of a class on the form.

private void RemoveUnwantedScenes(Boolean _isMain, string _thisScene, Viz_ability.VizCommands.VizCommand.VizLayer _layer, UserControl _control)
            Viz_ability.VizCommands.VizCommand command;
            List<string> scenes;
            if (_isMain)
                command = vizMain;
                scenes = mainScenes;
                command = vizPreview;
                scenes = previewScenes;

            UserControl control = null;

            for (int i = scenes.Count - 1; i > -1; i--)
                string scene = scenes[i];
                XmlNodeList nodeList = xml_sceneDoc.SelectNodes("scenes/scene[name = '" + _thisScene + "']/shares/share[. = '" + scene + "']");
                if (nodeList.Count == 0)

                    String userControlName = DataCenter.XMLUtilities.GetElementValue(xml_sceneDoc, "scenes/scene[name = '" + scene + "']/userControl");
                    if (!String.IsNullOrEmpty(userControlName))
                        control = (UserControl)(parent.Controls.Find(userControlName, false).FirstOrDefault());
                    ClearScene(_isMain, scene, _layer);   //Remove pre existing scene

                    XmlNode resetButton = xml_sceneDoc.SelectSingleNode("scenes/scene[name = '" + scene + "']/resetButton");
                    if ((resetButton != null) && (control != null))
                        Control[] matches = control.Controls.Find(resetButton.InnerText, true);
                        if (matches.Length > 0 && matches[0] is MutexPreviewButton)
                            MutexPreviewButton btn = (MutexPreviewButton)matches[0];

Open in new window

The following line is what does not work
 control = (UserControl)(parent.Controls.Find(userControlName, false).FirstOrDefault());

Open in new window

Watch Question
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE