Click here to Skip to main content
15,919,245 members

Comments by grcook00 (Top 3 by date)

grcook00 27-Jan-11 20:43pm View    
I guess I may have answered my own question. What I need to do is add the control programmatically in the constructor (rather than dropping it via the designer); then all seems to work as expected (at least in the bare-bones version). I hope that this also works once the bones have been filled in; it would also be nice to be able to use the GUI for property value modification, which I cannot do with this workaround (the designer does not show a property grid for late-added controls...). If anyone else has a workaround, please let me know!
grcook00 27-Jan-11 20:21pm View    
Thank you, Manfred, for your suggestion. Although I cannot refute that this may be by design, there must certainly be a workaround. As I am unable to edit the Designer-Generated Code, could I, perhaps, create a Form class that the designer would properly interpret? Or is there some other way that I can perform my original task (that is, to allow my Class to only be instantiated against certain controls of varying types (derived from Form and Panel), and defining methods and properties that the implementing controls must have)?

Thanks again for any and all suggestions!
grcook00 27-Jan-11 20:16pm View    
SA -


I appreciate your comment on my technique. However, I do not know what the "suspect" part of code would be that I have not presented. To that end, I created a virgin VB Express project and modified the code for class Form1 as follows:

Public Class Form1
Implements IMyControlInterface

'...

End Class

Public Interface IMyControlInterface

End Interface

Public Class MyControl
Inherits Control

Private MyParentControl As IMyControlInterface


Protected Overrides Sub OnParentChanged(ByVal e As System.EventArgs)

If MyBase.Parent IsNot Nothing Then

Me.MyParentControl = TryCast(MyBase.Parent, IMyControlInterface)

If Me.MyParentControl Is Nothing Then

Throw New Exception("Control can only be added to an object that implements IMyControlInterface.")

End If
End If
MyBase.OnParentChanged(e)
End Sub

End Class

Public Class MyControlPanel
Inherits Panel
Implements IMyControlInterface


'...
End Class

This is basically the same as the original code snippets above, except that I added inheritance for class MyControl (as Control) and included a blank implementation of IMyControlInterface (as it is, so far, in my own code). I still get the same results: dragging MyControl to Form1 invokes the exception, and dragging it to MyControlPanel1 on Form1 does not.

Perhaps Manfred's answer is correct, although I do not pretend to understand its reasoning. I have also responded to that answer, to see if Manfred (or anyone else) can offer an alternative...