I am just getting started in trying to create a custom Windows form control
in VB .NET. The premise is fairly simple. It's a "shape control" that
assumes the shape of its Background Image. The user specifies a Mask Color
and that color is rendered transparent, thus making the control the shape of
the image (in theory, anyway). I had a version of this working with VB6,
but now want to get my feet wet with .NET.
The problem is, I can't get the design-time behavior I need. When the user
places the control on a form or changes the background image, I want the
control to resize itself to the image.
Do I need to create an instance of the ControlDesigner class to do this, or
is there a simpler way? In either case, how would I do it?
If there is sample code or a decent article that covers these issues, please
point me to them. I don't want to waste your time explaining this stuff for
the umpteenth time. The Microsoft materials I've found don't seem to cover
this specific area. I'm sure there must be something out there, but I'm not
finding it.
TIA,
Dave
Dave Sours - 16 Aug 2004 23:04 GMT
OK, I'm making progress, I think.
It seems pretty clear that I do need a designer, and I'm trying to implement
one based on the example in the Help for ControlDesigner class.
Unfortunately, I don't see an event for my designer that relates to placing
the control on the form. I'm also not getting any event handlers in my
designer to trigger.
I'm obviously missing something. Here's a code snippet:
<DesignerAttribute(GetType(ShapeControlDesigner))> _
Public Class ShapeControl
Inherits System.Windows.Forms.UserControl
End Class
Public Class ShapeControlDesigner
Inherits System.Windows.Forms.Design.ControlDesigner
Public Sub New()
End Sub
' Sets a value and refreshes the control's display when the
' mouse position enters the area of the control.
Protected Overrides Sub OnMouseEnter()
ControlSize()
End Sub
Private Sub ControlSize()
If Not Me.Control.BackgroundImage Is Nothing Then
MyBase.Control.Size = MyBase.Control.BackgroundImage.Size
Me.Control.Size = MyBase.Control.BackgroundImage.Size
Me.Control.Refresh()
End If
End Sub
End Class