Prevent Child Components when Overlapping Panel - c#

I have a WinForms application which has two panels which individually contain a usercontrol component (one in each panel). How ever, because both panels are the same size and in the same location, the top most panel becomes a child of the other panel. I want to be able to hide one panel and then show the other panel:
this.panel1.visibile = false;
this.panel2.visibile = true;
But when I hide panel1, the second panel is hidden as well.
How can I make panel2 a non-child of panel1?
I like to keep things simple because I'm new to C# Programming.

This is a common accident in the designer. Dropping the second panel on top of the first one will make it a child of the first panel. Two basic strategies to fix this:
View + Other Windows + Document Outline. Click the second panel in the list and drag it to the form. You'll need to fix up the Location property by hand by typing its value in the Property window.
Keep the upper left corner of the second panel just a bit to the left or top of the first panel. Put it in the right place in the form's constructor by assigning its Location property after the InitializeComponent() call.
Check this answer for a control that works well at design time and lets you easily flip panels at runtime.

The designer will do this automatically because it assumes that when you drag one control over another, you want to make it a child of that control.
What I usually do to get around this is to create the control in a different place on the form, and then use the properties to manually match the positions of sizes of the two controls.

Related

Why do some objects appear on top of others, and not always like that?

I created 2 empty labels, using winforms, to cover some groupboxes
They were created exactly the same way, but they dont behave the same way
The first one:
The second one:
The first covers everything, the second one only covers one groupbox, and displays the other two.
I tried the right click+ send to back /send to front, but I believe this doesn't mean anything in the code, it only purpose is to allow you to visualize better what you are placing in your form, I guess.
Can someone shed some light?
Groubox, Panel, TabPage etc are containers in which we keep controls. Form itself is also a container..
When you drag a label or any other control to place in a form, and if you want to hide the other controls, you should place the label in the form itself and the label should not become child of any of the groupBoxes.
For that the left top corner of the label you drag should not go inside any of the groupBoxes. This is only when you drag. If you want to position at a point inside groupBox, then Drag the control outside the groupBox and then use keyboard arrows to position, this will not let the control to become child of the groupBox. You can also set position on property window.

Resize components inside user control while maximiziing

I am using c# and i am creating a simple design where i have a user control and some components inside it like treeviews and buttons. I am trying to fill a treeview with some nodes and drag-drop these nodes to other treeview and use the buttons to also copy nodes from side to another.
The problem i am having is that when i maximize the window containing this user control there is no effect on the inside components.
I have set the Dock property of the user control to Fill.
I have changed the anchor properties of the buttons and treeview inside the user control but the behavior wasn't as expected. For example i have tried to set the anchor property for the right treeview to be Top,Bottom,Left => and the result was a disaster
I have also done a lot of combinations for the anchor property of all the buttons but nothing gave me the right behavior. I just only need to maximize the window form and the controls will be maximized with the same proportion.
It sounds like you want a "3 column" interface where you have a TreeView on either side and Buttons in the middle to allow movement between the two. Assuming this is correct, you can accomplish your automatic resizing by using a TableLayout.
Essentially, it would be like this:
Add a TableLayout and edit the rows/columns such that there is a single row with 3 columns:
The first and last column would be sized at 50% (and would hold your TreeViews).
The middle would be an absolute size of (for example) 120. This would hold your Buttons.
Set the properties of this new TableLayout to Dock -> Fill the form. This will size the entire table to grow with your form.
Add your TreeView controls to the left/right columns and set them to Dock -> Fill the respective columns. Since these columns are dynamically sized, they will grow with the form.
In your middle column, add a Panel and set it to Dock -> Fill. We add a Panel here to hold the multiple Buttons you use for movement. This Panel will not grow in size because the middle column is sized absolutely.
Add your Buttons to the middle Panel.
Without a screenshot, I'm not completely sure what you are trying to achieve but I believe this is along the lines of it. The nice thing about this setup is there is zero code involved.

Ungroup panels?

