Re: Add menu items in program by PetterL
PetterL
Thu Jul 31 16:43:25 CDT 2008
"Lloyd Sheen" <a@b.c> wrote in message
news:%23YifZuD8IHA.3624@TK2MSFTNGP05.phx.gbl...
> PetterL wrote:
>> "Lloyd Sheen" <a@b.c> wrote in message
>> news:%23GGmRrd7IHA.2224@TK2MSFTNGP05.phx.gbl...
>>> PetterL wrote:
>>>> Sorry but i think i'm a bit lost here
>>>>
>>>> I used this code to create a few test menu items on a button
>>>> Dim ms As New MenuStrip()
>>>>
>>>> Dim windowMenu As ToolStripMenuItem = MenuStrip1.Items(1) '("Window")
>>>>
>>>> Dim windowNewMenu As New ToolStripMenuItem("New", Nothing, New
>>>> EventHandler(AddressOf windowNewMenu_Click))
>>>>
>>>> windowMenu.DropDownItems.Add(windowNewMenu)
>>>>
>>>> CType(windowMenu.DropDown, ToolStripDropDownMenu).ShowImageMargin =
>>>> False
>>>>
>>>> CType(windowMenu.DropDown, ToolStripDropDownMenu).ShowCheckMargin =
>>>> True
>>>>
>>>> ' Assign the ToolStripMenuItem that displays
>>>>
>>>> ' the list of child forms.
>>>>
>>>> ' ms.MdiWindowListItem = windowMenu
>>>>
>>>> MenuStrip1.MdiWindowListItem = windowMenu
>>>>
>>>> ' Add the window ToolStripMenuItem to the MenuStrip.
>>>>
>>>> 'ms.Items.Add(windowMenu)
>>>>
>>>> ' MenuStrip1.Items.Add(windowMenu)
>>>>
>>>> Me.Controls.Add(MenuStrip1)
>>>>
>>>> And then using following in the click event
>>>>
>>>>
>>>>
>>>> Private Sub windowNewMenu_Click(ByVal sender As Object, ByVal e As
>>>> EventArgs)
>>>>
>>>> Dim instance As ToolStripDropDown = CType(sender, ToolStripDropDown)
>>>>
>>>> Dim value As ToolStripItem
>>>>
>>>> value = instance.OwnerItem
>>>>
>>>> MessageBox.Show(value.Text)
>>>>
>>>> end sub
>>>>
>>>> I got an error message and i have no clue :-)
>>>>
>>>> The error is "System.InvalidCastException was unhandled
>>>> I'm a bit new to the menu in VB2005
>>>>
>>>> PetterL
>>>>
>>>> Message="Unable to cast object of type
>>>> 'System.Windows.Forms.ToolStripMenuItem' to type "Lloyd Sheen" <a@b.c>
>>>> wrote in message news:u2eST%23Q7IHA.3976@TK2MSFTNGP06.phx.gbl...
>>>>> PetterL wrote:
>>>>>> How do I uncheck all previous item(s) should only be one checked at
>>>>>> time.
>>>>>>
>>>>>> "Lloyd Sheen" <a@b.c> wrote in message
>>>>>> news:uYjwvvD7IHA.3480@TK2MSFTNGP03.phx.gbl...
>>>>>>> PetterL wrote:
>>>>>>>> By the way I'm using VB 2005
>>>>>>>>
>>>>>>>> "PetterL" <petterathome@hotmail.com> wrote in message
>>>>>>>> news:%23rtlB6C7IHA.2260@TK2MSFTNGP03.phx.gbl...
>>>>>>>>> I writing a program where I read menu items from a file. But I
>>>>>>>>> have problem when I click an menu item i want it to mark that one
>>>>>>>>> as checked but I cant access the menu object of that item to see
>>>>>>>>> witch object was selected.
>>>>>>>>>
>>>>>>>>> This is the sub procedure that read the items from an Array and
>>>>>>>>> create the menutitem. The array hold more information to use in
>>>>>>>>> program when one of the items is selected.
>>>>>>>>>
>>>>>>>>> Dim finisharray(,) As String ' is set up under the form sub.
>>>>>>>>>
>>>>>>>>> Dim FilRow() As String
>>>>>>>>>
>>>>>>>>> Dim n As Integer
>>>>>>>>>
>>>>>>>>> For n = 0 To 5
>>>>>>>>>
>>>>>>>>> FilRow = Split(finisharray(n, 1), "=")
>>>>>>>>>
>>>>>>>>> If FilRow(0) = "" Then Exit For
>>>>>>>>>
>>>>>>>>> Dim windowMenu As ToolStripMenuItem = MenuStrip1.Items(1)
>>>>>>>>>
>>>>>>>>> Dim windowNewMenu As New ToolStripMenuItem(Trim(FilRow(1)),
>>>>>>>>> Nothing, New EventHandler(AddressOf windowNewMenu_Click))
>>>>>>>>>
>>>>>>>>> windowMenu.DropDownItems.Add(windowNewMenu)
>>>>>>>>>
>>>>>>>>> CType(windowMenu.DropDown, ToolStripDropDownMenu).ShowImageMargin
>>>>>>>>> = False
>>>>>>>>>
>>>>>>>>> CType(windowMenu.DropDown, ToolStripDropDownMenu).ShowCheckMargin
>>>>>>>>> = True
>>>>>>>>>
>>>>>>>>> If My.Settings.SelFlash <> "" Then
>>>>>>>>>
>>>>>>>>> If My.Settings.SelFlash = LCase(Trim(FilRow(1))) Then
>>>>>>>>>
>>>>>>>>> windowNewMenu.Checked = True
>>>>>>>>>
>>>>>>>>> End If
>>>>>>>>>
>>>>>>>>> Else
>>>>>>>>>
>>>>>>>>> My.Settings.SelFlash = LCase(Trim(FilRow(1)))
>>>>>>>>>
>>>>>>>>> windowNewMenu.Checked = True
>>>>>>>>>
>>>>>>>>> End If
>>>>>>>>>
>>>>>>>>> Next
>>>>>>>>>
>>>>>>>>>
>>>>>>> In windowNewMenu_Click the sender parameter should be the
>>>>>>> ToolStripMenuItem that was clicked. You should be able to set the
>>>>>>> checked property or toggle it.
>>>>>>>
>>>>>>> Something like:
>>>>>>>
>>>>>>> public sub windowNewMenu_Click(sender as object, e As
>>>>>>> System.EventArgs)
>>>>>>> dim tsmi as ToolStripMenuItem=ctype(sender,ToolStripMenuItem)
>>>>>>> tsmi.checked=true
>>>>>>> end sub
>>>>>>>
>>>>>>> LS
>>>>> That depends on the application. If only one item can be checked then
>>>>> yes. If the menu items are simply toggle switches (like Always on
>>>>> Top) then no.
>>>>>
>>>>> To get the other items when one is clicked get the parent item and
>>>>> then iterate thru it's children.
>>>>>
>>>>> LS
>>>>
>>> First Peter please don't top post, it makes reading the thread
>>> difficult.
>>>
>>> You are adding items with the following line:
>>> Dim windowNewMenu As New ToolStripMenuItem
>>>
>>> In the event handler you are doing:
>>> Dim instance As ToolStripDropDown = CType(sender, ToolStripDropDown)
>>>
>>> I think if you check the sender should be ToolStripMenuItem. This is
>>> causing your problem.
>>>
>>> LS
>>
>> Sorry about the top posting. Have been out of newsgroops for a long time
>> :-)
>>
>> Yes that was the problem, but i'm stil lost when it comes to read and
>> walk trough the owner items list to reset those to False. It is the same
>> code. That was what i tried to do when the error above happend.
>
> In the event handler the sender as you have seen is a ToolStripMenuItem.
> One of the properties of this object is Parent which is a ToolStrip.
>
> So you would do:
>
> Dim ts as Toolstrip = ctype(instance.parent,Toolstrip)
>
> Now you can do the following:
>
> For each tsi as object in ts.items
> ' now here you will have to check the type of the object
> ' if it is not a ToolStripMenuItem ignore it (like a ToolStripSeparator)
>
> ' if it is a ToolStripMenuItem
> ctype(tsi,ToolStripMenuItem).checked = false
> next
>
> Now set the sender (instance) to checked.
>
> Hope this helps
> LS
Hi LS
When i Copy and paste this code into the
windowNewMenu_Click event, I cant just use Instance.parent so i changed it
to Sender.Instance.parent but get the folliwing error "Public member
'instacne' on type 'ToolStripMenuItem' not found."
I tried just sender.parent and getting "Public member 'parent' on type
'ToolStripMenuItem' not found."
Sorry that i can't be mutch help. I haven't done mutch with the sender
object in events before.
There is still VB 2005 I'm using.