Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to take away vertical space for programmatically added menu?

I add a MenuStrip to my form and I would like to add other controls below it like usual Point(0, 0) is the top left corner of empty form space. After I add the menu to my form and add more controls they overlap each other. So I want to take away some height of the client rect for the menu and a button with Location = (0,0) must be RIGHT below the menu.

How do I do that ?

If I assign a MainMenu to Menu property of the form it does that automatically but I really want and need to use MenuStrip.


Edit: this doesn't work:
MenuStrip menu = new MenuStrip();
menu.Items.Add("File");
menu.AutoSize = false;
menu.Height = 50;
menu.Dock = DockStyle.Top;
MainMenuStrip = menu;
Controls.Add(menu);

Button b = new Button();
b.Text = "hello world";
b.SetBounds(0, 25, 128, 50);
Controls.Add(b);

While this works like I would like it to do with MenuStrip:

Menu = new MainMenu();
Menu.MenuItems.Add("File");

Button b = new Button();
b.Text = "hello world";
b.SetBounds(0, 0, 128, 50);
Controls.Add(b);
like image 458
Bitterblue Avatar asked Nov 04 '22 03:11

Bitterblue


2 Answers

When you SetBounds(0, 25, 128, 50), you are actually setting b.Top to 25 (the second parameter). In order to set the top bound relative to the menu control, use:

b.SetBounds(0, menu.Bottom, 128, 50);

[UPDATE]

Alternatively, you could use:

public partial class Form1 : Form
{
    private int menuStripHeight = 50;

    public Form1()
    {
        InitializeComponent();
        this.ControlAdded += Form1_ControlAdded;

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        MenuStrip menu = new MenuStrip();
        menu.Items.Add("File");
        menu.AutoSize = false;
        menu.Height = menuStripHeight; ;
        menu.Dock = DockStyle.Top;
        MainMenuStrip = menu;
        Controls.Add(menu);

        Button b = new Button();
        b.Text = "hello world";

        // note that the position used is 0,0
        b.SetBounds(0, 0, 128, 50);

        Controls.Add(b);
    }

    void Form1_ControlAdded(object sender, ControlEventArgs e)
    {
        if (e.Control.GetType().FullName != "System.Windows.Forms.MenuStrip")
            e.Control.Top += menuStripHeight;
    }
}

[UPDATE 2]

Or you could just use a Panel:

MenuStrip menu = new MenuStrip();
menu.Items.Add("File");
menu.AutoSize = false;
menu.Height = menuStripHeight; ;
menu.Dock = DockStyle.Top;
MainMenuStrip = menu;
Controls.Add(menu);

Panel p = new Panel();
p.SetBounds(0, menuStripHeight, this.Width, this.Height);
Controls.Add(p);

Button b = new Button();
b.Text = "hello world";
p.Controls.Add(b);
b.SetBounds(0, 0, 128, 50);
like image 58
Alex Filipovici Avatar answered Nov 14 '22 15:11

Alex Filipovici


Use DockStyle.Top in both MenuStrip and Panel, but add them in the reverse order. Adding a control with Dock=Top puts this last control closest to the border, that is, on top of all the other controls. So without resorting to private constants and event handlers:

MenuStrip menu = new MenuStrip() {
  AutoSize = false,
  Dock = DockStyle.Top
};
menu.Items.Add("File");

Panel p = new Panel(){
   Dock = DockStyle.Top
};

Controls.Add(p);
Controls.Add(menu);
MainMenuStrip = menu;

Button b = new Button(){
  Text = "hello world"
};
p.Controls.Add(b);
b.SetBounds(0, 0, 128, 50);
like image 27
Lutz Lehmann Avatar answered Nov 14 '22 16:11

Lutz Lehmann