views:

358

answers:

4

I noticed that adding a MenuStrip (from the VS Toolbox) to my form design doesn't yield a menu bar like many native Windows applications. Instead I get a menu bar like VS'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, Windows Explorer and others? (Preferably with the designer, but I wouldn't mind adding it programmatically either.)

A: 

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
A: 

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
I'm kind of just asking this out of curiosity. I'm fine with a MenuStrip; just wondering if it's possible in C# to do this kind of thing.
BoltClock
+4  A: 

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
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.
lucifer
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
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?
lucifer
@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
I'm sorry. Don't be so hard on yourself, can happen to all of us :-)
lucifer
+5  A: 

This is easy.

Goto 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!

Happy coding!

lucifer
Wow, I had no idea I could add stuff to my Toolbox like that. Thanks a lot!
BoltClock
You're welcome. Happy to have helped. :)
lucifer
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
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
Yeh. If there's one thing I would love in the MainMenu, is the option to add images next to menu items...
lucifer
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
@BoltClock: dear god, are you using pre-2.0 .NET?
MusiGenesis
@MusiGenesis: No, .NET 3.5 actually. I'm new :P
BoltClock
Ooh! Yeah awesome, thanks MusiGenesis!
lucifer