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)
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
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.