MenuItem inheritance Issue
-
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
-
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
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
-
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
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.
-
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.