user1051434
user1051434

Reputation: 167

Panel fixed position

I have a Panel in a Form and I want it to be in a fixed position on the top of the Form. So, if I scroll down, the panel will always be visible.

Do you know how can I do this?

Upvotes: 2

Views: 5431

Answers (1)

LarsTech
LarsTech

Reputation: 81610

Have two panels, dock fill one to the form, but make sure the floating panel is part of the Forms's control collection, not the dock-filled panel's collection. Harder to do with the designer sometimes.

Sample Application:

public partial class Form1 : Form {
  Panel backPanel;
  Panel floatPanel;

  public Form1() {
    InitializeComponent();

    floatPanel = new Panel();
    floatPanel.BorderStyle = BorderStyle.FixedSingle;
    floatPanel.SetBounds(0, 0, 128, 64);
    this.Controls.Add(floatPanel);

    backPanel = new Panel();
    backPanel.Dock = DockStyle.Fill;
    backPanel.AutoScrollMinSize = new Size(0, 1000);
    this.Controls.Add(backPanel);
  }
}

That's for a floating panel that's on top of an existing panel. Doesn't make a lot of gui sense because what would happen if a control gets scrolled to underneath the floating panel?

If you are looking for the Top panel to just be above the scrolling portion, add the two panels in reverse order with the "top" panel being dock filled to the top so that the top panel isn't placed "behind" the scrolling panel.

Sample Application:

public partial class Form1 : Form {
  Panel backPanel;
  Panel topPanel;

  public Form1() {
    InitializeComponent();

    backPanel = new Panel();
    backPanel.Dock = DockStyle.Fill;
    backPanel.AutoScrollMinSize = new Size(0, 1000);
    this.Controls.Add(backPanel);

    topPanel = new Panel();
    topPanel.Height = 64;
    topPanel.Dock = DockStyle.Top;
    this.Controls.Add(topPanel);
  }
}

Upvotes: 1

Related Questions