C# hiding and showing a panel on top of a splitcontainer
I have a screen which is divided by a few splitcontainers. One of them contains rectangles which user components I made, these "rectangles" represent hospital beds. What I wanted to do is give users the option to toggle betwe开发者_Go百科en this "user component view" and a "datagrid view".
So I created a panel pnlPatients which I give the same size as the splitcontainer with the user components. When the user selects "Change view" the program is supposed to toggle between the two layouts.
Code: Attempt 1:
if (pnlPatients.Visible)
pnlPatients.Hide();
else
{
pnlPatients.Show();
pnlPatients.BringToFront();
}
Attempt 2:
pnlPatients.Visible = !pnlPatients.Visible;
pnlPatients.Invalidate();
The strange thing is that both attempts work like this:
The user first sees the "user component view". If he would toggle the view, it would correctly show the panel on top of the previous view. If he would toggle again then the panel would correctly be hidden. If he would then again toggle the view then the panel will not be shown. DO NOTE: while debugging, the visible property of the panel is correctly changed to TRUE or FALSE. But for some reason only the first time it is put to visible TRUE the panel can be seen.
Does anyone have an idea?
Best regards
Edit: I also tried this but to no succes:
pnlPatients.Visible = !pnlPatients.Visible;
if (pnlPatients.Visible)
{
pnlPatients.BringToFront();
}
else
{
pnlPatients.SendToBack();
}
In case someone doesn't want to wade through all of Tony's link:
this.splitContainer.Panel2.Hide();
this.splitContainer.Panel2Collapsed = true;
int control = 0;
private void hideShowLogToolStripMenuItem_Click(object sender, EventArgs e)
{
if (control == 0)
{
control = 1;
splitContainer1.Panel2Collapsed = false;
splitContainer1.Panel1Collapsed = true;
}
else if (control == 1)
{
control = 0;
splitContainer1.Panel2Collapsed = true;
splitContainer1.Panel1Collapsed = false;
}
}
Instead of Invalidating the Panel control, invalidate the Host form to force it to redraw all of it's children as well by calling this.Invalidate(true);
bool state;
private void btn_Click(object sender, EventArgs e)
{
if (state)
{
splitContainer1.Panel1Collapsed = true;
splitContainer1.Panel2Collapsed = false;
state = false;
}
else
{
splitContainer1.Panel1Collapsed = false;
splitContainer1.Panel2Collapsed = true;
state = true;
}
}
精彩评论