Search code examples
c#tabspanelalways-on-top

How can I set a panel to be always on top when changing tabpages in C#?


I have program with two tabs in a TabController, I also have a panel I want to always have in front. Despite what tabpage I am in. I tried setting the panel to BringToFront(), but that don´t seem to work when I change tabpage. Any suggestions how to solve this?


Solution

  • If the Panel is contained by the TabPage, then you'd have to manually switch it to the current tab whenever the tab changes, then call BringToFront().

    An alternative would be to make the Panel so it's directly contained by the Form, but in front of the TabControl (like it's "floating" over it). Then it would just stay there. You'd have to either manually fiddle with the Panel's Location() property to get it right (you couldn't drag it over the TabPage as then it would drop into it), or you could position it properly via code in the Load() event of the Form.

    Edit:

    For instance, if you properly positioned "panel1" in the TabPage at design-time, you could switch it to the Form using code like this:

        private void Form1_Load(object sender, EventArgs e)
        {
            Point pt = panel1.PointToScreen(new Point(0, 0));
            panel1.Parent = this;
            panel1.Location = this.PointToClient(pt);
            panel1.BringToFront();
        }