我创建了两个用户控件(名为 UserControl1 和 UserControl2)并动态地将一个添加到面板中:
UserControl yoozerControl;
if (pi != pie) {
yoozerControl = new UserControl1();
} else {
yoozerControl = new UserControl2();
}
panel1.Controls.Add(yoozerControl);
yoozerControl.Dock = DockStyle.Fill;
但是,我无法访问 UserControls 的成员。例如,UserControl1 有一个 TextBox1,但这不会编译:
UserControl yoozerControl;
if (pi != pie) {
yoozerControl = new UserControl1();
String s = yoozerControl.TextBox1.Text;
. . .
我必须做什么才能访问 UserControls 上控件的属性?
更新
// I was able to successfully test this by changing UserControl1's TextBox1 Modifier
// property to public, and the same with UserControl2's label1 Modifier property.
UserControl1 yoozerControl = null;
UserControl2 yowzerControl = null;
String s = String.Empty;
if (AppType.Equals(SearchTypes.Platypus)) {
yoozerControl = new UserControl1();
s = yoozerControl.textBox1.Text;
} else {
yowzerControl = new UserControl2();
s = yowzerControl.label1.Text;
}
if (!(null == yoozerControl)) {
panel1.Controls.Add(yoozerControl);
yoozerControl.Dock = DockStyle.Fill;
MessageBox.Show(s);
}
if (!(null == yowzerControl)) {
panel1.Controls.Add(yowzerControl);
yowzerControl.Dock = DockStyle.Fill;
MessageBox.Show(s);
}