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

Toolstrip item casting problem

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a

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

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

P: n/a

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.