OwnerDrawMenuItem .Text Property

Docking/MDI for Windows Forms Forum

Posted 4 years ago by EntityDev
Version: 14.1.0323
Avatar

Where does the OwnerDrawMenuItem get the value for its .Text property?

In the sample application, the main form code-behind handles DockManager.WindowCreated. When a ToolWindow is created, an item is added to the View menu. This is standard-issue behavior for a tabbed mdi.

However, when the menu item is created...

'Add the View menu item for the tool window
Dim menuItem As New OwnerDrawMenuItem(toolWindow.Text, New EventHandler(AddressOf Me.viewToolWindowMenuItem_Click))
menuItem.Tag = toolWindow
viewMenuItem.MenuItems.Add(menuItem)

 ...the value for toolWindow.Text is an empty String. Yet at runtime, the menu item does contain the value of the tool window's .Text Property - which is actually set after the above event handler is entered.

I assume that a protected event on the OwnerDrawMenuItem is querying the .Tag property for a ToolWindow and getting its .Text from there.

However, I'm using Microsoft's menu objects. On DockManager.WindowCreated, I'm creating a new System.Windows.Forms.ToolStripMenuItem...

'Add the View menu item for the tool window
Dim item As New ToolStripMenuItem(window.Text, Nothing, New EventHandler(AddressOf ViewToolWindowMenuItem_Click))
item.Tag = window
ViewToolStripMenuItem.DropDownItems.Add(item)

 At runtime, the DropDownItem appears to have an empty string for its .Text property.

 

Thanks for any help.

[Modified 4 years ago]

Comments (1)

Answer - Posted 4 years ago by EntityDev
Avatar

Please disregard. I found that it's set in the OwnerDrawMenuItem.Popup event handler.

Yep, it casts the .Tag as a ToolWindow, gets its .Text property and applies it. Never mind :D

 

I note that the methodology employed is pretty clunky. I'd recommend nowadays doing something like this:

    Private Sub ViewToolStripMenuItem_DropDownOpening(sender As Object, e As System.EventArgs) Handles ViewToolStripMenuItem.DropDownOpening
        For Each item As ToolStripItem In ViewToolStripMenuItem.DropDownItems
            If Not IsNothing(item.Tag) AndAlso TypeOf item.Tag Is ToolWindow Then
                item.Text = CType(item.Tag, ToolWindow).Text
            End If
        Next
    End Sub

 Whether using ToolStripMenuItems or OwnerDrawMenuItems for your menu, the methodology is the same and the code for each is similar. Of course for OwnerDrawMenuItem, you'd handle the Popup event instead.

[Modified 4 years ago]

The latest build of this product (v2020.1 build 0400) was released 6 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.