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

ToolStripDropDownMenu and SourceControl ??

Hello all,

I have dynamic controls all associated with a single ContextMenuStrip. For
each menu item I have click event method. For each event I have cast sender
to ToolStripMenuItem to ContextMenuStip to use SourceControl to get the
control that initiated the event.

This all works well if the the menu items are one level deep on the
contextmenustrip control, but if I create a sub menu item doing the above
logic does not work.

First of all it gives a cast error of
"Unable to cast object of type "System...ToolStripDropDownMenu" to
"System...ContextMenuStrip." So in the case of a sub menu I changed the cast
to ToolStripDropDownMenu.

ToolStripMenuItem TlStrip = (ToolStripMenuItem)sender;
ToolStripDropDownMenu temp = (ToolStripDropDownMenu)TlStrip.Owner;
TlStrip = (ToolStripMenuItem)temp.OwnerItem;
ContextMenuStrip Mnu = (ContextMenuStrip)TlStrip.Owner;

Using above code Mnu.SourceControl is always null.

What am I doing wrong?

--
Best Regards

"Failure is the opportunity to begin again more intelligently" – Henry Ford
Jun 27 '08 #1
4 6105
Its difficult to know when there are no responses wheather my question is not
detailed enough, not a common problem so no one is familiar with it to
answer, or just a dumb question (I have had a few). :)

For now going with need clarification. :)

I have a context menu strip as follows:
TestA
TestB - SubTestB

if I click on TestA Everything works fine. If I click on SubTestB the below
call to GetCtrl returns true but MyIcon is null.

Code Snippets:
///////////////////////////////////////////////////////////////
private bool GetCtrl(ToolStripMenuItem sender, ref MyDefIcon MyIcon)
{
bool bGood = false;
try
{
if (sender != null)
{
ContextMenuStrip Mnu = (ContextMenuStrip)sender.Owner;

if (Mnu != null)
{
MyIcon = (MyDefIcon)Mnu.SourceControl;
bGood = true;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

return (bGood);
}
///////////////////////////////////////////////////////////////
private void TestAToolStripMenuItem_Click(object sender, EventArgs e)
{
MyDefIcon MyIcon = null;
if(GetCtrl((ToolStripMenuItem)sender, ref MyIcon ))
{
if (MyIcon != null)
{
// do command
}
}
}
}
///////////////////////////////////////////////////////////////
private void SubTestBToolStripMenuItem_Click(object sender,
EventArgs e)
{
ToolStripMenuItem TlStrip = (ToolStripMenuItem)sender;
ToolStripDropDownMenu temp =
(ToolStripDropDownMenu)TlStrip.Owner;

MyDefIcon MyIcon = null;
if(GetCtrl((ToolStripMenuItem)temp.OwnerItem, ref MyIcon ))
{
if (MyIcon != null)
{
// do command
}
}
}
}


Please give advice or direction.

--
Best Regards

"Failure is the opportunity to begin again more intelligently" – Henry Ford

Jun 27 '08 #2
On Mon, 28 Apr 2008 12:52:00 -0700, David
<Da***@discussions.microsoft.comwrote:
Its difficult to know when there are no responses wheather my question
is not
detailed enough, not a common problem so no one is familiar with it to
answer, or just a dumb question (I have had a few). :)
How about option D: three hours is way too early to start getting worried
about whether anyone's going to answer your question.

Anyway, I seem to be able to reproduce the issue you're seeing. And I'd
agree that it seems to be wrong. Upon finding the top-level
ContextMenuStrip instance in which your menu item is contained, the
SourceControl property should, IMHO, _always_ tell you the name of the
control used to show the menu. But for me, just as you said, I find the
property is set to null when handling the Click event of a menu item
that's not a top-level item.

Seems like a bug to me. I suppose it might not be, but I'd have to see a
really good explanation for the behavior for me to believe that. You may
at this point simply need to consider finding a work-around and report the
bug to Microsoft in the hopes of getting it fixed.

Pete
Jun 27 '08 #3
By the way, a quick Google search turned up these:

What looks like an earlier, worse manifestation of the bug you found:
http://connect.microsoft.com/VisualS...edbackID=96476

A newer bug report describing the specific behavior you're running into:
https://connect.microsoft.com/Visual...dbackID=282496

Looks like sloppy work by whoever fixed the first bug.

Both articles describe what seems to me to be a reasonable workaround
(handle the Opening event for the context menu, and get the SourceControl
value then, at which point it's apparently still correct, saving it for
later use in the Click handler for the item).

Pete
Jun 27 '08 #4
Thanks for the information Peter, and sorry for my impatience. :)

--
Best Regards

"Failure is the opportunity to begin again more intelligently" – Henry Ford
Jun 27 '08 #5

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

Similar topics

0
by: MD | last post by:
I have a ContextMenu that is assigned to 3 RichTextBox's (i.e., if you right-click on any of the 3 RichTextBox's, you get the same ContextMenu). My ContextMenu has 2 items, each of which have their...
1
by: Jose | last post by:
Hi, Our small company is growing quickly, and we need a source control and build tool for all our c# .net code. For build tool, i was thinking Nant. For source control, i was thinking...
8
by: Dennis C. Drumm | last post by:
Is there a way to modify the standard context menu shown when someone right clicks in a windows text box and that would work for all open windows applications? The standard context menu for...
1
by: Wael_Bakr | last post by:
Hi I used to use one contextmenu for many controls when popup I had to get sourcecontrol property of sender to know owner control using 2005 beta contextmenustrip I don't find a similar...
3
by: Andy | last post by:
Hi I need a source/version control system for my different projects. I know I could use SourceSafe 2005 when it comes, but I've spent a lot of money on MSDN pro and need a free/cheap...
3
by: Bad_Kid | last post by:
How can solution (asp.net web page) be removed from the SourceControl? Do I have to make a new "blank solution" and add in it all projects one by one?
1
by: goRide | last post by:
Hi, I'm looking of a way (preferred - a ready class or dll) to customize the context menu. many application has more controls inside the context menu (like textbox, sliders, checkbox, panel...
22
by: MarkusR | last post by:
I am currently using StarTeam for my source control. (Will eventual switch to whatever the latest Visual Source Safe is going to be called). With debug, installers, deploy and everything the...
13
by: PetterL | last post by:
I writing a program where I read menu items from a file. But I have problem when I click an menu item i want it to mark that one as checked but I cant access the menu object of that item to see...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.