Toolstrip item casting problem

G

Guest

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.
 
L

Larry Lard

poppy said:
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.
 
G

Guest

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 said:
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.
 
L

Larry Lard

poppy said:
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*.
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top