So I am working on a program that has several screens which causes it to have overlapping controls (Buttons and lists).
I put the controls in panels which works great and then do show/hide for the panels.
This also works well.
I am having a problem now that I am up to several panels where when I move one panel up it gets absorbed by another and I need them to stay separate.
Example: When I move panel2 into place over panel1, panel2 becomes part of panel1. Then when I do panel1.Hide() and panel2.Show(), panel2 is still hidden because it is part of panel1. is there a way for me to ungroup these or move panel2 into place without it automatically becoming part of panel1? (I know I can show hide the controls inside of the panels, but this will add a lot of complexity because I have a ton of controls)
Perhaps there is a better solution than using panels?
You can use the View + Other Windows + Document Outline tool window to get these panels separated again. Drag the inner panel back to the parent. You'll then also have to edit the Location property to get it back in the right position.
This is annoying of course and good odds that you'll have to do this repeatedly. There's a better way to go about it, a TabControl has good design-time support and also has the same "overlapping panel" metaphor. You just need to hide the tabs at runtime. That's pretty easy to do, check the StackPanel control in this answer.

Modify docking layout in WinForms

I have a SplitContainer on my form that has its Dock property set to Fill. It contains several child controls, many of which have event handlers attached to them. Later I decide to put a StatusStrip at the bottom of my form. Guess what, I can't set the StatusStrip to dock to the bottom of my form. The SplitContainer will continue to Fill the entire form. Even though the StatusStrip apparently gets docked to the bottom, it actually hides the bottom part of the SplitContainer behind it.
The only around it is to CUT the SplitContainer and then PASTE it back. Cutting the SplitContainer makes the StatusStrip the only control on my form and thus lets it capture the bottom docking. Afterwards, pasting the SplitContainer allows it to fill the remaining area. In short, docking uses First Come, First Serve method.
Now since my controls have lots of event handlers attached to them, cutting and pasting becomes a nightmare for me. Having my project in C# means I have to attach all those event handlers manually.
Is there a better work around?
This is a z-order issue between the splitter and the statusstrip. When you have a control you want to dock fill and one or more controls you want to dock top, left, right, or bottom, you have to have the fill control be the first in the z-order.
The better way is to open the Document Outline tool, select the SplitContainer and use the up or down buttons to change its z-order.
I should add that in Winforms the z-order is specified by the order in which you add controls to the Controls collection. That order determines the order the associated system controls are created, hence their z-order. Using the Document Outline tool to alter z-order simply causes the generated code to be re-ordered.

Winforms: FlowLayoutPanel with Docking

This is in winforms. I am creating a User control that is basically a FlowlayoutControl filled with other User Controls. I need each of the controls added to be docked to the top of the previous (from left to right). Unfortunately it looks like the flowlayoutcontrol ignores any of the docking properties. Is there any way to dock the controls inside there? I need it to fill the item from left to right, but the items should be laid out like a list view. Theres really no code i can provide due to the fact that its a matter of figuring out what approach to take.
Getting the FlowLayoutPanel to dock right is tricky. From the original question, you want something like a list view. It's important to know that ONE of the items in your list (the widest one) defines a "virtual column" in the FlowLayoutPanel. The rest of the items will follow it. You can prove this in the VS designer by dragging one of the items to the right. The 'virtual column' will follow it, and if your other items are anchored they will follow the virtual column.
Note that you can't anchor the control that is defining the column. It has nothing to anchor to and strange things will happen.
Do do all this programatically, handle the Layout event on your FlowLayoutPanel and put code similar to the code below. It's important that in the designer all the items in your list are not docked and and have their anchoring set to 'none'. I spent a day on this yesterday and doing that in the designer is what made the code below work.
flowLayoutPanel.Controls[0].Dock = DockStyle.None;
flowLayoutPanel.Controls[0].Width = flowLayoutPanel.DisplayRectangle.Width - lowLayoutPanel.Controls[0].Margin.Horizontal;
for (int i = 1; i < flowLayoutPanel.Controls.Count; i++)
{
flowLayoutPanel.Controls[i].Dock = DockStyle.Top;
}
FlowLayoutPanel.FlowDirection Property indicates the flow direction of the FlowLayoutPanel control.
FlowLayoutPanel.WrapContents Property indicates whether the FlowLayoutPanel control should wrap its contents or let the contents be clipped.
The docking properties of the FlowLayoutPanel are for the panel itself (like if you wanted the FlowLayoutPanel docked to the left of the form, etc.), not the container of controls inside of it.
Try playing with the DefaultPadding and DefaultMargin properties, these apply to the spacing of the controls it contains

Categories