Visual C#/WinForms Panels - c#

I am trying to overlap panels so that whenever I click a button A certain panel will be visible.
However doing this job is very tricky as because the panels doesn't overlap.
for ex. I have panel 1 and panel 2:
I make panel 2 the same as panel 1,
Whenever I position them on the same position...
Sometimes, the panel 2 becomes a member of panel 1 and whenever I set the visibility of panel 1 to true panel 2 also shows up.
What I want is that the two panels to overlap each other.
"Btw, I'm making a vertical tab that's why I thought that hiding, unhiding the panels might be my best approach.
Is it possible to make the panels overlap each other?

The designer is fighting you do get them overlapped. You need to use a little trick to stop the bottom panel from sucking up the overlapping panel. Put it overlapping panel somewhat off towards the upper-left so they are truly overlapped. Then put it in the right spot by adding code to the form constructor:
public Form1() {
InitializeComponent();
panel2.Location = panel1.Location;
panel2.Size = panel1.Size; // optional
}
Another way to do it is with View + Other Windows + Document Layout. You can drag and drop the inner panel to the outer container (form). You will however have to edit the Location property by hand.

It is absolutely possible to have overlapping panels.
The problem you're facing is that the GUI editor treats your panel as containers (that is true) and as long as you place something (including other panel) within a panel it is "nested" in this container.
To avoid this behavior first place one panel and position/size it appropriately. Then right-click on it and choose "Lock controls". That will lock all current form controls and you will be able to put new controls -- including panels -- directly over them, without any fear that something will be nested or somehow placed inside an existing container.
And of course your controls can overlap -- consider only the order or control creation, that will also define the z-order of them in the form -- controls added later and drawn later and thus are positioned on top of those added earlier.
EDIT: Unfortunately I wasn't completely right with my answer. Locking panels does not prevent them from sucking up the controls places entirely within them. But in case of a partial overlap both containers are created on the same level of deepness, so the problem does not exist in case of overlapping panels, as it was asked in the question.

The Panel has a property called Location, which you can modify to fit your needs. As long as you manage to place your Panel so that it is given the correct parent, you can change the position by altering the Location property later. There's really no need to put design code into the constructor or anything such.
And to place the panel in the correct parent, just have the parent selected and double click the Panel control in the toolbar, rather than dragging it into the form manually. There's really no need to try to fight the designer on this one.

Related

Winform control becomes child of other control, as opposed to being added to the form.

So I have three panels all stacked on top of each other in the same location in a windows form application.
Each time I add another panel on top it becomes a child of the previous panel. This means each panel cannot be shown independently, ie, to show panel 2 I must first show panel1 (the parent). I'm fairly sure this is what is happening. However I need to be able to call panel2.show(), whilst panel 1 is hidden.
Is there a way to work around this?
This is a WinForms designer issue. You can either try moving the panels around and set the position in the properties or enter code behind and set the parent-child relationships manually.
basically don't just drag and drop panels on top of each other. Try to programmatically code their locations. Look for this kind of code in your Form.Designer.cs this.panel2.Controls.Add(this.panel3); and delete them.
Add all your panels in the form part which is supposed to be at the bottom.
this.Controls.Add(this.panel1);
this.Controls.Add(this.panel2);
this.Controls.Add(this.panel3);
This should be able to put all panels as form children.

Hiding a panel should force controls below to move up and resize the form

