Home > Back-end >  How to set the property of all controls of certain type?
How to set the property of all controls of certain type?

Time:04-05

I want to create a lot of panels and then only display one coreponding to the selected tree node.

I create panels using another form with this code:

private void btn_addpart_Click(object sender, EventArgs e)
        {
            Form1.instance.tree.SelectedNode.Nodes.Add(textBox1.Text);
            Form1.instance.tree.SelectedNode.Expand();
            Panel panel = new Panel();
            panel.Size = new Size(50, 50);
            panel.Location = new Point(rnd.Next(600),rnd.Next(200));
            panel.Name = textBox1.Text;
            panel.BackColor = Color.Red;
            Label label = new Label();
            label.Text = textBox1.Text;
            panel.Controls.Add(label);
            Form1.instance.Controls.Add(panel);
            this.Close();
        }

Then I only want to display the one corespoding to the to the tree node selected. For this whenever I click on a tree node I want to set the "visible" property of all panels to false and then only set the one i want to true. For this Im using this code.

private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) 
{ 
            foreach (Panel panel in this.Controls.OfType<Panel>())
            {
                panel.Visible = false;
            }
            Control control = Controls[treeView1.SelectedNode.Text];
            control.Visible = true; // here is says that control is null
}

But I get an error. What am I doing wrong and if i can't do it like this is there another way?

CodePudding user response:

I figured it out. The problem was that panel was not sent to Form1, but instead of fixing it I found a diffrent way. Which is this. This is played every time a tree node is clicked.

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
    {
        foreach (Panel panel in this.Controls.OfType<Panel>())
        {
            panel.Visible = false;
        }
        foreach (Panel panel in this.Controls.OfType<Panel>())
        {
            if (panel.Name == treeView1.SelectedNode.Text)
            {
                panel.Visible = true;
            }
        }
    }

And this is creating the panel i Form2

private void button1_Click(object sender, EventArgs e)
    {
        Form1.instance.tree.SelectedNode.Nodes.Add(textBox1.Text);
        Form1.instance.tree.SelectedNode.Expand();

        Panel panel = new Panel();
        panel.BorderStyle = BorderStyle.FixedSingle;
        panel.Location = new Point(random.Next(200), random.Next(200));
        panel.Size = new Size(50,50);
        panel.Name = textBox1.Text;
        Label label = new Label();
        label.Text = textBox1.Text;
        panel.Controls.Add(label);
        Form1.instance.Controls.Add(panel);
        foreach (Panel pln in Form1.instance.Controls.OfType<Panel>())
        {
            pln.Visible = false;
        }
        foreach (Panel pln in Form1.instance.Controls.OfType<Panel>())
        {
            if(pln.Name == Form1.instance.tree.SelectedNode.Text)
            {
                pln.Visible = true;
            }
        }
        this.Close();
    }
  • Related