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
Else
     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

lep1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jacques Bourgeois (James Burger)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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.