MouseOver Menu VB.NET

S

S. Viswanathan

Hi All,

I want to update the statusbar when mouse over on the each menu item. How
to write the code for this. Is there any specific events in menu?

Thanks and Regards
S. Viswanathan
 
K

Ken Tucker [MVP]

Hi,

I dont know of any specific mouse enter and leave events. You can
an owner drawn menu to know when the mouse is over an item and use a timer
to clear the text in the status bar. You need four menu items for this to
work mnufile, mnufileopen, mnufileclose, mnufilequit. All the mnuitems but
mnufile need to be set to ownerdrawn. mnuFile is the top level menuitem.
Hope this helps.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

StatusBar1.Text = ""

End Sub

Private Sub mnuFileClose_MeasureItem(ByVal sender As Object, ByVal e As
System.Windows.Forms.MeasureItemEventArgs) Handles mnuFileClose.MeasureItem,
mnuFileOpen.MeasureItem, mnuFileQuit.MeasureItem

Dim g As Graphics = e.Graphics

Dim mnu As MenuItem = CType(sender, MenuItem)

Dim sz As SizeF = g.MeasureString(mnu.Text.ToString, Me.Font)

e.ItemHeight = CInt(sz.Height) + 5

e.ItemWidth = CInt(sz.Width) + 15

End Sub

Private Sub mnuFileClose_DrawItem(ByVal sender As Object, ByVal e As
System.Windows.Forms.DrawItemEventArgs) Handles mnuFileClose.DrawItem,
mnuFileOpen.DrawItem, mnuFileQuit.DrawItem

Dim br As Brush = SystemBrushes.WindowText

Dim brBack As Brush

Dim rDraw As Rectangle

Dim r As Rectangle = e.Bounds

Dim mnu As MenuItem = CType(sender, MenuItem)

Dim strText As String = mnu.Text.ToString

Dim x As Integer = r.X + 2

Dim y As Integer = r.Y + 2

Dim bEnabled As Boolean = mnu.Enabled

Dim bSelected As Boolean = CBool(e.State And DrawItemState.Selected)

Dim g As Graphics = e.Graphics

rDraw = r

rDraw.Inflate(-1, -1)

g.FillRectangle(New SolidBrush(Color.FromArgb(128, SystemColors.Menu)), r)

If Not bEnabled Then

g.DrawString(strText, Me.Font, Brushes.Gray, 20, r.Y + 3)

Return

End If

If bSelected Then

brBack = Brushes.LightBlue

g.FillRectangle(Brushes.LightBlue, rDraw)

g.DrawRectangle(Pens.Blue, rDraw)

StatusBar1.Text = strText

tmrClearStatusBar.Interval = 3000 ' 3 second

tmrClearStatusBar.Enabled = True

Else

brBack = SystemBrushes.Menu

g.FillRectangle(brBack, r)

End If

g.DrawString(strText, Me.Font, Brushes.Black, 2, r.Y + 3)

br = Nothing

brBack = Nothing

rDraw = Nothing

End Sub



Private Sub tmrClearStatusBar_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles tmrClearStatusBar.Tick

StatusBar1.Text = ""

tmrClearStatusBar.Enabled = False

End Sub



Ken

-----------------------------------

Hi All,

I want to update the statusbar when mouse over on the each menu item. How
to write the code for this. Is there any specific events in menu?

Thanks and Regards
S. Viswanathan
 
H

Herfried K. Wagner [MVP]

S. Viswanathan said:
I want to update the statusbar when mouse over on the each menu item. How
to write the code for this. Is there any specific events in menu?

Add a handler to the menuitem's 'Select' event and update the statusbar
there.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

VB.NET & VB6 8
Vb.Net Reporting 1
WinForms Parameters 3
System Information 2
import 4
Accessing Sub Menu Items 5
Statusbar and menu 3
Trapping control key 2

Top