By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 769 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

ContextMenuStrip not passed with Form?

P: n/a
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,
Jun 27 '08 #1
Share this Question
Share on Google+
9 Replies


P: n/a
On Jun 5, 10:13 pm, ginacresse <ginacre...@discussions.microsoft.com>
wrote:
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

Jun 27 '08 #2

P: n/a
On Thu, 5 Jun 2008 12:13:02 -0700, ginacresse
<gi********@discussions.microsoft.comwrote:
>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
....
Jun 27 '08 #3

P: n/a
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.
Jun 27 '08 #4

P: n/a
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.
Jun 27 '08 #5

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

Thanks!

"Jack Jackson" wrote:
On Thu, 5 Jun 2008 12:13:02 -0700, ginacresse
<gi********@discussions.microsoft.comwrote:
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
....
Jun 27 '08 #6

P: n/a
On Jun 5, 11:49 pm, ginacresse <ginacre...@discussions.microsoft.com>
wrote:
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
Jun 27 '08 #7

P: n/a
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!

"Jack Jackson" wrote:
On Thu, 5 Jun 2008 12:13:02 -0700, ginacresse
<gi********@discussions.microsoft.comwrote:
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
....
Jun 27 '08 #8

P: n/a
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
...
On Thu, 5 Jun 2008 14:38:07 -0700, ginacresse
<gi********@discussions.microsoft.comwrote:
>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!

"Jack Jackson" wrote:
>On Thu, 5 Jun 2008 12:13:02 -0700, ginacresse
<gi********@discussions.microsoft.comwrote:
>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
....
Jun 27 '08 #9

P: n/a
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
:-)
"Jack Jackson" wrote:
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
...
On Thu, 5 Jun 2008 14:38:07 -0700, ginacresse
<gi********@discussions.microsoft.comwrote:
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!

"Jack Jackson" wrote:
On Thu, 5 Jun 2008 12:13:02 -0700, ginacresse
<gi********@discussions.microsoft.comwrote:

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
....
Jun 27 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.