3

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?

rj tubera
  • 747
  • 4
  • 29
  • 51

3 Answers3

4

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.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • +1: This is how I've always *hidden* a panel behind another when I didn't want to go to the trouble of having different `UserControl` options to instance instead. It would be nice if there were a designer-friendly way to overlap panels ...kind of like a hidden tab. – IAbstract Sep 10 '11 at 14:14
  • @IAbstract - check this: http://stackoverflow.com/questions/2340566/creating-wizards-for-windows-forms-in-c/2342320#2342320 – Hans Passant Sep 10 '11 at 14:31
0

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.

Alexander Galkin
  • 12,086
  • 12
  • 63
  • 115
  • 2
    It would be nice if Lock Controls worked this way. It actually doesn't, the locked panel still sucks up the overlapped panel. Try it. – Hans Passant Sep 10 '11 at 14:03
  • thanks!!! I see so that's how the lock controls do... :))) Btw, how to marked correct answers as CORRECT? – rj tubera Sep 10 '11 at 14:04
  • @Hand Passant: Hmm... I checked it before I posted my answer, but now I see that sometimes it doesn't work. In any case, the panel is sucked up only if it position completely within an existing container, overlap is still possible. But I will correct my answer – Alexander Galkin Sep 10 '11 at 14:17
0

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.

havardhu
  • 3,576
  • 2
  • 30
  • 42