[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 942
  • Last Modified:

MeasureItem and DrawItem not working

Hi,

I am building the contents of a context menu during runtime.  I am trying to modify the appearance of certain menu items, i.e. change the background colour of the item to yellow or green, depending on certain conditions.

The code for these items looks like this:

 Private WithEvents mMenuX As MenuItem

 Public Sub populateMenu(ByVal pArraylist As ArrayList)
        Dim i As Integer

        For i = 0 To pArraylist.Count - 1
            mMenuX = New MenuItem
            mMenuX.OwnerDraw = True
            AddHandler mMenuX.MeasureItem, New MeasureItemEventHandler(AddressOf Me.TaskItems_MeasureItem)
            AddHandler mMenuX.DrawItem, New DrawItemEventHandler(AddressOf Me.TaskItems_DrawItem)
            mMenuX.Text = Trim(pArraylist.Item(i))
            mMenuTasks.MenuItems.Add(mMenuX.Text, New System.EventHandler(AddressOf Me.TaskItems_Click))
        Next
    End Sub

Private Sub TaskItems_MeasureItem(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.MeasureItemEventArgs)

        Dim mnuItem As MenuItem = CType(sender, MenuItem)
        Dim MenuFont As New Font("Arial", 12)
        e.ItemHeight = e.Graphics.MeasureString(mnuItem.Text, MenuFont).Height + 25
        e.ItemWidth = e.Graphics.MeasureString(mnuItem.Text, MenuFont).Width + 130
    End Sub

    Private Sub TaskItems_DrawItem(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.DrawItemEventArgs)

        ' Cast the sender to MenuItem so you can access text property.
        Dim customItem As MenuItem = CType(sender, MenuItem)
        ' Create a Brush and a Font to draw the MenuItem.
        Dim aBrush As System.Drawing.Brush = System.Drawing.Brushes.DarkMagenta
        Dim aFont As New Font("Garamond", 10, FontStyle.Italic, _
            GraphicsUnit.Point)
        ' Get the size of the text to use later to draw an ellipse
        ' around the item.
        Dim stringSize As SizeF = e.Graphics.MeasureString( _
            customItem.Text, aFont)
        ' Draw the item and then draw the ellipse.
        e.Graphics.DrawString(customItem.Text, aFont, _
            aBrush, e.Bounds.X, e.Bounds.Y)
        e.Graphics.DrawEllipse(New Pen(System.Drawing.Color.Black, 2), _
            New Rectangle(e.Bounds.X, e.Bounds.Y, stringSize.Width, _
            stringSize.Height))

    End Sub

My problem is that the MeasureItem and DrawItem events are not being executed.  Can anyone tell why this could be.

Thanks

Pix
0
Pixie
Asked:
Pixie
  • 7
  • 5
1 Solution
 
RonaldBiemansCommented:
Have you set the .ownerdraw property  of the contextmenus  to true ?
0
 
RonaldBiemansCommented:
sorry I meant

Have you set the .ownerdraw property  of the contextmenuItems  to true ?
0
 
RonaldBiemansCommented:
Yes, you have. ;-)
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
RonaldBiemansCommented:
I think there is a bug there somehow the ownerdraw isn't set to thrue when you create it , you can solve it like this

Public Sub populateMenu(ByVal pArraylist As ArrayList)
        Dim i As Integer

        For i = 0 To pArraylist.Count - 1
            mMenuX = New MenuItem
            Mmenux.Text = Trim(pArraylist.Item(i))
            mmenutasks.MenuItems.Add(Mmenux.Text, New System.EventHandler(AddressOf Me.TaskItems_Click))

        Next
        For Each x As MenuItem In mmenutasks.MenuItems
            x.OwnerDraw = True
            AddHandler x.MeasureItem, New MeasureItemEventHandler(AddressOf Me.TaskItems_MeasureItem)
            AddHandler x.DrawItem, New DrawItemEventHandler(AddressOf Me.TaskItems_DrawItem)
        Next
    End Sub

0
 
PixieAuthor Commented:
Hi,

I changed code, now the menu items don't appear at all???

Thanks

Pix
0
 
RonaldBiemansCommented:
That is strange because it works fine here?  Nice menu by the way ;-)

Could you post the code you have know so I can see if there is something else wrong
0
 
PixieAuthor Commented:
Here's the whole class just in case it's something else funny.  The code for the DrawItem comes from an example I found (notice all the comments).  I have no clue what it looks like because I can't get it working.  But thanks.

Imports System.Windows.Forms.ContextMenu
Imports System.Windows.Forms
Imports System
Imports System.Drawing
Imports System.Resources

