Reputation: 945
I am new to C# and I am using windows forms.
I have Form1
with 20 buttons
and 20 user controls
. What I am trying to do is:
When I click button1
user control1
shows up and the rest of user controls
hide, click button2
user control2
shows up and the rest of user controls
hide, click button3
user control3
shows up and the rest of user controls
hide and so on. I can use UserControl.visible = True
to show one user control
and hide the rest by setting the rest of user controls visibility = False
but I do not want to write too much code.
So instead I used the following simple code to show only one user control
at time when relevant button
is clicked and hide the rest of user controls
, but this code did not work it runs with no error but the user controls
still visible .
Anyone knows why this code did not show one user control
and hide the rest? I will be happy to hear other ideas too. Thank you
private void button1_Click(object sender, EventArgs e)
{
foreach (Control ctrl in this.Controls)
{
if (ctrl .GetType() == typeof(UserControl))
{
ctrl .Visible = false;
}
}
UserControl1.visible = True;
}
Upvotes: 3
Views: 2435
Reputation: 117009
This code worked a treat for me:
var pairs = new[]
{
new { Button = button1, UserControl = userControl1 },
new { Button = button2, UserControl = userControl2 },
new { Button = button3, UserControl = userControl3 },
new { Button = button4, UserControl = userControl4 },
new { Button = button5, UserControl = userControl5 },
new { Button = button6, UserControl = userControl6 },
new { Button = button7, UserControl = userControl7 },
new { Button = button8, UserControl = userControl8 },
new { Button = button9, UserControl = userControl9 },
new { Button = button10, UserControl = userControl10 },
new { Button = button11, UserControl = userControl11 },
new { Button = button12, UserControl = userControl12 },
new { Button = button13, UserControl = userControl13 },
new { Button = button14, UserControl = userControl14 },
new { Button = button15, UserControl = userControl15 },
new { Button = button16, UserControl = userControl16 },
new { Button = button17, UserControl = userControl17 },
new { Button = button18, UserControl = userControl18 },
new { Button = button19, UserControl = userControl19 },
new { Button = button20, UserControl = userControl20 },
};
foreach (var pair in pairs)
{
pair.UserControl.Visible = false;
pair.Button.Click += (s, e2) =>
{
foreach (var pair2 in pairs) { pair2.UserControl.Visible = false; }
pair.UserControl.Visible = true;
};
}
I purposefully create a list of button/user control pairs so that they are hard-coded and I don't have to go looking for them by name. It's better this way for refactoring and verifying your code.
Then the first foreach
loop just wires up the click event which makes all of the user controls invisible except for the paired user control.
Just pop this code in your Form_Load
event handler.
Upvotes: 2
Reputation: 205539
The reason your code is not working as expected is this condition:
if (ctrl.GetType() == typeof(UserControl))
GetType()
returns the actual type which never will be UserControl
, but some derived from it.
Use the following instead
if (ctrl is UserControl)
Upvotes: 5
Reputation: 12439
Button
has a property Tag
. Save your UserControl
references in tag property for all buttons:
button1.Tag = UserControl1;
button2.Tag = UserControl2;
button3.Tag = UserControl3;
//and so on...
//you can do it FormLoad event handler
Now subscribe all buttons to single Click
event:
button1.Click += button1_Click; //I am using "button1_Click" for all buttons
button2.Click += button1_Click;
button3.Click += button1_Click;
Now the main thing is here:
private void button1_Click(object sender, EventArgs e)
{
Button btn = sender as Button; //cast the sender
//hide all user controls
foreach (Control ctrl in this.Controls)
{
if (ctrl .GetType() == typeof(UserControl))
{
ctrl .Visible = false;
}
}
//show the usercontrol saved in "Tag" of the current button
(btn.Tag as UserControl).Visible = True;
}
Note: I have not compiled this code, if you get any error. Please let me know.
Upvotes: 3