Avatar of jetbet
jetbetFlag for New Zealand

asked on 

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;
            }
            else
            {
                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
                    scenes.Remove(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];
                            btn.ResetButton();
                        }
                    }
                }
            }
        }

Open in new window


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

Open in new window

C#.NET Programming

Avatar of undefined
Last Comment
jetbet
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of jetbet
jetbet
Flag of New Zealand image

ASKER

Yes that was it. I should have read further rather than relying on examples. Thanks a lot.
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo