473,406 Members | 2,390 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,406 software developers and data experts.

Problems Converting a ContextMenu to a ContextMenuStrip

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" and "Insert Row >"; the "Insert
Row >" item on the first level invokes, if that's the right word, a second
level which has two items, "Above" and "Below". This all worked when it was
a ContextMenu. But I am trying to change it to use the new menu classes and
I still have one unresolved problem.

Here's the code which sets up the menu ...

Dim CntxMen_rtbs As New ContextMenuStrip 'context menu for the
sample text rtbs

Dim LoadSampleTextFileMenuItem As New ToolStripMenuItem("Load Sample Text
File")
CntxMen_rtbs.Items.Add(LoadSampleTextFileMenuItem)
AddHandler LoadSampleTextFileMenuItem.Click, AddressOf
LoadSampleTextFileMenuItemClick

Dim miAB_array(1) As ToolStripMenuItem 'array of menu items for Above,
Below (for Insert Row/File)
miAB_array(0) = New ToolStripMenuItem("Above", Nothing, New
System.EventHandler(AddressOf InsertRowAbove_Click))
miAB_array(1) = New ToolStripMenuItem("Below", Nothing, New
System.EventHandler(AddressOf InsertRowBelow_Click))
Dim InsertRowMenuItem As New ToolStripMenuItem("Insert Row", Nothing,
miAB_array)
CntxMen_rtbs.Items.Add(InsertRowMenuItem)

This code results in a menu which LOOKS right, but an error here might
explain the problem I am having. (There doesn't seem to be a constructor
for ToolStripMenuItem which supports an EventHandler without an Image.)
But, as I said, this code results in a menu which looks correct.

The problem occurs in the EventHandler for the "Above" item. (I haven't
coded the "Below" item EventHandler yet.) The problem occurs in code which
is trying to determine the RTB for which the context menu was used. The
following code for the "Load Sample Text File" seems to operate correctly
....

Private Sub LoadSampleTextFileMenuItemClick(ByVal sender As Object, _
ByVal e As System.EventArgs)
Dim whichone As WhichSTrtb
Dim thertb As RichTextBox
Dim ctxmenu As ContextMenuStrip
Dim TheMenuItem As ToolStripMenuItem
TheMenuItem = DirectCast(sender, ToolStripMenuItem)
ctxmenu = DirectCast(TheMenuItem.Owner, ContextMenuStrip)
thertb = DirectCast(ctxmenu.SourceControl, RichTextBox)
whichone = DirectCast(thertb.Tag, WhichSTrtb)
MsgBox("menu item click entered for " + whichone.i.ToString + ", " +
whichone.j.ToString)

So here, finally, is the code (where the problem is) for the "Above" item
.... (similar, of course, to the code above - but it is trying to back up one
additional level ...)

Private Sub InsertRowAbove_Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
MsgBox("insert row above invoked")
Dim whichone As WhichSTrtb
Dim thertb As RichTextBox
Dim ctxmenu As ContextMenuStrip
Dim TheAboveBelowMenuItem As ToolStripMenuItem
TheAboveBelowMenuItem = DirectCast(sender, ToolStripMenuItem)
Dim TheInsertRowMenuItem As ContextMenuStrip
TheInsertRowMenuItem = DirectCast(TheAboveBelowMenuItem.Owner,
ContextMenuStrip)
ctxmenu = DirectCast(TheInsertRowMenuItem.Owner, ContextMenuStrip)
'******************************
thertb = DirectCast(ctxmenu.SourceControl, RichTextBox)
whichone = DirectCast(thertb.Tag, WhichSTrtb)
MsgBox("menu item click entered for " + whichone.i.ToString + ", " +
whichone.j.ToString)
End Sub

The code as shown has an Intellisense error on the indicated statement which
says "'Owner' is not a member of 'System.Windows.Forms.ContextMenuStrip'."
That I know I could fix. But I have spent many hours already getting rid of
various Intellisense errors only to run into run time errors.

So ... if someone could tell me the right way to find the RTB in my
InsertRowAbove_Click routine I would be very grateful.

Thanks, Bob
Aug 3 '08 #1
2 3395

"eBob.com" <eB******@totallybogus.comwrote in message
news:%2******************@TK2MSFTNGP06.phx.gbl...
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" and "Insert Row >"; the
"Insert Row >" item on the first level invokes, if that's the right word,
a second level which has two items, "Above" and "Below". This all worked
when it was a ContextMenu. But I am trying to change it to use the new
menu classes and I still have one unresolved problem.

Here's the code which sets up the menu ...

Dim CntxMen_rtbs As New ContextMenuStrip 'context menu for the
sample text rtbs

Dim LoadSampleTextFileMenuItem As New ToolStripMenuItem("Load Sample Text
File")
CntxMen_rtbs.Items.Add(LoadSampleTextFileMenuItem)
AddHandler LoadSampleTextFileMenuItem.Click, AddressOf
LoadSampleTextFileMenuItemClick

Dim miAB_array(1) As ToolStripMenuItem 'array of menu items for Above,
Below (for Insert Row/File)
miAB_array(0) = New ToolStripMenuItem("Above", Nothing, New
System.EventHandler(AddressOf InsertRowAbove_Click))
miAB_array(1) = New ToolStripMenuItem("Below", Nothing, New
System.EventHandler(AddressOf InsertRowBelow_Click))
Dim InsertRowMenuItem As New ToolStripMenuItem("Insert Row", Nothing,
miAB_array)
CntxMen_rtbs.Items.Add(InsertRowMenuItem)

This code results in a menu which LOOKS right, but an error here might
explain the problem I am having. (There doesn't seem to be a constructor
for ToolStripMenuItem which supports an EventHandler without an Image.)
But, as I said, this code results in a menu which looks correct.

The problem occurs in the EventHandler for the "Above" item. (I haven't
coded the "Below" item EventHandler yet.) The problem occurs in code
which is trying to determine the RTB for which the context menu was used.
The following code for the "Load Sample Text File" seems to operate
correctly ...

Private Sub LoadSampleTextFileMenuItemClick(ByVal sender As Object, _
ByVal e As System.EventArgs)
Dim whichone As WhichSTrtb
Dim thertb As RichTextBox
Dim ctxmenu As ContextMenuStrip
Dim TheMenuItem As ToolStripMenuItem
TheMenuItem = DirectCast(sender, ToolStripMenuItem)
ctxmenu = DirectCast(TheMenuItem.Owner, ContextMenuStrip)
thertb = DirectCast(ctxmenu.SourceControl, RichTextBox)
whichone = DirectCast(thertb.Tag, WhichSTrtb)
MsgBox("menu item click entered for " + whichone.i.ToString + ", " +
whichone.j.ToString)

So here, finally, is the code (where the problem is) for the "Above" item
... (similar, of course, to the code above - but it is trying to back up
one additional level ...)

Private Sub InsertRowAbove_Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
MsgBox("insert row above invoked")
Dim whichone As WhichSTrtb
Dim thertb As RichTextBox
Dim ctxmenu As ContextMenuStrip
Dim TheAboveBelowMenuItem As ToolStripMenuItem
TheAboveBelowMenuItem = DirectCast(sender, ToolStripMenuItem)
Dim TheInsertRowMenuItem As ContextMenuStrip
TheInsertRowMenuItem = DirectCast(TheAboveBelowMenuItem.Owner,
ContextMenuStrip)
ctxmenu = DirectCast(TheInsertRowMenuItem.Owner, ContextMenuStrip)
'******************************
thertb = DirectCast(ctxmenu.SourceControl, RichTextBox)
whichone = DirectCast(thertb.Tag, WhichSTrtb)
MsgBox("menu item click entered for " + whichone.i.ToString + ", " +
whichone.j.ToString)
End Sub

The code as shown has an Intellisense error on the indicated statement
which says "'Owner' is not a member of
'System.Windows.Forms.ContextMenuStrip'." That I know I could fix. But I
have spent many hours already getting rid of various Intellisense errors
only to run into run time errors.

So ... if someone could tell me the right way to find the RTB in my
InsertRowAbove_Click routine I would be very grateful.

Thanks, Bob
I don't know if this is progress, but the following code ...

Private Sub InsertRowAbove_Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
MsgBox("insert row above invoked")
Dim whichone As WhichSTrtb
Dim thertb As RichTextBox
Dim ctxmenu As ContextMenuStrip
Dim TheAboveBelowMenuItem As ToolStripMenuItem
TheAboveBelowMenuItem = DirectCast(sender, ToolStripMenuItem)
Dim TheInsertRowMenuItem As ToolStripMenuItem
TheInsertRowMenuItem = DirectCast(TheAboveBelowMenuItem.OwnerItem,
ToolStripMenuItem)
ctxmenu = DirectCast(TheInsertRowMenuItem.Owner, ContextMenuStrip)
thertb = DirectCast(ctxmenu.SourceControl, RichTextBox)
whichone = DirectCast(thertb.Tag, WhichSTrtb) ' **** NullReferenceException
****
MsgBox("menu item click entered for " + whichone.i.ToString + ", " +
whichone.j.ToString)
End Sub

.... gets no compile time errors. However, the statement indicated produces
a NullReference. That makes sense as the debugger shows that "thertb" is
Nothing. And that makes sense since ctxmenu.SourceControl is Nothing. I
hope that this is interesting info, but it is not helping me to find my
error.

Bob
Aug 4 '08 #2

"eBob.com" <eB******@totallybogus.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
>
"eBob.com" <eB******@totallybogus.comwrote in message
news:%2******************@TK2MSFTNGP06.phx.gbl...
>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" and "Insert Row >";
the "Insert Row >" item on the first level invokes, if that's the right
word, a second level which has two items, "Above" and "Below". This all
worked when it was a ContextMenu. But I am trying to change it to use
the new menu classes and I still have one unresolved problem.

Here's the code which sets up the menu ...

Dim CntxMen_rtbs As New ContextMenuStrip 'context menu for the
sample text rtbs

Dim LoadSampleTextFileMenuItem As New ToolStripMenuItem("Load Sample Text
File")
CntxMen_rtbs.Items.Add(LoadSampleTextFileMenuItem )
AddHandler LoadSampleTextFileMenuItem.Click, AddressOf
LoadSampleTextFileMenuItemClick

Dim miAB_array(1) As ToolStripMenuItem 'array of menu items for Above,
Below (for Insert Row/File)
miAB_array(0) = New ToolStripMenuItem("Above", Nothing, New
System.EventHandler(AddressOf InsertRowAbove_Click))
miAB_array(1) = New ToolStripMenuItem("Below", Nothing, New
System.EventHandler(AddressOf InsertRowBelow_Click))
Dim InsertRowMenuItem As New ToolStripMenuItem("Insert Row", Nothing,
miAB_array)
CntxMen_rtbs.Items.Add(InsertRowMenuItem)

This code results in a menu which LOOKS right, but an error here might
explain the problem I am having. (There doesn't seem to be a constructor
for ToolStripMenuItem which supports an EventHandler without an Image.)
But, as I said, this code results in a menu which looks correct.

The problem occurs in the EventHandler for the "Above" item. (I haven't
coded the "Below" item EventHandler yet.) The problem occurs in code
which is trying to determine the RTB for which the context menu was used.
The following code for the "Load Sample Text File" seems to operate
correctly ...

Private Sub LoadSampleTextFileMenuItemClick(ByVal sender As Object, _
ByVal e As System.EventArgs)
Dim whichone As WhichSTrtb
Dim thertb As RichTextBox
Dim ctxmenu As ContextMenuStrip
Dim TheMenuItem As ToolStripMenuItem
TheMenuItem = DirectCast(sender, ToolStripMenuItem)
ctxmenu = DirectCast(TheMenuItem.Owner, ContextMenuStrip)
thertb = DirectCast(ctxmenu.SourceControl, RichTextBox)
whichone = DirectCast(thertb.Tag, WhichSTrtb)
MsgBox("menu item click entered for " + whichone.i.ToString + ", " +
whichone.j.ToString)

So here, finally, is the code (where the problem is) for the "Above" item
... (similar, of course, to the code above - but it is trying to back up
one additional level ...)

Private Sub InsertRowAbove_Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
MsgBox("insert row above invoked")
Dim whichone As WhichSTrtb
Dim thertb As RichTextBox
Dim ctxmenu As ContextMenuStrip
Dim TheAboveBelowMenuItem As ToolStripMenuItem
TheAboveBelowMenuItem = DirectCast(sender, ToolStripMenuItem)
Dim TheInsertRowMenuItem As ContextMenuStrip
TheInsertRowMenuItem = DirectCast(TheAboveBelowMenuItem.Owner,
ContextMenuStrip)
ctxmenu = DirectCast(TheInsertRowMenuItem.Owner, ContextMenuStrip)
'******************************
thertb = DirectCast(ctxmenu.SourceControl, RichTextBox)
whichone = DirectCast(thertb.Tag, WhichSTrtb)
MsgBox("menu item click entered for " + whichone.i.ToString + ", " +
whichone.j.ToString)
End Sub

The code as shown has an Intellisense error on the indicated statement
which says "'Owner' is not a member of
'System.Windows.Forms.ContextMenuStrip'." That I know I could fix. But I
have spent many hours already getting rid of various Intellisense errors
only to run into run time errors.

So ... if someone could tell me the right way to find the RTB in my
InsertRowAbove_Click routine I would be very grateful.

Thanks, Bob
I don't know if this is progress, but the following code ...

Private Sub InsertRowAbove_Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
MsgBox("insert row above invoked")
Dim whichone As WhichSTrtb
Dim thertb As RichTextBox
Dim ctxmenu As ContextMenuStrip
Dim TheAboveBelowMenuItem As ToolStripMenuItem
TheAboveBelowMenuItem = DirectCast(sender, ToolStripMenuItem)
Dim TheInsertRowMenuItem As ToolStripMenuItem
TheInsertRowMenuItem = DirectCast(TheAboveBelowMenuItem.OwnerItem,
ToolStripMenuItem)
ctxmenu = DirectCast(TheInsertRowMenuItem.Owner, ContextMenuStrip)
thertb = DirectCast(ctxmenu.SourceControl, RichTextBox)
whichone = DirectCast(thertb.Tag, WhichSTrtb) ' ****
NullReferenceException ****
MsgBox("menu item click entered for " + whichone.i.ToString + ", " +
whichone.j.ToString)
End Sub

... gets no compile time errors. However, the statement indicated
produces a NullReference. That makes sense as the debugger shows that
"thertb" is Nothing. And that makes sense since ctxmenu.SourceControl is
Nothing. I hope that this is interesting info, but it is not helping me
to find my error.

Bob
Well ... after many Google searches and reading until my eyes were bloodshot
I've learned that what I was trying to do just doesn't seem to be possible.
The workaround is to salt away the SourceControl in the ContextMenuStrip
Opening event. A bit of a hack in my opinion. Here's a useful thread which
probably explains the problem better than I did:
http://groups.google.com/group/micro...9d69233f6ea851

Aug 5 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mantorok | last post by:
Hi I have a button on a Windows Form and it has a ContextMenu set, when the user clicks the button I want the menu to appear, I've tried this: btnMenu.ContextMenuStrip.Show(); Absolutely...
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
1
by: =?Utf-8?B?QnJhZA==?= | last post by:
i have a menu system that is generated dynamically everything works good except for one minor astetic.... I click on a menu item that displays a ContextMenuStrip Popup Menu If an item on...
0
by: Dom | last post by:
I'm using a DataGridView, and the right mouse button has a special functionality. Unfortunately, the Context Menu always pops up. I noticed that the ContextMenuStrip of the EditingControl is...
0
by: Dom | last post by:
I'm using a DataGridView, and the right mouse button has a special functionality. Unfortunately, the Context Menu always pops up. I noticed that the ContextMenuStrip of the EditingControl is...
6
by: Academia | last post by:
VS2008 The help for Form lists ContextMenu as a property but it does not appear in the properties for my form. Is there something I must do to make it appear there. Thanks
1
by: Darin | last post by:
I understand microsoft's change from contextmenu to contextmenustrip - the strip looks "prettier". But, since the contextmenu isn't available in the designer, it suer woul dhave been nice when...
1
by: ehud37new | last post by:
this script work fine in IE but not in FireFox where is the problem? here is the script /*------------------------------------------------------------------ File: menu.js ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.