Important alert: (current site time 9/3/2014 2:38:09 AM EDT)
 

VB icon

Icons in VB .NET Menus!!!!!

Email
Submitted on: 3/29/2002 10:27:04 AM
By: The New iSoftware Company!  
Level: Advanced
User Rating: By 7 Users
Compatibility: VB.NET
Views: 85274
 
     This is the first code on the .NET side of Planet Source Code's VB section to do this. I am proud to post a sample of Dr. GUI's sample code. I made a lot of changes to the original code like adding comments to it. It still needs some work. Well commented. Read the label of the screen shot, please. Don't forget to comment and vote! PS. It can be used in place of any menuitem. (Could not be uploaded. Here is the class.) ***New version bug (need help) email me if you wish to help! This is NOT C# code!

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Icons in VB .NET Menus!!!!!
// Description:This is the first code on the .NET side of Planet Source Code's VB section to do this. I am proud to post a sample of Dr. GUI's sample code. I made a lot of changes to the original code like adding comments to it. It still needs some work. Well commented. Read the label of the screen shot, please. Don't forget to comment and vote! PS. It can be used in place of any menuitem. (Could not be uploaded. Here is the class.) ***New version bug (need help) email me if you wish to help! This is NOT C# code!
// By: The New iSoftware Company!
//
// Side Effects:Does not highlight. Sorta basic.
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=179&lngWId=10//for details.//**************************************

Public Class clsMenuIcons
 'Original Code by Dr. GUI and the People at Microsoft.
 'Modifications By The New iSoftware.
 'Please give credit.
#Region " Visual Basic .NET Icons on MenuItems Class File "
 Inherits MenuItem
 Public Icon As Icon
 Public Font As Font
 Sub New(ByVal Text As String, ByVal EventHandler As EventHandler, ByVal Shortcut As Shortcut, ByVal Icon As Icon)
MyBase.New(Text, EventHandler, Shortcut) 'Creates Base Menu Meth. and Prop.
Me.OwnerDraw = True 'Required. Allows the OnMeasureItem and OnDrawItem overrides.
Me.Icon = Icon 'Sets the icon.
Me.Font = SystemInformation.MenuFont 'Sets font to normal, can be overridden.
 End Sub
 Protected Overrides Sub OnMeasureItem(ByVal E As MeasureItemEventArgs)
MyBase.OnMeasureItem(E) 'Do this first. Fires code for this.
Dim sfStringFormat As New StringFormat()
sfStringFormat.HotkeyPrefix = Drawing.Text.HotkeyPrefix.Show 'The _ for &.
sfStringFormat.SetTabStops(50, New Single() {0}) 'Declares tab.
If Icon.Height > Font.Height Then 'Sets height.
 E.ItemHeight = Me.Icon.Height + 2 'Icon as base.
Else
 E.ItemHeight = Me.Font.Height + 2 'Font as base. 
End If
E.ItemWidth = CInt(E.Graphics.MeasureString(AppendShortcut(), Me.Font, 1000, sfStringFormat).Width) + Icon.Width + 5 ' Width Set
sfStringFormat.Dispose() 'Clean Up!
 End Sub
 Protected Overrides Sub OnDrawItem(ByVal E As DrawItemEventArgs)
MyBase.OnDrawItem(E) 'Do this first. Fires code for this.
Dim brBrush As Brush
Dim sfStringFormat As New StringFormat()
E.Graphics.FillRectangle(SystemBrushes.Menu, E.Bounds) 'Draws BG.
If Not (Icon Is Nothing) Then
 E.Graphics.DrawIcon(Icon, E.Bounds.Left + 1, E.Bounds.Top + 1) 'Draws Icon. any size works at org size.
End If
sfStringFormat.HotkeyPrefix = Drawing.Text.HotkeyPrefix.Show 'The _ for &.
sfStringFormat.SetTabStops(50, New Single() {0}) 'Declares tab.
brBrush = New SolidBrush(SystemColors.MenuText) 'Sets the brush color.
E.Graphics.DrawString(AppendShortcut(), Me.Font, brBrush, E.Bounds.Left + Icon.Width + 10, E.Bounds.Top, sfStringFormat) 'Draws text.
brBrush.Dispose() 'Clean Up!
sfStringFormat.Dispose()
 End Sub
 Private Function AppendShortcut() As String 'Basicaly determines to draw the HeldButtton+AnyKey thing. 
Dim sTemp As String
sTemp = Me.Text
If Me.ShowShortcut And Me.Shortcut <> Shortcut.None Then
 Dim kKeys As Keys = CType(Shortcut, Keys)
 sTemp = sTemp & Convert.ToChar(9) & System.ComponentModel.TypeDescriptor.GetConverter(GetType(Keys)).ConvertToString(kKeys)
End If
Return sTemp
 End Function
#End Region
End Class


Other 7 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

3/30/2002 9:20:15 AMThe New iSoftware Company!

If you people are needing help using this read or use this (* = new line):*
Dim eh as EventHandler *
Dim WithEvents mnuMenuName as New clsMenuIcons("Text Caption", eh, Shortcut.SomeSC, SomeIcon)*
'The Form load *
mnuParentMenuItem.MenuItems.Add(mnuMenuName)*
'Events
Private Sub mnuMenuName_Click(ByVal sender as Object, ByVal e as EventHandler) Handles mnuMenuName.Click *
'Code Here*
End Sub *
(If this comment was disrespectful, please report it.)

 
3/30/2002 10:46:00 AMroswellevent

Great Code!!!!!
That what i need!
thx for sharing your code!
(If this comment was disrespectful, please report it.)

 
3/30/2002 8:17:57 PMThe New iSoftware Company!

Please vote.
(If this comment was disrespectful, please report it.)

 
4/12/2002 3:50:00 AMIanW

I can't believe Microsoft haven't made icon menus a standard feature. I'll keep this for when I make the move to .net.
(If this comment was disrespectful, please report it.)

 
5/24/2002 4:03:27 PMMattias

Its a good thing you made this!!
But it would be even greater to make the Office XP menu thing i mean THATS what i call menu!
But its still great you made this!!!
(Hehe i havent tried it yet but i hope it works)
I will give you 5/5 for this one!
(If this comment was disrespectful, please report it.)

 
6/5/2002 7:11:52 PMKetan

This didn't work for me .. where do you actually get the icon from? I know I have to pass it to the class but in the example it doesn't say how to get the icon from the path .. please help.

Thanks
Ketan
(If this comment was disrespectful, please report it.)

 
6/10/2002 5:34:40 PMNew iSoft

Apply a icon class to the Icon item.
(If this comment was disrespectful, please report it.)

 
6/19/2002 11:18:27 AMAmeen Tayyebi

It would be better if you provide documentation on how to use this and how to do it, how to place icons for several menu items, etc.
(If this comment was disrespectful, please report it.)

 
6/20/2002 12:54:19 PMFlavio

Cool!
(If this comment was disrespectful, please report it.)

 
1/5/2003 4:15:40 AM

What's better, VB 6 or .NET? i started 6 learning Edition, and now i can't find profesional edition anywhere,i have .NET but i don't like it as much, VB ^ was much, much easier that .NET :-(
(If this comment was disrespectful, please report it.)

 
5/6/2003 3:23:13 AMchenfs

Do you have any example on how to assign the icon class to your new MenuItem Class ?? Thanks
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.