473,473 Members | 1,800 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Toolstrip item casting problem

I am having trouble creating a menu item.

The Code I have:

private void NewLoad(object sender, System.EventArgs e) {
//error here
ToolStripItem mn = (ToolStripItem)sender;
}

The error I get is :
"Unable to cast object of type 'System.Windows.Forms.ContextMenuStrip' to
type 'System.Windows.Forms.ToolStripItem'."}

When I hover my mouse over "object sender" I get "{
[System.Windows.Forms.ContextMenuStrip], Name: contextMenuStrip1, Items: 6} "

What am I doing wrong. I'm going to cry now.
Apr 6 '06 #1
4 11277

poppy wrote:
I am having trouble creating a menu item.

The Code I have:

private void NewLoad(object sender, System.EventArgs e) {
//error here
ToolStripItem mn = (ToolStripItem)sender;
}

The error I get is :
"Unable to cast object of type 'System.Windows.Forms.ContextMenuStrip' to
type 'System.Windows.Forms.ToolStripItem'."}

When I hover my mouse over "object sender" I get "{
[System.Windows.Forms.ContextMenuStrip], Name: contextMenuStrip1, Items: 6} "

What am I doing wrong. I'm going to cry now.


Don't cry :( Basically you are mixing up _containers_ and _things that
go in containers_. A ToolStripItem is "an element such as a button,
combo box, text box, or label that can be contained in a ToolStrip
control or a ToolStripDropDown control" - so it's a _thing that goes in
a container_. But a ContextMenuStrip is a ToolStripDropDownMenu, which
is a ToolStripDropDown, which is a ToolStrip, which "Provides a
container for Windows toolbar objects". So a ContextMenuStrip is a
_container_

So what we need to see now is the code where you hook up this event
handler, and we can tell you what's wrong with it. It looks like you've
hooked up to a container event when you meant to hook up to an item
event, but we need to see it to be sure.

--
Larry Lard
Replies to group please

Apr 6 '06 #2
Thanks.
Heres the code :

public bool Register(IPlugin ipi)
{
contextMenuStrip1.Click += new EventHandler(NewLoad);
this.contextMenuStrip1.Items.Add(ipi.Name);
Console.WriteLine("Registered: " + ipi.Name);
return true;
}

I am basically using reflection on an assemblt to dynamically create a
context menu.

"Larry Lard" wrote:

poppy wrote:
I am having trouble creating a menu item.

The Code I have:

private void NewLoad(object sender, System.EventArgs e) {
//error here
ToolStripItem mn = (ToolStripItem)sender;
}

The error I get is :
"Unable to cast object of type 'System.Windows.Forms.ContextMenuStrip' to
type 'System.Windows.Forms.ToolStripItem'."}

When I hover my mouse over "object sender" I get "{
[System.Windows.Forms.ContextMenuStrip], Name: contextMenuStrip1, Items: 6} "

What am I doing wrong. I'm going to cry now.


Don't cry :( Basically you are mixing up _containers_ and _things that
go in containers_. A ToolStripItem is "an element such as a button,
combo box, text box, or label that can be contained in a ToolStrip
control or a ToolStripDropDown control" - so it's a _thing that goes in
a container_. But a ContextMenuStrip is a ToolStripDropDownMenu, which
is a ToolStripDropDown, which is a ToolStrip, which "Provides a
container for Windows toolbar objects". So a ContextMenuStrip is a
_container_

So what we need to see now is the code where you hook up this event
handler, and we can tell you what's wrong with it. It looks like you've
hooked up to a container event when you meant to hook up to an item
event, but we need to see it to be sure.

--
Larry Lard
Replies to group please

Apr 6 '06 #3

poppy wrote:
Thanks.
Heres the code :

public bool Register(IPlugin ipi)
{
contextMenuStrip1.Click += new EventHandler(NewLoad);
this.contextMenuStrip1.Items.Add(ipi.Name);
Console.WriteLine("Registered: " + ipi.Name);
return true;
}

