Add menu items in program

P

PetterL

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
 
L

Lloyd Sheen

PetterL said:
By the way I'm using VB 2005
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
 
L

Lloyd Sheen

PetterL said:
How do I uncheck all previous item(s) should only be one checked at time.

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
 
P

PetterL

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
 
L

Lloyd Sheen

PetterL said:
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

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
 
P

PetterL

Lloyd Sheen said:
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.
 
L

Lloyd Sheen

PetterL said:
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
 
P

PetterL

Lloyd Sheen said:
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.
 
L

Lloyd Sheen

PetterL said:
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.

Can you show us what your code looks like now? this should not be that
difficult.

LS
 
P

PetterL

Lloyd Sheen said:
Can you show us what your code looks like now? this should not be that
difficult.

LS

Here is what the windowNewMenu_click event look like now
Private Sub windowNewMenu_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) ' Handles flashToolStripMenuItem.Click

Dim ts As ToolStrip = CType(sender.parent, ToolStrip)' i have tried
sender.instance.parent also

'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

End Sub

I may not get around to check the answer for a while.

PetterL
 
L

Lloyd Sheen

PetterL said:
Here is what the windowNewMenu_click event look like now
Private Sub windowNewMenu_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) ' Handles flashToolStripMenuItem.Click

Dim ts As ToolStrip = CType(sender.parent, ToolStrip)' i have tried
sender.instance.parent also

'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

End Sub

I may not get around to check the answer for a while.

PetterL
Now have you tried to execute that code?

LS
 
P

PetterL

Lloyd Sheen said:
Can you show us what your code looks like now? this should not be that
difficult.

LS

Sorry about that i have not ansvered befor. I have been on holiday. But this
is how the code look like now



Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

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)

'Me.Controls.Add(ms)











' ''ShortNameList is an arraylist populated from a SQL table

'Dim tsmiCurrent As ToolStripMenuItem = MenuStrip1.Items(0)

'Dim cmsCurrent As New MenuStrip

'Dim mnuX As ToolStripMenuItem

'Dim ShortNameList() As String

'ShortNameList(0) = "truls"

'ShortNameList(1) = "heidi"

'ShortNameList(3) = "Petter"

'For i As Integer = 0 To 3

' mnuX = New ToolStripMenuItem(ShortNameList(i))

' AddHandler mnuX.Click, AddressOf MenuItem1_Click

' cmsCurrent.Items.Add(mnuX)

'Next

''tsmiCurrent.DropDown = cmsCurrent

End Sub



Private Sub windowNewMenu_Click(ByVal sender As Object, ByVal e As
EventArgs)

Dim instance As ToolStripItem = CType(sender, ToolStripItem)

Dim value As ToolStripItem

value = instance.OwnerItem.

MessageBox.Show(value.Text)

'Dim f As New Form()

'f.MdiParent = Me

'f.Text = "Form - " + Me.MdiChildren.Length.ToString()

'f.Show()

End Sub

End Class

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

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)

'Me.Controls.Add(ms)











' ''ShortNameList is an arraylist populated from a SQL table

'Dim tsmiCurrent As ToolStripMenuItem = MenuStrip1.Items(0)

'Dim cmsCurrent As New MenuStrip

'Dim mnuX As ToolStripMenuItem

'Dim ShortNameList() As String

'ShortNameList(0) = "truls"

'ShortNameList(1) = "heidi"

'ShortNameList(3) = "Petter"

'For i As Integer = 0 To 3

' mnuX = New ToolStripMenuItem(ShortNameList(i))

' AddHandler mnuX.Click, AddressOf MenuItem1_Click

' cmsCurrent.Items.Add(mnuX)

'Next

''tsmiCurrent.DropDown = cmsCurrent

End Sub



Private Sub windowNewMenu_Click(ByVal sender As Object, ByVal e As
EventArgs)

Dim instance As ToolStripItem = CType(sender, ToolStripItem)

Dim value As ToolStripItem

value = instance.OwnerItem.

MessageBox.Show(value.Text)

'Dim f As New Form()

'f.MdiParent = Me

'f.Text = "Form - " + Me.MdiChildren.Length.ToString()

'f.Show()

End Sub

End
 

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

Top