Public Class clsContextMenu
    Private WithEvents mNotify As New NotifyIcon
    Private mContextMenu As New ContextMenu
    Private mMenuNew As New MenuItem
    Private WithEvents mMenuLogon As New MenuItem
    Private mMenuDash1 As New MenuItem
    Private mMenuDash2 As New MenuItem
    Private WithEvents mMenuTasks As New MenuItem
    Private WithEvents mMenuTask As New MenuItem
    Private mMenuDash3 As New MenuItem
    Private WithEvents mMenuProject As New MenuItem
    Private mMenuStop As New MenuItem
    Private WithEvents mMenuX As MenuItem

    Public Sub createMenus()

        mNotify.ContextMenu = mContextMenu
        mNotify.Icon = New Icon("C:\tmp\Icons\TIMER01.ico")
        mNotify.Text = "Task Monitoring"
        mNotify.Visible = True

        mMenuLogon.Index = 0
        mMenuLogon.Text = "Logon"

        mMenuDash1.Index = 1
        mMenuDash1.Text = "-"

        mMenuNew.Index = 2
        mMenuNew.Text = "New"

        mMenuDash2.Index = 3
        mMenuDash2.Text = "-"

        mMenuTasks.Index = 4
        mMenuTasks.Text = "Tasks"

        mMenuDash3.Index = 5
        mMenuDash3.Text = "-"

        mMenuStop.Text = "Stop"

        mMenuNew.MenuItems.Add(mMenuProject)
        mMenuNew.MenuItems.Add(mMenuTask)

        mMenuNew.Enabled = False
        mMenuTasks.Enabled = False

        mContextMenu.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {mMenuLogon, _
        mMenuDash1, mMenuNew, mMenuDash2, mMenuTasks, mMenuDash3, mMenuProject, mMenuTask, mMenuStop})

    End Sub

    Private Sub mMenuLogon_Click(ByVal Sender As Object, ByVal e As EventArgs) _
    Handles mMenuLogon.Click

        Dim FrmLogon As New frmLogin

        FrmLogon.ShowDialog()

        mMenuNew.Enabled = True
        mMenuTasks.Enabled = True

    End Sub

    Private Sub mMenuProject_Click(ByVal Sender As Object, ByVal e As EventArgs) _
    Handles mMenuProject.Click

        Dim FrmNewProject As New frmTask

        FrmNewProject.Show()

    End Sub

    Private Sub mMenuTask_Click(ByVal Sender As Object, ByVal e As EventArgs) _
    Handles mMenuTask.Click

        Dim FrmNewTask As New FrmNewTask

        FrmNewTask.Show()

    End Sub

    Public Sub populateMenu(ByVal pArraylist As ArrayList)
        Dim i As Integer

        For i = 0 To pArraylist.Count - 1
            mMenuX = New MenuItem
            mMenuX.Text = Trim(pArraylist.Item(i))
            mMenuTasks.MenuItems.Add(mMenuX.Text, New System.EventHandler(AddressOf Me.TaskItems_Click))

        Next
        For Each x As MenuItem In mMenuTasks.MenuItems
            x.OwnerDraw = True
            AddHandler x.MeasureItem, New MeasureItemEventHandler(AddressOf Me.TaskItems_MeasureItem)
            AddHandler x.DrawItem, New DrawItemEventHandler(AddressOf Me.TaskItems_DrawItem)
        Next

    End Sub

    Private Sub TaskItems_Click(ByVal Sender As Object, ByVal e As EventArgs)
        Dim i As Integer


        mMenuX = mMenuTasks.MenuItems(Sender.index)

        If mMenuX.Checked = True Then
            clsTask.selectTaskName(mMenuX.Text)
            mMenuX.Checked = False
        Else
            If mMenuX.Checked = False Then
                mMenuX.Checked = True
            End If
        End If



    End Sub
    Private Sub TaskItems_MeasureItem(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.MeasureItemEventArgs)

        Dim mnuItem As MenuItem = CType(sender, MenuItem)
        Dim MenuFont As New Font("Arial", 12)
        e.ItemHeight = e.Graphics.MeasureString(mnuItem.Text, MenuFont).Height + 25
        e.ItemWidth = e.Graphics.MeasureString(mnuItem.Text, MenuFont).Width + 130
    End Sub

    Private Sub TaskItems_DrawItem(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.DrawItemEventArgs)

        ' Cast the sender to MenuItem so you can access text property.
        Dim customItem As MenuItem = CType(sender, MenuItem)
        ' Create a Brush and a Font to draw the MenuItem.
        Dim aBrush As System.Drawing.Brush = System.Drawing.Brushes.DarkMagenta
        Dim aFont As New Font("Garamond", 10, FontStyle.Italic, _
            GraphicsUnit.Point)
        ' Get the size of the text to use later to draw an ellipse
        ' around the item.
        Dim stringSize As SizeF = e.Graphics.MeasureString( _
            customItem.Text, aFont)
        ' Draw the item and then draw the ellipse.
        e.Graphics.DrawString(customItem.Text, aFont, _
            aBrush, e.Bounds.X, e.Bounds.Y)
        e.Graphics.DrawEllipse(New Pen(System.Drawing.Color.Black, 2), _
            New Rectangle(e.Bounds.X, e.Bounds.Y, stringSize.Width, _
            stringSize.Height))

    End Sub


End Class

Thanks a stack
Pix
0
 
PixieAuthor Commented:
Hi,
I have just included Imports System.Windows.Forms.Form to the class and now a I get a blank list of menu items.
Pix
0
 
RonaldBiemansCommented:
Aha, a notifyicon, Sorry this is a know bug sorry and cannot be solved there is no workaround

look here

http://support.microsoft.com/?kbid=827043
0
 
PixieAuthor Commented:
Thanks, that clears that up for me.  I have been battling with this the whole day and was starting to get really frustrated.  Do you have any suggestions to alternatives for what I am trying to achieve?  I looked into DevExpress controls, but their help is really very unhelpful.  They have components called PopupMenus with BarItems...

Oh well, thanks again

Pix
0
 
RonaldBiemansCommented:
Well can't you get around using the notifyicon, because if you don't use that then there is no problem.
0
 
PixieAuthor Commented:
Thanks again
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now