我的表单名为Form1
,其中包含一个Button控件(bunifuImageButton9
)和一个用户控件(UserControl1
)。用户控件有一个文本框(textBox2
)。我需要按钮来更改用户控件中文本框中的文本。
我知道如何更改普通文本框中的内容,但我看不到如何访问用户控件中的文本框。
我该怎么做?
答案 0 :(得分:0)
快速,肮脏且可能错误的做法是在textBox2
上将UserControl1
公开而不是私有,然后从表单中调用
userControl1.textBox2.Text = "some new value";
更合适的方法是向UserControl1
添加公共属性,以有意义的方式公开文本框:
class UserControl1 {
public string SomeCoolTextValue {
get {
return textBox2.Text;
}
set {
textBox2.Text = value;
}
}
}
class Form1 {
private void bunifuImageButton9_Click(object sender, EventArgs e) {
userControl1.SomeCoolTextValue = "some new value";
}
}