在Windows窗体中访问另一个窗体上的控件的最佳方法是通过创建一个公共属性或方法来实现。以下是一个简单的示例:
public string Form1LabelText
{
get { return label1.Text; }
set { label1.Text = value; }
}
private void button1_Click(object sender, EventArgs e)
{
Form1 form1 = (Form1)this.Owner;
form1.Form1LabelText = textBox1.Text;
}
public void SetLabelText(string text)
{
label1.Text = text;
}
private void button1_Click(object sender, EventArgs e)
{
Form1 form1 = (Form1)this.Owner;
form1.SetLabelText(textBox1.Text);
}
通过这种方式,您可以在Windows窗体中访问另一个窗体上的控件,并在不同的窗体之间传递数据。
领取专属 10元无门槛券
手把手带您无忧上云