Answered by:
Runtime InvalidCastException

Question
-
Hi all!
In my app there is a class named TabPageEx. This class is an extension of System.Windows.Forms.TabPage
At runtime, it occurs an error, when I try to get the selected tabpage and try to set a TabPageEx object using it.
Here is the code that I try to execute.this.tabPageEx = (TabPageEx)tcDataPanes.SelectedTab;
The error message:
System.InvalidCastException was unhandled
Message="Unable to cast object of type 'System.Windows.Forms.TabPage' to type 'Interface.TabPageEx'."
Source="Horus"
StackTrace:
at Horus.fMain.button1_Click(Object sender, EventArgs e) in C:\gdt\HMain.cs:line 214
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Horus.Program.Main() in C:\gdt\Horus\Horus\Program.cs:line 18
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
How can I set the tabPageEx1 object?
Thank you in advance!
- Moved by jack 321 Friday, June 20, 2008 8:20 AM off topic
Wednesday, June 18, 2008 1:49 PM
Answers
-
Discussion in Windows Forms General
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- Marked as answer by Ed Price - MSFTMicrosoft employee Friday, May 11, 2012 5:00 AM
Friday, June 20, 2008 8:20 AM -
- Proposed as answer by Ed Price - MSFTMicrosoft employee Friday, May 11, 2012 5:02 AM
- Marked as answer by Ed Price - MSFTMicrosoft employee Friday, May 11, 2012 5:02 AM
Friday, May 11, 2012 5:02 AM
All replies
-
TabControl.SelectedTab returns a TabPage, which is the base class you have extended with your TabPageEx. Therefore you are trying to downcast from a base class (TabPage) to a derived class (TabPageEx). Such downcasting is an illegal operation - you can't go from something more general to something more specific. Your TabPageEx may have lots of fields and methods that are not in TabPage. TabControl.SelectedTab gives you a TabPage object - from where is the runtime expected to get the extra fields and methods that a TabPageEx object expects?Wednesday, June 18, 2008 2:02 PM -
I understand your explanation. It was very helpful.
So, how can I get the tabPageEx which is selected? I need to get it, because it contains a container which I will use to add some controls dynamically.
After to execute the showed command:
this.tabPageEx = tcDataPanes.SelectedTab;
I would like to execute this one.
this.tabPageEx.addControl(dial);
The method addControl selects a container and so, adds the control at it.
Can you help me to find a solution to this problem?
Thank you again!
Wednesday, June 18, 2008 4:22 PM -
Discussion in Windows Forms General
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- Marked as answer by Ed Price - MSFTMicrosoft employee Friday, May 11, 2012 5:00 AM
Friday, June 20, 2008 8:20 AM -
- Proposed as answer by Ed Price - MSFTMicrosoft employee Friday, May 11, 2012 5:02 AM
- Marked as answer by Ed Price - MSFTMicrosoft employee Friday, May 11, 2012 5:02 AM
Friday, May 11, 2012 5:02 AM