Click here to Skip to main content
15,885,216 members
Articles / Programming Languages / Visual Basic

Custom Menus Made Easy

Rate me:
Please Sign up or sign in to vote.
4.00/5 (5 votes)
16 Jul 2006CPOL2 min read 53K   913   24   7
An easy way to make your menus stand out.

Sample Image - ColorObject.jpg

Introduction

Microsoft has upgraded the basic MenuStrip object. Now you can easily have images with it, woohoo! What they didn't do, is make you able to change colors (and by change colors, I mean gradient colors), and let's face it, if your GUI isn't blue, then why would you have a blue menu? The user might be a little confused. And, that's where this comes in; instead of paying for some over-priced control, you can use the ToolstripProfessionalRenderer, and the best part is that I have wrapped everything you need into a nice little control.

How it Works

This control was a bit interesting to make. I am sure people will point out code issues and stuff, but keep in mind, I wasn't originally going to put this on the web. Back to the code, it uses the ToolstripProfessionalRenderer with an inherited ProfessionalColorTable. What I then did was override almost all MenuStrip properties with my own. This is where it gets messy. Since the original properties are ReadOnly and we want to write them, I had to create another set of properties to write to the local variables the ReadOnly variables get their information from. Then, to make it accessible from an object, I had to put the writeable properties in a custom object, then create a way for the object and the class to share data.

How to Use

Overall, not so bad, eh? This control is pretty easy to use, and from what I can find, there is only one bug which I have yet to find an answer to. This is how to use the ColorObject:

First, you must declare the ColorObjectRenderer to be the renderer of the MenuStrip.

VB
Public Sub New()

    ' This call is required by the Windows Form Designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.

    ToolStripManager.Renderer = Me.ColorObject1.Renderer

End Sub

This tells the MenuStrip to be rendered using Me.ColorObject1.Renderer. All the other things are taken care of. Renderer is a property of my object, just FYI. Now, here is how to use it and where the bug is. In any method, just change one of the color properties. Simple, right? But there is a problem, the control isn't automatically redrawn; after you change the color, you have to Invalidate the MenuStrip to redraw. I haven't found a way to programmatically do this, seeing as there isn't a way to access the menu item my object renders. If anyone knows a way around this, please tell me so I can update my code.

VB
Private Sub AnySub()
    Dim clr As New ColorDialog

    With clr
        If .ShowDialog = Windows.Forms.DialogResult.OK And .Color <> Nothing Then
            Me.ColorObject1.SecondMenuBarColor = .Color
            Me.mnuNew.Invalidate()
        End If
    End With
End Sub

Closing

I hope you found this article helpful. If you didn't, please leave a comment telling me why, so next time I can do better. Thanks!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
I am iwdu15, a college frosh who will be majoring in Computer Science. I use VB.Net, C#, C++, J#, Java, and some C.

Comments and Discussions

 
GeneralFont Color Pin
JoeFisher7130-Sep-08 3:06
JoeFisher7130-Sep-08 3:06 
GeneralBlack Theme Pin
JoeFisher7126-Sep-08 11:00
JoeFisher7126-Sep-08 11:00 
GeneralRe: Black Theme Pin
sokket29-Sep-08 15:36
sokket29-Sep-08 15:36 
GeneralThank U so much Pin
Nguyen Dinh Trung28-Jun-07 0:33
Nguyen Dinh Trung28-Jun-07 0:33 
GeneralRe: Thank U so much Pin
iwdu15028-Jun-07 11:46
iwdu15028-Jun-07 11:46 
GeneralThanks! Pin
Lividum23-Feb-07 17:46
Lividum23-Feb-07 17:46 
Hey Dude

First of all THANKS A LOT!

This is really what i was looking for cause i hate the blue margin color.

Only a little small thingy you didn't make it possible to change the mid color so I added it myself.

But anyway it's greatSmile | :)
Big Grin | :-D
Thanks!
GeneralRe: Thanks! Pin
iwdu15024-Feb-07 15:43
iwdu15024-Feb-07 15:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.