Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. MenuItem inheritance Issue

MenuItem inheritance Issue

Scheduled Pinned Locked Moved C#
helpoopquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    ekynox
    wrote on last edited by
    #1

    Hi Folks, I just was experimenting with customizing the MenuItem class to suit my needs. I have introduced an accessor method which handles an additional string variable in my inherited class called PJMenuItem. PJMenuItem pjmi = new PJMenuItem(); pjmi.ExtraInfo = "c:\\temp\\"; pjmi.Text = "HelloWorld"; cm.MenuItems.Add(pjmi.Text,new System.EventHandler(LaunchAction)); Now the problem becomes that each time the menuitem gets invoked via the LaunchAction event handler an exception message gets thrown. Stating that the specified cast is invalid. The event handler is defined as private void LaunchAction(object sender, System.EventArgs e) { try { PJMenuItem pjmi = (PJMenuItem) sender; MessageBox.Show(pjmi.ExtraInfo.ToString()); } catch(System.Exception ee) { MessageBox.Show(ee.ToString()); } } Can anyone please tell me how I can overcome this exception error ??? thanks

    R 1 Reply Last reply
    0
    • E ekynox

      Hi Folks, I just was experimenting with customizing the MenuItem class to suit my needs. I have introduced an accessor method which handles an additional string variable in my inherited class called PJMenuItem. PJMenuItem pjmi = new PJMenuItem(); pjmi.ExtraInfo = "c:\\temp\\"; pjmi.Text = "HelloWorld"; cm.MenuItems.Add(pjmi.Text,new System.EventHandler(LaunchAction)); Now the problem becomes that each time the menuitem gets invoked via the LaunchAction event handler an exception message gets thrown. Stating that the specified cast is invalid. The event handler is defined as private void LaunchAction(object sender, System.EventArgs e) { try { PJMenuItem pjmi = (PJMenuItem) sender; MessageBox.Show(pjmi.ExtraInfo.ToString()); } catch(System.Exception ee) { MessageBox.Show(ee.ToString()); } } Can anyone please tell me how I can overcome this exception error ??? thanks

      R Offline
      R Offline
      Rob Graham
      wrote on last edited by
      #2

      You're using the wrong overload of the Add method. Passing a string as the frst parameter causes a new MenuItem to be created for you, with the supplied text as the caption and the specified handler. You need to specify the handler in your PJMenuItem constructor (and pass it to the base class constructor) then used the version of the Add method that takes a reference to the menuitem. Absolute faith corrupts as absolutely as absolute power Eric Hoffer All that is necessary for the triumph of evil is that good men do nothing. Edmund Burke

      E 1 Reply Last reply
      0
      • R Rob Graham

        You're using the wrong overload of the Add method. Passing a string as the frst parameter causes a new MenuItem to be created for you, with the supplied text as the caption and the specified handler. You need to specify the handler in your PJMenuItem constructor (and pass it to the base class constructor) then used the version of the Add method that takes a reference to the menuitem. Absolute faith corrupts as absolutely as absolute power Eric Hoffer All that is necessary for the triumph of evil is that good men do nothing. Edmund Burke

        E Offline
        E Offline
        ekynox
        wrote on last edited by
        #3

        g'day Rob Would it be possible for you to give me another example of your explanation as I am unable to follow through. The point I am having difficulty is with the part where you said i need to specify the event handler in the constructor and then passing it to the base class constructor. I am refering to the following article on msdn about base class constructors: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfbasepg.asp But I cant seem to draw a correlation between the msdn article and your explanation. I would really appreciate it if you could give me another example. thanks.

        E 1 Reply Last reply
        0
        • E ekynox

          g'day Rob Would it be possible for you to give me another example of your explanation as I am unable to follow through. The point I am having difficulty is with the part where you said i need to specify the event handler in the constructor and then passing it to the base class constructor. I am refering to the following article on msdn about base class constructors: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfbasepg.asp But I cant seem to draw a correlation between the msdn article and your explanation. I would really appreciate it if you could give me another example. thanks.

          E Offline
          E Offline
          ekynox
          wrote on last edited by
          #4

          G'day again Rob, I read your post again and again inconjunction with Uncle gate's article, I finally understood it. Thanks for helping me out.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups