Hiding/Showing panels
-
I try to hide/show panels with controls depending on what TreeView node the user clicks (for large data inputs), but it doesn't work and I have no idea why not. This is my code for the TreeView's AfterSelect event:
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
{
if (treeView1.SelectedNode == null)
{
this.Text = "null";
}
else
{
if (treeView1.SelectedNode.Name == "Node1")
{
panel2.Visible = false;
panel1.Visible = true;
this.Text = "panel1 + | panel2 -";
}
else if (treeView1.SelectedNode.Name == "Node2")
{
panel1.Visible = false;
panel2.Visible = true; //this does nothing?
this.Text = "panel1 - | panel2 +";
}
}}
By changing the form text I confirmed that the event gets properly thrown and by stepping through the code I found out that if Node2 is selected, the
panel2.Visible = true
line does absolutely nothing and afterwardsVisible
is stillfalse
. Why does that happen? I'm really confused. Thanks. -
I try to hide/show panels with controls depending on what TreeView node the user clicks (for large data inputs), but it doesn't work and I have no idea why not. This is my code for the TreeView's AfterSelect event:
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
{
if (treeView1.SelectedNode == null)
{
this.Text = "null";
}
else
{
if (treeView1.SelectedNode.Name == "Node1")
{
panel2.Visible = false;
panel1.Visible = true;
this.Text = "panel1 + | panel2 -";
}
else if (treeView1.SelectedNode.Name == "Node2")
{
panel1.Visible = false;
panel2.Visible = true; //this does nothing?
this.Text = "panel1 - | panel2 +";
}
}}
By changing the form text I confirmed that the event gets properly thrown and by stepping through the code I found out that if Node2 is selected, the
panel2.Visible = true
line does absolutely nothing and afterwardsVisible
is stillfalse
. Why does that happen? I'm really confused. Thanks.I created a test project with a TreeView, some Nodes, and two Panels; your code works fine for me. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
I try to hide/show panels with controls depending on what TreeView node the user clicks (for large data inputs), but it doesn't work and I have no idea why not. This is my code for the TreeView's AfterSelect event:
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
{
if (treeView1.SelectedNode == null)
{
this.Text = "null";
}
else
{
if (treeView1.SelectedNode.Name == "Node1")
{
panel2.Visible = false;
panel1.Visible = true;
this.Text = "panel1 + | panel2 -";
}
else if (treeView1.SelectedNode.Name == "Node2")
{
panel1.Visible = false;
panel2.Visible = true; //this does nothing?
this.Text = "panel1 - | panel2 +";
}
}}
By changing the form text I confirmed that the event gets properly thrown and by stepping through the code I found out that if Node2 is selected, the
panel2.Visible = true
line does absolutely nothing and afterwardsVisible
is stillfalse
. Why does that happen? I'm really confused. Thanks.This stuff would work just fine, provided the controls and panels are layed out properly.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
I try to hide/show panels with controls depending on what TreeView node the user clicks (for large data inputs), but it doesn't work and I have no idea why not. This is my code for the TreeView's AfterSelect event:
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
{
if (treeView1.SelectedNode == null)
{
this.Text = "null";
}
else
{
if (treeView1.SelectedNode.Name == "Node1")
{
panel2.Visible = false;
panel1.Visible = true;
this.Text = "panel1 + | panel2 -";
}
else if (treeView1.SelectedNode.Name == "Node2")
{
panel1.Visible = false;
panel2.Visible = true; //this does nothing?
this.Text = "panel1 - | panel2 +";
}
}}
By changing the form text I confirmed that the event gets properly thrown and by stepping through the code I found out that if Node2 is selected, the
panel2.Visible = true
line does absolutely nothing and afterwardsVisible
is stillfalse
. Why does that happen? I'm really confused. Thanks.Sounds like
Panel2
is hidden behind something else, or possibly not even added to the form correctlyIf you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) [My Articles] [My Website]
-
I try to hide/show panels with controls depending on what TreeView node the user clicks (for large data inputs), but it doesn't work and I have no idea why not. This is my code for the TreeView's AfterSelect event:
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
{
if (treeView1.SelectedNode == null)
{
this.Text = "null";
}
else
{
if (treeView1.SelectedNode.Name == "Node1")
{
panel2.Visible = false;
panel1.Visible = true;
this.Text = "panel1 + | panel2 -";
}
else if (treeView1.SelectedNode.Name == "Node2")
{
panel1.Visible = false;
panel2.Visible = true; //this does nothing?
this.Text = "panel1 - | panel2 +";
}
}}
By changing the form text I confirmed that the event gets properly thrown and by stepping through the code I found out that if Node2 is selected, the
panel2.Visible = true
line does absolutely nothing and afterwardsVisible
is stillfalse
. Why does that happen? I'm really confused. Thanks.Apparently it didn't work because I had one panel over the other. But that is the entire point of this. Instead of putting a ton of controls next to each other or in TapControls, I want them to appear and disappear at the same location. How do I do that?
-
Apparently it didn't work because I had one panel over the other. But that is the entire point of this. Instead of putting a ton of controls next to each other or in TapControls, I want them to appear and disappear at the same location. How do I do that?
Megidolaon wrote:
Apparently it didn't work because I had one panel over the other.
It's not that at all. Let me run through the sequence of events you went through to construct this: 1) create two panels, one above the other. (could be side by side, I dunno). 2) Put controls on each panel. 3) Drag one panel over the other so they are in the same place. Now, when you make one visible and one invisible, you see one panel. When you make that panel invisibile, and the other visible, you see no panels. This is because when you dragged one panel onto the other it became a child of the panel you dropped it on. So when you make the master panel invisible, you hide all it's children - including the other panel. Leave them separate, and move them with your code in the form load event. It'll work now... Or better, use a TabControl instead of panels...it is a lot easier to maintain in the designer! :-D
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
-
Apparently it didn't work because I had one panel over the other. But that is the entire point of this. Instead of putting a ton of controls next to each other or in TapControls, I want them to appear and disappear at the same location. How do I do that?
as Griff said, you probably have one of the panels containing the other. There are many ways not to do that, here is one I use sometimes: - inside Visual Designer, add first panel, smaller than intended; - now add second panel next to the first one, also smaller than intended; - so far you made sure they both belong to the Form directly; - now select one, look for the Properties pane, and adjust the Location and Size properties by editing the numbers (watch the result each time you leave the property's value field); - then select the second one (if not visible, since overlapped by the other one, use the combobox in the properties pane), and give it the appropriate (maybe the same) values for Location and Size. The net result is you only see one of the panels in Designer, however you will see one or the other at run-time. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
as Griff said, you probably have one of the panels containing the other. There are many ways not to do that, here is one I use sometimes: - inside Visual Designer, add first panel, smaller than intended; - now add second panel next to the first one, also smaller than intended; - so far you made sure they both belong to the Form directly; - now select one, look for the Properties pane, and adjust the Location and Size properties by editing the numbers (watch the result each time you leave the property's value field); - then select the second one (if not visible, since overlapped by the other one, use the combobox in the properties pane), and give it the appropriate (maybe the same) values for Location and Size. The net result is you only see one of the panels in Designer, however you will see one or the other at run-time. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
Thanks. I didn't notice I had made panel2 the child of panel1 cause a single mm off and they were still independent. But changing locations works great. In the Visual Designer I have the panels next to each other and just overlay them at runtime like this:
panel2.Location = new Point(panel1.Location.X, panel1.Location.Y);
I still need to test how this works with the many different controls needed on other forms. After all the point of this is to switch from a huge cluster**** of nested tab controls to a more intuitive and easier usable design like for example VS options.
-
Thanks. I didn't notice I had made panel2 the child of panel1 cause a single mm off and they were still independent. But changing locations works great. In the Visual Designer I have the panels next to each other and just overlay them at runtime like this:
panel2.Location = new Point(panel1.Location.X, panel1.Location.Y);
I still need to test how this works with the many different controls needed on other forms. After all the point of this is to switch from a huge cluster**** of nested tab controls to a more intuitive and easier usable design like for example VS options.
If the Form is going to show mostly text, I tend to create the structured parts by run-time code, i.e. code I write myself, as opposed to me doing repetitive sequences of point-and-click operations inside Visual Designer. You could easily write a method that adds a Panel holding some TextBoxes/ListBoxes/TreeViews/whatever, positioning them, setting their Anchors/Docks, etc. Then call that method over and over. Some advantages: it becomes easier to change your mind about layout details; and you could fetch the data from some external store (file, database, ...) and make it much more dynamic. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
If the Form is going to show mostly text, I tend to create the structured parts by run-time code, i.e. code I write myself, as opposed to me doing repetitive sequences of point-and-click operations inside Visual Designer. You could easily write a method that adds a Panel holding some TextBoxes/ListBoxes/TreeViews/whatever, positioning them, setting their Anchors/Docks, etc. Then call that method over and over. Some advantages: it becomes easier to change your mind about layout details; and you could fetch the data from some external store (file, database, ...) and make it much more dynamic. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
Nah, it's not that much to do. But I tested it now with a form that has many controls for data input and it works like a charm. In the designer it's a huge form with all panels side by side for easy editing and the first thing I do when the form is constructed is change the size and hide all panels. Then I display the appropriate panel when selecting a node and hide all others.