I'm lost on this one.

I'm creating a "Preferences" type form. On this form I have a tree view and a panel.

I have created multiple user controls, each to be displayed in the panel when a certain tree node is selected.

The problem I am having is finding the best way to display the correct user control when a node is clicked. All I can seem to reference in the Panel.Controls collection is an index number and that is rather useless to me because I cant find a value in the tree view to correspond to it.

Is there a way to "bind" so to speak, a tree view node to a user control?

How would you go about doing something like this and keep it relatively easy to maintain and update?

Any suggestions are welcome.

I think I solved my problem.

Here's the solution I came up with...suggestions welcome:

private void Main_Load(object sender, EventArgs e)
        {
            treeViewSettings.ExpandAll();

            UserSettings = new LocalSettings();
            UserSettings = UserSettings.Load();

            //General Tree
            panelContent.Controls.Add(new General.Root());
            panelContent.Controls.Add(new General.Help());

            //DataAccess Tree
            panelContent.Controls.Add(new DataAccess.Root());
            panelContent.Controls.Add(new DataAccess.DataManagementSystem());

        }

        private void treeViewSettings_BeforeSelect(object sender, TreeViewCancelEventArgs e)
        {
            foreach (Control x in panelContent.Controls)
            {
                x.Hide();
            }
        }
        private void treeViewSettings_AfterSelect(object sender, TreeViewEventArgs e)
        {
            string TreeNameToLoad = treeViewSettings.SelectedNode.Name.Remove(0, 4);
            string ControlNameToLoad;

            foreach (Control x in panelContent.Controls)
            {
                ControlNameToLoad = x.ToString().Replace("DATA.Settings.", "").Replace(".", "");

                if (TreeNameToLoad == ControlNameToLoad)
                {
                    x.Show();
                    break;
                }
            }
        }

Not very graceful, but it works so long as each tree node name is prefixed with "Node" and each usercontrol name matches the the name of the node it corresponds with.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.