Programatically Uncheck ToolStripMenuItems

I have several different type of picture formats that the user can select using a check in each menuitem.   An Addhandler is used in Form1.Load to trigger the MyMenuClick subroutine.   Thus far, however, the code hangs in the CheckMenuItem function on the "m.checked = checked" line.   For some reason, it is challenging to uncheck and check toolstripmenuitems -- so am I leaving out a CType (cast) or do I need a sender.sourcecontrol?

Private Function checkMenuItem(ByRef m As ToolStripMenuItem, ByVal checked As Boolean)
If m IsNot Nothing Then
     m.Checked = checked   'hang occurs on this line
     Return True
     Return False
End If
End Function

Private Sub MyMenuClick(sender As Object, e As EventArgs)
        checkMenuItem(WmfToolStripMenuItem, False)
        checkMenuItem(JpgToolStripMenuItem, False)
        checkMenuItem(GifToolStripMenuItem, False)
        checkMenuItem(TifToolStripMenuItem, False)
        checkMenuItem(BmpToolStripMenuItem, False)
End Sub

Open in new window

Who is Participating?
Jacques Bourgeois (James Burger)Connect With a Mentor PresidentCommented:
Works very well here.

Is it possible that it hangs in the CheckedChanged or the CheckStateChanged that are triggered when you change the check.

And a little note, you are making one of the most common mistakes I see in VB. ByRef is not necessary here. ByRef needs to be used only for Value objects (structures and basic types). When you pass an object that is created on a class, the variable contains a pointer to the object in memory, ByVal makes a copy of that pointer, the routine thus works on the same pointer, so on the same object.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.