问题
I am working on a wizard form framework that will allow me easily create wizard forms.
I have a WizardForm form that has a panel on it.
My plan is to dynamically load UserControls into this panel on the form.
Each UserControl that is loaded onto the form panel must implement certain properties (allowNavigateNext, AllowNAvigate previous etc.). So I have created an interface to enforce that contract so that any custom user control can be used in the wizard as long as it implements that Interface. So far so good.
I created my first user control. I implemented the interface. All was good.
I went to do the second user control and I realized that this second user control would have the EXACT SAME implementation of the interface as the first User Control. So...I thought...aha...duplicated code...how can I get rid of that.
So...
I created a NEW user control and called it UserControlWizardBase. I had this new UserControlWizardBase inherit from the same interface and put the same implementation in there as with the first two.
Then I went back to custom user controls 1 and 2. I removed the interface implementation and interface statements in there. I went into the designer file of each though and changed the inheritance of each from UserControl to UserControlWizardBase.
That didn't quite compile until I added a public constructor that calls into the base constructore like this...
Public Sub New(ByVal showNavigateCancel As Boolean, _ ByVal showNavigateFinish As Boolean, _ ByVal showNavigateNext As Boolean, _ ByVal showNavigatePrevious As Boolean)
MyBase.New(showNavigateCancel, _
showNavigateFinish, _
showNavigateNext, _
showNavigatePrevious)
End Sub
(I learned at that moment that you can't inherit the constructor of the base class. Or can you and I just didn't know what I was doing?)
In any case...now all compiles...in fact there are no run time errors at all. But when I run the code and load CustomUserControl 1 by loading into the panel on the wizard form...it is NOT loading CustomUserControl at all but rather is loading USerControlWizardBase.
Now hopefully you understand my problem AND you can point me in the right direction for solving it.
EDIT - MORE INFORMATION
I am closer to having a bead on the problem. Here is the problem. The code that loads the userControl into my wizard form panel is as follows
Public Sub InitUserControl(ByVal wizardUserControl As UserControl)
wizardUserControl.Dock = DockStyle.Fill
Try
Dim iwp As IWizardUserControl = DirectCast(wizardUserControl, IWizardUserControl)
_wizardDialog.NavigatePrevious.Enabled = iwp.ShowNavigatePrevious
_wizardDialog.NavigateNext.Enabled = iwp.ShowNavigateNext
_wizardDialog.NavigateFinish.Enabled = iwp.ShowNavigateFinish
_wizardDialog.NavigateCancel.Enabled = iwp.ShowNavigateCancel
TryCast(_wizardDialog, Form).Text = iwp.StepCaption
_wizardDialog.PageDescription.Text = iwp.Description
_wizardDialog.PictureBox.Image = iwp.PageImage
_wizardDialog.PageHelpText.Text = iwp.PageHelpText
_wizardDialog.UIRoot.Controls.Clear()
_wizardDialog.UIRoot.Controls.Add(wizardUserControl)
Catch
' Do Nothing
End Try
End Sub
Do you see the problem...the routine is passing in a usercontrol and then casting it to type IWizardUserControl.
The problem is that it needs to load the specific type into the panel not the interface or UserControl type. But how do I do that without tying it to a specific implementation of IWizardUserControl?
Help. I am still learning OOP as you can tell.
ANOTHER EDIT - MORE INFORMATION STILL
Let me describe the NATURE of the problem. I get no runtime errors with the code you see here. But it is loading the BASE Type instead of the DERIVED type even though the InitUserControl is being passed one of the derived types. (I know that because if I put a breakpoint there and drill into the type it is showing the type NOT as UserControlWizardBase but the correct type which is one of the derived types...in this case SetupWizardStartPage).
Keep in mind that SetupWizardStartPage has a big label with a large font and a lot of text describing what the wizard does.
But when it loads the none of that shows. If I try to see if that description label is there it is NOT there. What IS there is the ONE AND ONLY control that I have implemented in UserControlWizardBase.
That is how I know that the base control rather than the derived control is being loaded into the panel. THIS even though if I query the wizardUSerControl it is showing the derived type rather than base type.
Seth
回答1:
You are correct in that you cannot not inherit constructors.
I bet it is loading your class but it is showing it as a USerControlWizardBase. Try casting it to your interface or the base class (UserControlWizardBase.TryCast(control)
).
回答2:
I don't see a problem with your implementation, other than the fact that you really should be passing in wizardUserControl
to Controls.Add
, not iwp
. Aside from that, what particular problem are you encountering?
回答3:
I feel REALLY REALLY stupid. The problem is that line of code that calls into the base constructor MyBase.NEW
was supposed to be followed by ITS own InitializeComponent. The only initialize component that was being called was the base classes not the derived classes.
Duh.
Seth
来源:https://stackoverflow.com/questions/1183424/winforms-usercontrol-is-not-using-the-inheritance-tree-i-have-created-what-am-i