I am able to have checked menu items show the variables that I need to show, but they don't uncheck themselves unless I go back and click them. I want to have only one checkmark on a country at a time...is there any way? I've tried if...elseif , but that doesn't work....example below (If I only want Mexico checked and NOT United States, but both are checked still).

If MexicoToolStripMenuItem.CheckState = CheckState.Checked Then
   UnitedStatesToolStripMenuItem.CheckState=CheckState.Unchecked...
End IF

Dani AI

Generated

Brief summary: the goal is a mutually-exclusive check for the country items (only one checked at a time). reported that checked items stay checked until clicked again; suggested pairwise toggling and showed an explicit "uncheck all except sender" function. For a cleaner, scalable solution, attach one handler to the whole country group and let it find the clicked item's siblings and uncheck them, then check the clicked item. This avoids pairwise logic and adding a handler per item by hand.

Example single-handler implementation (attach this handler to every country item):

Private Sub Country_Click(sender As Object, e As EventArgs) Handles mnuUS.Click, mnuMexico.Click, mnuUK.Click, mnuNewZealand.Click, mnuSouthAfrica.Click
    Dim clicked As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem)
    Dim parent As ToolStripDropDownItem = TryCast(clicked.OwnerItem, ToolStripDropDownItem)
    If parent IsNot Nothing Then
        For Each it As ToolStripItem In parent.DropDownItems
            Dim mi = TryCast(it, ToolStripMenuItem)
            If mi IsNot Nothing Then mi.Checked = False
        Next
    End If
    clicked.Checked = True
End Sub

If menu items are created dynamically, hook them at creation time instead of using a long Handles clause:

For Each it As ToolStripItem In mnuCountries.DropDownItems
    Dim mi = TryCast(it, ToolStripMenuItem)
    If mi IsNot Nothing Then AddHandler mi.Click, AddressOf Country_Click
Next

Notes and troubleshooting: set each country item to use the check UI (CheckOnClick or RadioCheck for a radio-style glyph) so the control shows checked state. Skip non-ToolStripMenuItem entries (separators) when iterating. If a behavior that allows "no selection" is required, change the logic to respect the toggled state instead of always forcing clicked.Checked = True. This approach scales well and keeps the selection logic centralized compared with pairwise toggles or many tiny handlers.

Recommended Answers

All 2 Replies

In the properties window make sure CheckedOnClick equals true for each of the menu items.

Add to your menu item events:

mnuUnitedStates_Click
mnuMexico.Checked = Not mnuUnitedStates.Checked
End Sub

mnuMexico_Click
mnuUnitedStates.Checked = Not mnuMexico.Checked
End Sub

Make sure that all your Country DropDownItems CheckOnClick is True

' M A I N   M E N U   S T R I P
    ' File (mnuFile)
    ' ' ' Exit (mnuExit)
    ' Country (mnuCountries)
    ' ' ' South Africa
    ' ' ' Afghanistan
    ' ' ' New Zealand
    ' ' ' United Kingdom
    ' ' ' United States

    ''' <summary>
    ''' This function will uncheck all the mnuCountries DropDownItems, except for the sender
    ''' </summary>
    ''' <param name="sender">This will be a ToolStripMenuItem</param>
    Sub ChangeMenuCheckState(ByVal sender As Object)
        Dim mnuItem As ToolStripMenuItem = sender
        For Each item As ToolStripMenuItem In mnuCountries.DropDownItems
            If item.Name <> mnuItem.Name Then
                item.CheckState = CheckState.Unchecked
            End If
        Next
    End Sub
    Private Sub mnuSouthAfrica_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuSouthAfrica.Click
        ChangeMenuCheckState(sender)
    End Sub
    Private Sub mnuAfghanistan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuAfghanistan.Click
        ChangeMenuCheckState(sender)
    End Sub
    Private Sub mnuNewZealand_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuNewZealand.Click
        ChangeMenuCheckState(sender)
    End Sub
    Private Sub mnuUK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuUK.Click
        ChangeMenuCheckState(sender)
    End Sub
    Private Sub mnuUS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuUS.Click
        ChangeMenuCheckState(sender)
    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.