45

I noticed that adding a MenuStrip (from the Toolbox) to my form design doesn't yield a menu bar like the one seen in many native Windows applications. Instead I get a menu bar like Visual Studio's own. None of the style settings for MenuStrip appear to mimic the much more common native menu bar.

Is there a way to add a menu bar to my Windows Forms application that looks the same as the one you see in Notepad, Task Manager and others? (Preferably with the designer, but I wouldn't mind adding it programmatically either.)

Screenshot for illustration:

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • 1
    What about it is that different than the menu found in notepad or windows explorer? It is very similar to that found in office 2003, so it isn't unusual. – Jared Peless May 06 '10 at 03:09

4 Answers4

68

Go to your Toolbox, right click anywhere inside and select "Choose Items".

When the dialog loads and appears, scroll down til you see MainMenu. Add that to the toolbox, and you've got yourself a native menu bar!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 3
    Wow, I had no idea I could add stuff to my Toolbox like that. Thanks a lot! – BoltClock May 06 '10 at 03:39
  • 6
    I was not aware of this component. I gave it a go and it has *very* limited: I was not able to change the colour in the designer, no support for localisation, no images, etc. If this is what you want, go for it. There *are* reasons why this has been superseded by the MenuStrip. – Charlie Salts May 06 '10 at 03:54
  • 1
    Yep, it is. I don't need the versatility of the newer MenuStrip so I'm open to options like this. I also just found that it's a .NET Framework 2.0 class. – BoltClock May 06 '10 at 04:09
  • 1
    Yeh. If there's one thing I would love in the MainMenu, is the option to add images next to menu items... –  May 06 '10 at 04:18
  • Some of the constructors for `MenuItem` take an `Image` as a parameter, so I think images can be added but not from the designer. – MusiGenesis May 06 '10 at 04:33
  • @BoltClock: dear god, are you using pre-2.0 .NET? – MusiGenesis May 06 '10 at 04:34
  • @MusiGenesis: No, .NET 3.5 actually. I'm new :P – BoltClock May 06 '10 at 04:40
  • Please see my new answer below for a better alternative – Cris McLaughlin Dec 03 '12 at 20:06
  • Is it possible to add icons in MainMenu without altering the appearance?, OwnerDraw alters the Windows 7 style. –  Apr 13 '15 at 22:26
  • Oh my god, I have never know anything about the toolbox like that! – raspiduino May 30 '21 at 10:42
  • My Visual Studio 2022 preview crashes when i try to rename a menu item. – Yılmaz Alpaslan Jun 23 '21 at 13:57
  • Just wanted to mention, that the [docs](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.mainmenu?view=netframework-4.8.1) at say: `This class is not available in .NET Core 3.1 and later versions.` – Xan-Kun Clark-Davis Jun 13 '23 at 01:39
23

You can do this by setting your form's Menu property, like this:

private void Form1_Load(object sender, EventArgs e)
{
    this.Menu = new MainMenu();
        MenuItem item = new MenuItem("File");
        this.Menu.MenuItems.Add(item);
            item.MenuItems.Add("Save", new EventHandler(Save_Click));
            item.MenuItems.Add("Open", new EventHandler(Open_Click)); 
        item = new MenuItem("Edit");
        this.Menu.MenuItems.Add(item);
            item.MenuItems.Add("Copy", new EventHandler(Copy_Click));
            item.MenuItems.Add("Paste", new EventHandler(Paste_Click)); 
        // etc ...
}

private void Save_Click(object sender, EventArgs e)
{
    // save
}

These menus will look like "normal" system menus.

I couldn't find any designer support for this, though. In my defense, I didn't try real hard.

MusiGenesis
  • 74,184
  • 40
  • 190
  • 334
  • There is an easier solution to this. It can be found in the "Choose Items" dialog. The component's called "MainMenu", and has Form Designer support, too. –  May 06 '10 at 03:35
  • 1
    This is a great answer. It may not be as simple or friendly as using the designer, but it isn't worth a downvote. – BoltClock May 06 '10 at 03:40
  • I agree. This is a great answer, and will come in handy when I'm working on projects on computers that don't have an IDE installed! I just tried to upvote, but it says the question is too old, and I can't vote until the question is edited, huh? –  May 06 '10 at 04:21
  • @jts: it's edited (so you can undo your downvote), and I accept your apology. :) I am a dumbass, though - I did look through the Choose Items dialog and found 3 MainMenu controls in there, 2 of which were already checked although none was in my toolbox, so I gave up early. I try never to wrestle with Visual Studio after midnight. – MusiGenesis May 06 '10 at 04:29
  • 1
    I'm sorry. Don't be so hard on yourself, can happen to all of us :-) –  May 06 '10 at 07:47
  • I was looking for exactly this kind of code for writing winforms in F#. Thanks! – Ken Feb 10 '15 at 19:01
8

Instead of using a the MainMenu component you can create your own renderer for the MenuStrip component. The advantage here is being able to add images to MenuStripItem objects. Here is the pastebin for the custom renderer:

NativeRenderer

There are different themes that can be applied in the constructor of the renderer. Try them all to see the native themes. To use this renderer simply set the instance to the MenuStrip Renderer property:

menuStrip.Renderer = new NativeRenderer([theme]);
Cris McLaughlin
  • 1,201
  • 2
  • 14
  • 23
  • 1
    Ooh, this looks useful! If I do end up using it (I've kinda moved on to [greener pastures](http://msdn.microsoft.com/en-us/library/ms754130.aspx)), I'd much like to credit the source. Or is it in the public domain? :) – BoltClock Dec 03 '12 at 23:40
  • Sorry BoltClock, I haven't lurked here in quite some time. I have been trying to find the original author (it was posted on code.google.com somewhere). I will post the original link when I find it. – Cris McLaughlin Jul 15 '13 at 00:58
  • @BoltClock I found it! Please give all the credit to this genius: http://code.google.com/p/szotar/source/browse/trunk/Client/Szotar.WindowsForms/Base/NativeToolStripRenderer.cs – Cris McLaughlin Oct 29 '13 at 15:27
  • seems like it can't find the ToolbarTheme type – Shadowblitz16 Feb 27 '19 at 02:24
4

I normally set the MenuStrip's RenderMode to System which gives a minimalist, single colour menu (no gradients or anything decadent like that).

If that does not go far enough, then you'll likely have to jump through some low-level hoops to get what you want.

Charlie Salts
  • 13,109
  • 7
  • 49
  • 78