如何设置某类所有控件的属性?
How to set the property of all controls of certain type?
我想创建很多面板,然后只显示一个对应于所选树节点的面板。
我使用以下代码使用另一种形式创建面板:
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();
}
然后我只想显示一个corespoding到所选的树节点。为此,每当我单击一个树节点时,我想将所有面板的“可见”属性 设置为 false,然后只将我想要的那个设置为 true。为此,我使用此代码。
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
}
但是我得到一个错误。我做错了什么,如果我不能这样做,还有另一种方法吗?
我明白了。问题是面板没有发送到 Form1,但我没有修复它,而是找到了一种不同的方法。这是哪个。每次单击树节点时播放。
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;
}
}
}
这就是在 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();
}
我想创建很多面板,然后只显示一个对应于所选树节点的面板。
我使用以下代码使用另一种形式创建面板:
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();
}
然后我只想显示一个corespoding到所选的树节点。为此,每当我单击一个树节点时,我想将所有面板的“可见”属性 设置为 false,然后只将我想要的那个设置为 true。为此,我使用此代码。
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
}
但是我得到一个错误。我做错了什么,如果我不能这样做,还有另一种方法吗?
我明白了。问题是面板没有发送到 Form1,但我没有修复它,而是找到了一种不同的方法。这是哪个。每次单击树节点时播放。
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;
}
}
}
这就是在 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();
}