Results 1 to 3 of 3

Thread: Toggling MenuItems (ToolStripMenuItem) checked state

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    547

    Toggling MenuItems (ToolStripMenuItem) checked state

    I have a menu bar like this.

    Menu
    .Item1
    .Item2
    .Item3

    I need to use them like radio buttons. So i have come up with this. Seems a nasty hack since it loops all my menu items. And if i later add different checks it would interfear.


    Any better solutions?

    vb Code:
    1. Private Sub mnuMainMenuToolBarComposeExistingThread_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    2.         Handles mnuMainMenuToolBarPrivateMessage.Click, mnuMainMenuToolBarComposeExistingThread.Click, _
    3.         mnuMainMenuToolBarComposeNewThread.Click
    4.  
    5.         For Each ctrl As ToolStripMenuItem In mnuMainMenuToolBar.Items
    6.             If Not ctrl.Name = DirectCast(sender, ToolStripMenuItem).Name Then
    7.                 CType(ctrl, ToolStripMenuItem).Checked = False
    8.             End If
    9.         Next ctrl
    10.  
    11.  
    12.     End Sub

    (edit just realized this rewritten version is not even working)

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    547

    Re: Toggling MenuItems (ToolStripMenuItem) checked state

    Not sure if this is any neater

    vb Code:
    1. Dim name As String = DirectCast(sender, ToolStripMenuItem).Name
    2.  
    3.         Dim input() As ToolStripMenuItem = {mnuMainMenuToolBarPrivateMessage, _
    4.                                     mnuMainMenuToolBarComposeExistingThread, _
    5.                                     mnuMainMenuToolBarComposeNewThread}
    6.  
    7.         For Each i In input
    8.             If Not i.Name = name Then
    9.                 i.Checked = False
    10.             End If
    11.         Next

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Toggling MenuItems (ToolStripMenuItem) checked state

    vb.net Code:
    1. Private Sub ToolStripMenuItems_CheckedChanged(sender As System.Object,
    2.                                               e As System.EventArgs) Handles Item3ToolStripMenuItem.CheckedChanged,
    3.                                                                              Item2ToolStripMenuItem.CheckedChanged,
    4.                                                                              Item1ToolStripMenuItem.CheckedChanged
    5.     Dim currentItem = DirectCast(sender, ToolStripMenuItem)
    6.     Dim parentItem = DirectCast(currentItem.OwnerItem, ToolStripMenuItem)
    7.  
    8.     If currentItem.Checked Then
    9.         For Each sibling As ToolStripMenuItem In parentItem.DropDownItems
    10.             If sibling IsNot currentItem Then
    11.                 sibling.Checked = False
    12.             End If
    13.         Next
    14.     End If
    15. End Sub
    This assumes that CheckOnClick is True for each item. It also assumes that the parent contains nothing but menu items.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width