Menustrip (with code)

885 views Asked by At

Im trying to learn how to add a menustrip with code. I want that my toolstripmenuitems stay next to each other like this:

But i'm getting this:

This is my code:

private void Form1_Load(object sender, EventArgs e)
{
    MenuStrip menu;
    menu = new MenuStrip();
    ToolStripMenuItem test;
    test = new ToolStripMenuItem("&test");
    test.DropDownItems.Add("aa", null, aa_click);
    menu.Items.Add(test);
    this.Controls.Add(menu);

    MenuStrip menu2;
    menu = new MenuStrip();
    ToolStripMenuItem test2;
    test2 = new ToolStripMenuItem("&test2");
    test2.DropDownItems.Add("zz", null, zz_click);
    menu.Items.Add(test2);
    this.Controls.Add(menu);
}

Thanks for helping me!

2

There are 2 answers

0
poke On

Don’t create multiple menu strips, but only a single one where you keep adding your menu items to:

MenuStrip menu = new MenuStrip();

ToolStripMenuItem test = new ToolStripMenuItem("&test");
test.DropDownItems.Add("aa", null, aa_click);
menu.Items.Add(test);

ToolStripMenuItem test2 = new ToolStripMenuItem("&test2");
test2.DropDownItems.Add("zz", null, zz_click);
menu.Items.Add(test2);

this.Controls.Add(menu);
0
Ansar Halidi On
    private void pesertaToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Peserta rt = new Peserta();
        rt.Show();
    }