I am basically using reflection on an assemblt to dynamically create a
context menu.


OK. So you create a context menu with an item on it with ipi.Name as
its text. And you want NewLoad to handle... clicking on the *item*,
presumably? In which case you should do this:

public bool Register(IPlugin ipi)
{
ToolStripItem newItem =
this.contextMenuStrip1.Items.Add(ipi.Name);
newItem.Click += new EventHandler(NewLoad);

Console.WriteLine("Registered: " + ipi.Name);
return true;
}

Hopefully you see why - We are now hooking to the Click event of the
*item*, not the *menu*.

--
Larry Lard
Replies to group please

Apr 6 '06 #4
Thanks for the help Larry but I finaly sorted it :
ToolStripMenuItem item = new ToolStripMenuItem(ipi.Name, null, new
EventHandler(NewLoad));
this.contextMenuStrip1.Items.Add(item);

You where right, I was using a container instead of the item.

"Larry Lard" wrote:

poppy wrote:
Thanks.
Heres the code :

public bool Register(IPlugin ipi)
{
contextMenuStrip1.Click += new EventHandler(NewLoad);
this.contextMenuStrip1.Items.Add(ipi.Name);
Console.WriteLine("Registered: " + ipi.Name);
return true;
}

I am basically using reflection on an assemblt to dynamically create a
context menu.


OK. So you create a context menu with an item on it with ipi.Name as
its text. And you want NewLoad to handle... clicking on the *item*,
presumably? In which case you should do this:

public bool Register(IPlugin ipi)
{
ToolStripItem newItem =
this.contextMenuStrip1.Items.Add(ipi.Name);
newItem.Click += new EventHandler(NewLoad);

Console.WriteLine("Registered: " + ipi.Name);
return true;
}

Hopefully you see why - We are now hooking to the Click event of the
*item*, not the *menu*.

--
Larry Lard
Replies to group please

Apr 6 '06 #5

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

Similar topics

1
by: GatorBait | last post by:
Hi all, I'm using the new ToolStrip control in a project and I'm running into a problem that I'm hoping someone can help me with. I am building the toolstrip in code by adding buttons,...
2
by: VBTricks.de.vu Webmaster | last post by:
Hello, I'm still reimplementing the GUI of my app using the new toolstrips. My current problem is to autosize a combobox to the available with in the parent toolstrip. There are some buttons...
4
by: Adam Honek | last post by:
I have a ToolStrip. The ToolStrip has a ToolStripDropDownButton with 3 ToolStripMenuItems My question is how do I catch the click event ot these 3 ToolStripMenuItems? I tried the below but...
1
by: PJ6 | last post by:
This would have been better if I could have just implemented an interface or two to author my own toolstrip controls... but no, I have to inherit from just the existing ones. Fine, I thought... ...
4
by: gene kelley | last post by:
The default ToolStrip apparently renders an MS Office Style ToolStrip when the RenderMode is other than "System". When using a BackgroundImage in the ToolStrip, this Office Style border has rounded...
6
by: Mike | last post by:
I have a program that is crashing on some machines (at a client of ours). I have determined that it goes down on this line of code: ToolStrip x = new ToolStrip(); I created a simple test app...
4
mafaisal
by: mafaisal | last post by:
Hello, I am Using Vb.Net2005. Project is Desktop Application In Mdi Parent Form I Have 20 Toolstrip Menu Items. I am Developed in 1024 * 768 screen resolution. When we working that All Menu Item...
1
by: priyamtheone | last post by:
In C# 2005 (.Net 2.0), while working with a toolstrip, if its TabStop property is set to TRUE and there are two comboboxes and a button in the toolstrip, why doesn't the button get any focus when the...
3
HaLo2FrEeEk
by: HaLo2FrEeEk | last post by:
Ok, I have a menuStrip and a toolStrip. the menuStrip is laid out like this: Actions Connect Disconnect - Exit Pretty simple. The toolstrip has 1 item, a toolstripstatus. I...
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...
1
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...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.