OK, this is driving me a bit nuts at this point. Time to ask the crowd:
I have a form that consists of: two vertical "halves" created by a SplitContainer. Inside the bottom "half" / SplitContainer panel... I have:
A set of fields/controls for the user to fill out, contained within a panel.
Below that, a panel containing buttons for "submit", "cancel"...and one that's "show/hide". This "show/hide" is intended to show/hide the panel I just described in point 1 above.
What I want to happen is: when the user clicks to "hide" the panel of controls, not only do those controls vanish, but also:
The panel containing the submit, cancel and show/hide buttons moves up (so that we don't have this big blank space where the now-hidden panel once was).
The form resizes itself vertically so as to make up for the now-hidden panel and the fact that we've moved the bottom controls upwards.
I have no problem getting the panel of controls/fields to show/hide. But I can't seem to figure out how to accomplish the other two tasks. I've tried various combinations of AutoSize, AutoSizeMode, and Dock options.
Any suggestions?
If you put all your controls into a FlowLayoutPanel where the children are arranged vertically, then hiding some controls should cause the "lower" controls to move up the page.
You might still need to write some code to resize the form itself once the optional content has been hidden or before it's shown.

Overlay two panels in a windows form application

A Windows Form application displays a main form. This main form contains few different Panel controls. Depending on some condition in that form one of these Panel controls is to be made active while the others are made invisible.
First panel contains a DataGridview, Second panel contains controls to display the details of the DataGridView. On Add button click (or) On selecting a record in DataGridView the second panel should be visible. I am using Visible property to show and hide panel, but the gap is showing there as shown in screenshot.
Please suggest best way to handle this.
You just need the panels to share the same location. You probably don't want to do that at design-time, as it makes future maintenance of the form difficult.
In the Form.Load event, set the location of the bottom panel to match the location of the top panel.
PanelDetails.Location = PanelDataGrid.Location;
Now when you hide one and show the other, they'll appear in the same place.
The smartest way is to keep the panels separate at the designer. This way any editings made on them can be done more easily, and all actual objects can be seen immediately.
In the designer, set the other panels' Visible properties to False, excep the first one on the top.
Form.Load:
Set all the other panels' locations to match the location of the first panel (on the top)
Set the form's height to match a desired height, which fits to your heightest panel
Use the buttons to toggle panels visibility (and possibly the form height too)

Panels in same area in winform

I currently have a windows form that I am using as a "holder" for many panels. All of these panels are being used like pages, so say I have a default panel, a details panel etc. They all cover the exact same area and i am using a top menu to have buttons to show/hide the correct panel.
The problem I have with this is that I cannot select one panel and edit it or simply bring it to the front to edit it in "design" mode, I currently have to keep sending the top panel to the back until I get the one I need. This is okay now, but eventually my program will have over 20 panel type pages, and this will get tedious.
Is there a better way to manage this, or is there an easier way than using panels?
All these panels are used like pages
Above line in your question suggests that you should be using TabPage instead of Panels.
Therefore, You can use a TabControl, and add TabPage to it both at design time as well as at runtime easily, and add controls of your choice to the different TabPage(s).
According to wikipedia:-
Tab is one that allows multiple documents to be contained within a single window, using tabs as a navigational widget for switching between sets of documents. which is essentially what you want to do.
As shown in the picture
You can then use Button to switch b/w different TabPage or show/hide TabPage.
It will not only solve your problem of designing at design time, it will also provide a "clean" user interface. Hope that helps!
Check this:-
http://en.wikipedia.org/wiki/Tab_(GUI)
Use a TabControl instead. It will allow you to switch an active panel also in design time.
If you want to keep the panel method there is a way of working around the problem of one panel becoming an element of another. Drag the panel outside the boundary of the other panel (so that it is no longer an element of the other panel) and then re-position it using the arrow keys on your keyboard (i.e. not by dragging it into position over the other panel using the mouse). It seems like the panel will only become an element of another panel if it is dragged into position over the top of another panel using the mouse.

C# WinForms Move elements between panels

I need to create a form with two panels:
1. Destination
2. Source
On the source panel there will be picture boxes. I need to be able to move it from source to point at destination panel with mouse.
I have a problem connected with different coordinates of the panels.
Please, help with advice or an idea what to do.
Moving those controls requires changing their Parent property. That's not easy to do, there is no good time to do this while the user is dragging with the mouse. You'll also get the effect of the panel clipping the control, you cannot display it on both with half of the control on one and the other half on the other panel. And yes, you have to change the Location property of the control when you change the parent or it will jump.
Punt the problem, don't use two panels. It only has to look like a panel, easily done by drawing one in the form's Paint method (or OnPaint override, better). Use e.Graphics.DrawRectangle or FillRectangle.

Categories

Resources