|
-
Apr 14th, 2011, 04:26 PM
#1
Thread Starter
Fanatic Member
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:
Private Sub mnuMainMenuToolBarComposeExistingThread_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles mnuMainMenuToolBarPrivateMessage.Click, mnuMainMenuToolBarComposeExistingThread.Click, _ mnuMainMenuToolBarComposeNewThread.Click For Each ctrl As ToolStripMenuItem In mnuMainMenuToolBar.Items If Not ctrl.Name = DirectCast(sender, ToolStripMenuItem).Name Then CType(ctrl, ToolStripMenuItem).Checked = False End If Next ctrl End Sub
(edit just realized this rewritten version is not even working)
-
Apr 14th, 2011, 05:49 PM
#2
Thread Starter
Fanatic Member
Re: Toggling MenuItems (ToolStripMenuItem) checked state
Not sure if this is any neater
vb Code:
Dim name As String = DirectCast(sender, ToolStripMenuItem).Name Dim input() As ToolStripMenuItem = {mnuMainMenuToolBarPrivateMessage, _ mnuMainMenuToolBarComposeExistingThread, _ mnuMainMenuToolBarComposeNewThread} For Each i In input If Not i.Name = name Then i.Checked = False End If Next
-
Apr 14th, 2011, 07:14 PM
#3
Re: Toggling MenuItems (ToolStripMenuItem) checked state
vb.net Code:
Private Sub ToolStripMenuItems_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles Item3ToolStripMenuItem.CheckedChanged, Item2ToolStripMenuItem.CheckedChanged, Item1ToolStripMenuItem.CheckedChanged Dim currentItem = DirectCast(sender, ToolStripMenuItem) Dim parentItem = DirectCast(currentItem.OwnerItem, ToolStripMenuItem) If currentItem.Checked Then For Each sibling As ToolStripMenuItem In parentItem.DropDownItems If sibling IsNot currentItem Then sibling.Checked = False End If Next End If End Sub
This assumes that CheckOnClick is True for each item. It also assumes that the parent contains nothing but menu items.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|