none
problem with menustrips in vs 2005

    Question

  • Hi,

     i'm new to using visual studio2005. in my application i'm using menu strips and there i have added one toolstripmenu item and one toolstrip separator. and I also added some toolstrip menu items dynamically.

    the problem is when i want to access those menuitems using the for each loop i'm getting an exception that unable to cast from the tool strip menu item to tool stripseparator.

     I'll paste the part of the code here to understand my problem clrear.

    regards

    GRK

    code:

    Dim item As New ToolStripMenuItem(dev.Identity.ProductName, Nothing, New EventHandler(AddressOf OnDeviceClick))

    Me.  ChooseDeviceToolStripMenuItem.DropDownItems.Add(item)

    Next

    **************************************************************************

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

    Try

    If TypeOf sender Is ToolStripMenuItem Then

    ' Make sure it's closed.

    Dim item As ToolStripMenuItem

    For Each item In ChooseDeviceToolStripMenuItem.DropDownItems

    item.Checked = False

    Next

    item = CType(sender, ToolStripMenuItem)

    item.Checked = True

    End If

    Catch ex As Exception

    MessageBox.Show(ex.Message, SD_Ex_Caption)

    End Try

    Tuesday, February 06, 2007 9:50 AM

Answers

  • "Thanks for the information."  It must have worked for somebody...

    ToolStripMenuItems have a Checked property (otherwise you wouldn't be messing with them!), so the code should work.  Maybe after verifying that it's not a separator, cast the item as a ToolStripMenuItem and then try to set its Checked property to False.

    You're only allowing one selection at a time, so I'd scrap the menu items and make it a combo box instead.  They're a lot easier to deal with, and they can be built into ToolStrips as well.  Pre-load "Select a printer" as the top option, and dynamically add printer names to the item list.  Use SelectedIndex (-1 if you use the "Select a printer" on top) to refer back to your device list to get details on the device, or use the Text property if you just need the printer name.

    Wednesday, February 14, 2007 5:11 PM

All replies

  • GRK,

    ToolStripSeparator Class  represents a line used to group items of a ToolStrip or the drop-down items of a MenuStrip or ContextMenuStrip or other ToolStripDropDown control. According to your description, I guess you are using the ToolStripSeparator to group the related items in ToolStripDropDown control.

    There are so many properties of ToolStripSeparator class, nearly all of them are inherited from ToolStripDropDown. Please take a look at the following link on these properties:

    http://msdn2.microsoft.com/en-us/library/system.windows.forms.toolstripseparator_properties(VS.80).aspx

    The following code use the AddRange or Add method to add the menu commands and ToolStripSeparator controls to the MenuStrip in the order you want.

        ' This code adds a top-level File menu to the MenuStrip.

        Me.menuStrip1.Items.Add(New ToolStripMenuItem() _

        {Me.fileToolStripMenuItem})

     

        ' This code adds the New and Open menu commands, a separator bar,

        ' and the Save and Exit menu commands to the top-level File menu,

        ' in that order.

        Me.fileToolStripMenuItem.DropDownItems.AddRange(New _

        ToolStripMenuItem() {Me.newToolStripMenuItem, _

        Me.openToolStripMenuItem, Me.toolStripSeparator1, _

        Me.saveToolStripMenuItem, Me.exitToolStripMenuItem})

    By the way, what is the ChooseDeviceToolStripMenuItem? Actually I don't know its meaning exactly from your code. Please give me some more detailed information if you still have problem with this question.

    Wednesday, February 07, 2007 6:33 AM
  • hi

              Thanks for your reply actually the  chooseDeviceToolStripMenuItem is  toolstripmenuItem which again has a collection. the collection is to add the installed printers to the menu item dynamically at run time.in the  ChooseDeviceToolstipMenuItem collection the top menu item  and a toolstrip separator are added at design time, and the printer name are added dynamically. so while accessing each of the toolstrip menu item with a foreach a loop the error occuring with the toolstrip separator.

    Hope i conveyed my problem correctly.

    regards

    GRK

    Friday, February 09, 2007 1:23 PM
  • You must not assume that all the items in your drop-down menu is a ToolStripMenuItem.  You need to move up a level of inheritance to ToolStripItem, a parent class of both ToolStripMenuItem and ToolStripSeparator.  Then you can test for the item being an MenuItem before trying to set its Checked property.  Thus, the loop would look something like:

    Dim item As ToolStripItem

    For Each item In ChooseDeviceToolStripMenuItem.DropDownItems

      if item.GetType() = ToolStripMenuItem Then

        item.Checked = False

      end if

    Next

    Friday, February 09, 2007 2:02 PM
  • Hi,

      thanks for you reply.I tried the above code also but there Item don't have checked property.so it is giving me error.

    -regards

    GRK

    Saturday, February 10, 2007 10:14 AM
  •  

    Gracias por la información.

    Atte.

     

    Mario Torres

     

    Monday, February 12, 2007 10:08 PM
  • Oh god what does it mean.
    Wednesday, February 14, 2007 4:32 AM
  • "Thanks for the information."  It must have worked for somebody...

    ToolStripMenuItems have a Checked property (otherwise you wouldn't be messing with them!), so the code should work.  Maybe after verifying that it's not a separator, cast the item as a ToolStripMenuItem and then try to set its Checked property to False.

    You're only allowing one selection at a time, so I'd scrap the menu items and make it a combo box instead.  They're a lot easier to deal with, and they can be built into ToolStrips as well.  Pre-load "Select a printer" as the top option, and dynamically add printer names to the item list.  Use SelectedIndex (-1 if you use the "Select a printer" on top) to refer back to your device list to get details on the device, or use the Text property if you just need the printer name.

    Wednesday, February 14, 2007 5:11 PM
  • Hi james,

        now I removed that toolstripseparator and now its working fine. thanks for your suggestions

    -regards

    GRK

    Thursday, February 15, 2007 5:27 AM