ContextMenuStrip not passed with Form?

G

ginacresse

Hi,
I have a form (Form1) with a ContextMenuStrip (ContextMenuStrip1). If I
pass Form1 to a Utility Sub with the following code:

Public Sub Utility(theForm as Object)
with theForm
For Each MenuItem As ToolStripMenuItem In .ContextMenuStrip1.Items
If LCase(Strings.Left(MenuItem.Name, 8)) = LCase("tsmAudit") Then
MenuItem.Enabled = False
End If
Next
End With
End Sub

I get an error the ContextMenuStrip1 does not exist on Form1 -- but it does.
If I run this code in Form1 it works perfectly. Does anyone know why this
doesn't work and how I can accomplish what I'm trying to do?

Thanks,
 
K

kimiraikkonen

Hi,
I have a form (Form1) with a ContextMenuStrip (ContextMenuStrip1). If I
pass Form1 to a Utility Sub with the following code:

Public Sub Utility(theForm as Object)
with theForm
For Each MenuItem As ToolStripMenuItem In .ContextMenuStrip1.Items
If LCase(Strings.Left(MenuItem.Name, 8)) = LCase("tsmAudit") Then
MenuItem.Enabled = False
End If
Next
End With
End Sub

I get an error the ContextMenuStrip1 does not exist on Form1 -- but it does.
If I run this code in Form1 it works perfectly. Does anyone know why this
doesn't work and how I can accomplish what I'm trying to do?

Thanks,

I think you made a typo, remove the "dot" before ContextMenuStrip1
which exists in the code you posted, and change your code as follows:


Public Sub Utility(theForm as Object)
with theForm
For Each MenuItem As ToolStripMenuItem In ContextMenuStrip1.Items
If LCase(Strings.Left(MenuItem.Name, 8)) = LCase("tsmAudit")
Then
MenuItem.Enabled = False
End If
Next
End With
End Sub

..and i hope it'll work fine.

Hope this helps,

Onur Güzel
 
J

Jack Jackson

Hi,
I have a form (Form1) with a ContextMenuStrip (ContextMenuStrip1). If I
pass Form1 to a Utility Sub with the following code:

Public Sub Utility(theForm as Object)
with theForm
For Each MenuItem As ToolStripMenuItem In .ContextMenuStrip1.Items
If LCase(Strings.Left(MenuItem.Name, 8)) = LCase("tsmAudit") Then
MenuItem.Enabled = False
End If
Next
End With
End Sub

I get an error the ContextMenuStrip1 does not exist on Form1 -- but it does.
If I run this code in Form1 it works perfectly. Does anyone know why this
doesn't work and how I can accomplish what I'm trying to do?

Thanks,

Utility can't see the ContextMenuStrip1 variable in the form unless it
is Public. Look at the Modifier property of ContextMenuStrip1 to see
what its access is.

The only way this code could work is if you use Option Strict Off,
which is a bad idea as it can hide errors. You declare the parameter
to Utility as Object, which means the resolution of the
ContextStripMenu1 item must be done at runtime, not compile time.

A better way to do this would be to pass the ContextMenuStrip to
Utility rather than the form. It is not a good idea for a routine
like Utility to know that the form it is passed has a context menu
called ContextMenu1:

Public Sub Utility(cms As ContextMenuStrip)
For Each mitem As ToolStripMenuItem In cms
....
 
G

ginacresse

Hi Onur,

I tried removing the dot, but then it told me "ContextMenuStrip1 is not
declared". I should clarify that the Utility sub is in a business object
class, not in the form.

Thanks for the suggestion, and I'm open to any more you may have.
 
G

ginacresse

I should add that it seems the ContextMenuStrip is not treated like any other
control on the form, which may be part of my problem. If I put a line of
code in my Utility sub like:

MyControl = theForm.Controls.Find("TextBox1", True)

it will find TextBox1 if it exists on theForm (Form1). But if I do this:

MyControl = theForm.Controls.Find("ContextMenuStrip1", True)

it will not find the ContextMenuStrip. This makes me think that the
ContextMenuStrip is not treated the same as other controls on the form.
 
G

ginacresse

Thanks, Jack. That was the problem. You are right, and I'll use your
suggestion about passing the ContextMenuStrip to the sub.

Thanks!
 
K

kimiraikkonen

Hi Onur,

I tried removing the dot, but then it told me "ContextMenuStrip1 is not
declared". I should clarify that the Utility sub is in a business object
class, not in the form.

Thanks for the suggestion, and I'm open to any more you may have.

Sorry, i missed "With" that specifies ContextMenuStrip1 is the member
of "theForm" using "dot"(that's why "with" is used), as well.

Then Jack's post should help you in that case, passing
ContextMenuStrip as parameter to Utility sub.

Thanks,

Onur
 
G

ginacresse

I'm wondering if there's a way to get the name of the ContextMenuStrip from
the form that has already been passed to the Utility sub. This is a very
large application there are a lot of calls to this utility sub (which
includes more code than I included in my example). I thought maybe a For
Each CMS in .Controls or possibly theForm.Controls.Find, but ContextMenuStrip
doesn't appear to be included in the Controls Collection. Am I barking up
the wrong tree here?

Thanks!
 
J

Jack Jackson

I don't believe there is any way to find all of the ContextMenuStrips
given a form reference.

Another approach, which will involve modifying all of the form classes
and therefore may not work for you, would be to define an interface
and have all of the form classes implement it.

Public Interface IMyContextMenu
ReadOnly Property ContextMenu() As ContextMenuStrip
End Interface

In each form class:

Public Class MyForm
Implements IMyContextMenu

Private Property MyContextMenu() As ContextMenuStrip _
Implements IMyContextMenu.Contextmenu
Get
Return Me.ContextMenu1
End Get
End Property

Then in Utility, given a reference to a form:

Dim imcm As IMyContextMenu = TryCast(theForm, IMyContextMenu)

If imcm IsNot Nothing AndAlso imcm.ContextMenu IsNot Nothing Then
' This form implements IMyContextMenu and has a context menu
For Each MenuItem As ToolStripMenuItem In imcm.ContextMenu.Items
...
 
G

ginacresse

Thanks, Jack. Of the two options, I think I like passing the CMS to the
Utilitiy sub best. I'll just have to roll up my sleeves and bite the bullet
:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top