I am trying to make a UserControl or Component which can be dragged from the Toolbox onto a container control such as a Form or a Panel. Its purpose is to change the behaviour of that container. In particular, I would like to add functions to the container's painting and mouse actions. I DO NOT want to have to write code for the container itself -- that's the whole point of the Toolbox object.
I can do this by getting a reference to the object's Parent (or Host in the case of a Component) and handling its events, as this example illustrates:
However, I have heard that handling the events of a parent control conflicts with OOP principles. So I would like to know, does that objection apply in a case like my example? If so, what are the drawbacks? And what alternative techniques are available?vb Code:
Public Class DeformablePolygon Inherits Windows.Forms.UserControl Private WithEvents _Parent As Windows.Forms.Control Public Property VertexCollection As System.Drawing.PointF() Protected Overrides Sub OnParentChanged(e As System.EventArgs) _Parent = Me.Parent MyBase.OnParentChanged(e) End Sub Private Sub _Parent_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles _Parent.Paint If _VertexCollection IsNot Nothing Then e.Graphics.DrawPolygon(System.Drawing.Pens.Black, _VertexCollection) End If End Sub '+ mouse events etc. End Class
BB


Reply With Quote

. I'm still open to other comments, so I won't mark this resolved yet. BB
