Toolstrip item casting problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • poppy

    Toolstrip item casting problem

    I am having trouble creating a menu item.

    The Code I have:

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

    The error I get is :
    "Unable to cast object of type 'System.Windows .Forms.ContextM enuStrip' to
    type 'System.Windows .Forms.ToolStri pItem'."}

    When I hover my mouse over "object sender" I get "{
    [System.Windows. Forms.ContextMe nuStrip], Name: contextMenuStri p1, Items: 6} "

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

    #2
    Re: Toolstrip item casting problem


    poppy wrote:[color=blue]
    > I am having trouble creating a menu item.
    >
    > The Code I have:
    >
    > private void NewLoad(object sender, System.EventArg s e) {
    > //error here
    > ToolStripItem mn = (ToolStripItem) sender;
    > }
    >
    > The error I get is :
    > "Unable to cast object of type 'System.Windows .Forms.ContextM enuStrip' to
    > type 'System.Windows .Forms.ToolStri pItem'."}
    >
    > When I hover my mouse over "object sender" I get "{
    > [System.Windows. Forms.ContextMe nuStrip], Name: contextMenuStri p1, Items: 6} "
    >
    > What am I doing wrong. I'm going to cry now.[/color]

    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 ToolStripDropDo wn control" - so it's a _thing that goes in
    a container_. But a ContextMenuStri p is a ToolStripDropDo wnMenu, which
    is a ToolStripDropDo wn, which is a ToolStrip, which "Provides a
    container for Windows toolbar objects". So a ContextMenuStri p 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

    Comment

    • poppy

      #3
      Re: Toolstrip item casting problem

      Thanks.
      Heres the code :

      public bool Register(IPlugi n ipi)
      {
      contextMenuStri p1.Click += new EventHandler(Ne wLoad);
      this.contextMen uStrip1.Items.A dd(ipi.Name);
      Console.WriteLi ne("Registered : " + ipi.Name);
      return true;
      }

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

      "Larry Lard" wrote:
      [color=blue]
      >
      > poppy wrote:[color=green]
      > > I am having trouble creating a menu item.
      > >
      > > The Code I have:
      > >
      > > private void NewLoad(object sender, System.EventArg s e) {
      > > //error here
      > > ToolStripItem mn = (ToolStripItem) sender;
      > > }
      > >
      > > The error I get is :
      > > "Unable to cast object of type 'System.Windows .Forms.ContextM enuStrip' to
      > > type 'System.Windows .Forms.ToolStri pItem'."}
      > >
      > > When I hover my mouse over "object sender" I get "{
      > > [System.Windows. Forms.ContextMe nuStrip], Name: contextMenuStri p1, Items: 6} "
      > >
      > > What am I doing wrong. I'm going to cry now.[/color]
      >
      > 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 ToolStripDropDo wn control" - so it's a _thing that goes in
      > a container_. But a ContextMenuStri p is a ToolStripDropDo wnMenu, which
      > is a ToolStripDropDo wn, which is a ToolStrip, which "Provides a
      > container for Windows toolbar objects". So a ContextMenuStri p 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
      >
      >[/color]

      Comment

      • Larry Lard

        #4
        Re: Toolstrip item casting problem


        poppy wrote:[color=blue]
        > Thanks.
        > Heres the code :
        >
        > public bool Register(IPlugi n ipi)
        > {
        > contextMenuStri p1.Click += new EventHandler(Ne wLoad);
        > this.contextMen uStrip1.Items.A dd(ipi.Name);
        > Console.WriteLi ne("Registered : " + ipi.Name);
        > return true;
        > }
        >
        > I am basically using reflection on an assemblt to dynamically create a
        > context menu.[/color]

        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(IPlugi n ipi)
        {
        ToolStripItem newItem =
        this.contextMen uStrip1.Items.A dd(ipi.Name);
        newItem.Click += new EventHandler(Ne wLoad);

        Console.WriteLi ne("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

        Comment

        • poppy

          #5
          Re: Toolstrip item casting problem

          Thanks for the help Larry but I finaly sorted it :
          ToolStripMenuIt em item = new ToolStripMenuIt em(ipi.Name, null, new
          EventHandler(Ne wLoad));
          this.contextMen uStrip1.Items.A dd(item);

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



          "Larry Lard" wrote:
          [color=blue]
          >
          > poppy wrote:[color=green]
          > > Thanks.
          > > Heres the code :
          > >
          > > public bool Register(IPlugi n ipi)
          > > {
          > > contextMenuStri p1.Click += new EventHandler(Ne wLoad);
          > > this.contextMen uStrip1.Items.A dd(ipi.Name);
          > > Console.WriteLi ne("Registered : " + ipi.Name);
          > > return true;
          > > }
          > >
          > > I am basically using reflection on an assemblt to dynamically create a
          > > context menu.[/color]
          >
          > 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(IPlugi n ipi)
          > {
          > ToolStripItem newItem =
          > this.contextMen uStrip1.Items.A dd(ipi.Name);
          > newItem.Click += new EventHandler(Ne wLoad);
          >
          > Console.WriteLi ne("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
          >
          >[/color]

          Comment

          Working...