473,397 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

ContextMenuStrip not passed with Form?

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
9 2345
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jake | last post by:
I'm having a problem with the ContextMenuStrip object when using it on a NotifyIcon while the taskbar is at the top of the screen. Problem: The context menu will draw above the top of the...
0
by: academic | last post by:
I'm having a problem merging a ContextMenuStrip for a UserControl to the MenuStrip on the form containing the UserControl. I tried the following thinking that it is equivalent to cloning the...
1
by: Ron M. Newman | last post by:
I have a context menu strip. I can Add elements to its "Items", but there is nothing in there to add a sub context menu strip. Menu Item 1 Item 2 Sub Item 1 <-- impossible to add! How do...
7
by: Dave | last post by:
How do I get my ContextMenuStrip to receive key down preview events? I have the event hooked but I don't see the key strokes in the event? Dave
5
by: =?Utf-8?B?cHJvZ2dlcg==?= | last post by:
I've written an application that can minimize to the tray and can reappear if you double click on the icon in the tray (ShowInTaskbar is set to false). I've also created a ContextMenuStrip (Visual...
3
by: ommail | last post by:
I have two RichTextBox controls on a form, and single ContextMenuStrip control which serves for both textboxes. I need to determine which RichTextBox control invokes an event handler in...
7
by: Joe Cool | last post by:
Let's say I have a Treeview control on a form. Each leaf node in the Treeview has a ContextMenuStrip, each with one ToolStripMenuItem, and all ToolStripMenuItems Click event is handled by a comment...
1
by: Tony Johansson | last post by:
Hello! I have a System.Windows.Forms.MenuStrip with some ToolStripMenuItem. One of these ToolStripMenuItem is called saveMemberToolStripMenuItem. This one is defined like this...
2
by: eBob.com | last post by:
I've been working on an app which has an array of RichTextBoxes. And I have a context menu for the RTBs. The context menu has two levels; the first level has two items, "Load Sample Text File"...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.