I’ve created two UserControls (named UserControl1 and UserControl2) and dynamically add one to a panel:
UserControl yoozerControl;
if (pi != pie) {
yoozerControl = new UserControl1();
} else {
yoozerControl = new UserControl2();
}
panel1.Controls.Add(yoozerControl);
yoozerControl.Dock = DockStyle.Fill;
However, I cannot access the UserControls’ members. e.g., UserControl1 has a TextBox1, but this won’t compile:
UserControl yoozerControl;
if (pi != pie) {
yoozerControl = new UserControl1();
String s = yoozerControl.TextBox1.Text;
. . .
What must I do to access the properties of the controls on the UserControls?
UPDATE
// 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);
}
You need the user control to be typed as Your control. UserControl1 or UserControl2 (not UserControl)
And as Adam mentioned, if you haven’t already made things publicly accessible, you’ll need to deal with that as well.