C# Access UserControl from main form

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

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
You've pass "false" as the second parameter which means it will NOT recurse into container controls looking for a match.  As such, the call will only succeed if the UserControl is directly contained by the Form, and not if it is inside another container (such as a Panel).

Change it to "true" and see if that works better:
control = (UserControl)(parent.Controls.Find(userControlName, true).FirstOrDefault());

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jetbetAuthor Commented:
Yes that was it. I should have read further rather than relying on examples. Thanks a lot.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.