如何更改父窗体中子窗体控件的属性
how to change properties of child form controls in parent form
我有一个包含按钮和一些子窗体的 Mdiparent 窗体。
单击父窗体中的按钮时,如何更改所有子窗体中所有文本框的背景色?
这个ChilForm
;
public ChilForm()
{
InitializeComponent();
}
public void ChangeTextboxColor()
{
textBox1.BackColor = Color.Yellow;
}
这是Parent
;
ChilForm frm = new ChilForm();
public Parent()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//Shows the child
frm.Show();
}
private void button2_Click(object sender, EventArgs e)
{
//Changes color
frm.ChangeTextboxColor();
}
我知道答案已经给出了..但我会参加活动和代表..
多播委托是最好的选择在这里
所以这是我的解决方案。
namespace winMultiCastDelegate
{
public partial class Form1 : Form
{
public delegate void ChangeBackColorDelegate(Color backgroundColor);
//just avoid null check instanciate it with fake delegate.
public event ChangeBackColorDelegate ChangeBackColor = delegate { };
public Form1()
{
InitializeComponent();
//instanciate child form for N time.. just to simulate
for (int i = 0; i < 3; i++)
{
var childForm = new ChildForm();
//subscribe parent event
this.ChangeBackColor += childForm.ChangeColor;
//show every form
childForm.Show();
}
}
private void button1_Click(object sender, EventArgs e)
{
ChangeBackColor.Invoke(Color.Black);
}
}
/// <summary>
/// child form class having text box inside
/// </summary>
public class ChildForm : Form
{
private TextBox textBox;
public ChildForm()
{
textBox = new TextBox();
textBox.Width = 200;
this.Controls.Add(textBox);
}
public void ChangeColor(Color color)
{
textBox.BackColor = color;
}
}
}
我有一个包含按钮和一些子窗体的 Mdiparent 窗体。 单击父窗体中的按钮时,如何更改所有子窗体中所有文本框的背景色?
这个ChilForm
;
public ChilForm()
{
InitializeComponent();
}
public void ChangeTextboxColor()
{
textBox1.BackColor = Color.Yellow;
}
这是Parent
;
ChilForm frm = new ChilForm();
public Parent()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//Shows the child
frm.Show();
}
private void button2_Click(object sender, EventArgs e)
{
//Changes color
frm.ChangeTextboxColor();
}
我知道答案已经给出了..但我会参加活动和代表.. 多播委托是最好的选择在这里 所以这是我的解决方案。
namespace winMultiCastDelegate
{
public partial class Form1 : Form
{
public delegate void ChangeBackColorDelegate(Color backgroundColor);
//just avoid null check instanciate it with fake delegate.
public event ChangeBackColorDelegate ChangeBackColor = delegate { };
public Form1()
{
InitializeComponent();
//instanciate child form for N time.. just to simulate
for (int i = 0; i < 3; i++)
{
var childForm = new ChildForm();
//subscribe parent event
this.ChangeBackColor += childForm.ChangeColor;
//show every form
childForm.Show();
}
}
private void button1_Click(object sender, EventArgs e)
{
ChangeBackColor.Invoke(Color.Black);
}
}
/// <summary>
/// child form class having text box inside
/// </summary>
public class ChildForm : Form
{
private TextBox textBox;
public ChildForm()
{
textBox = new TextBox();
textBox.Width = 200;
this.Controls.Add(textBox);
}
public void ChangeColor(Color color)
{
textBox.BackColor = color;
}
}
}