Hi all,
(I hope I am in the right forum)
I am working on a custom Control. I would like to initialize certain properties. I've done it various ways, and can successfully initialize the properties several ways; however, there is a slight problem.
[u]The Problem</u>
This only happens ONCE after the BUILD. I drop the control on the form, and it sets its initial property values fine. Then I adjust those values. Then I go into debug mode. The debug-mode control is correct, but the design-mode control
re-initializes itself. I repeat, this behavior only happens once after the build.
[u]My question is this:</u>
Is there a standard way of initializing property values for custom controls for design-mode? For instance, say you want the custom control's Height property initialized at 50 when dropped on the form. How would you do that?
Just in case anyone cares, below is the code I'm using. It's nothing special: its something I whipped up trying to isolate this and a couple of other issues I was having.
Code:
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Drawing
Public Class UserControl1
Private WithEvents myContainer As Control
Private isInitialized As Boolean
Private defaultValuesAreSet As Boolean
Private initHeight As Integer = 50
<DefaultValue(False)> _
Public Property IsInit() As Boolean
Get
Return Me.isInitialized
End Get
Set(ByVal value As Boolean)
Me.isInitialized = value
End Set
End Property
Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
If Not Me.isInitialized Then
Me.defaultValuesAreSet = False
Else
Me.defaultValuesAreSet = True
End If
End Sub
Protected Overrides Sub OnPaint( _
ByVal e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
If Me.isInitialized Then
If Me.DesignMode And Not Me.defaultValuesAreSet Then
Me.Height = Me.initHeight
Me.defaultValuesAreSet = True
End If
Me.Top = 0
Me.Left = 0
Me.Width = Me.Parent.ClientRectangle.Width
Me.BackColor = Color.Red
End If
End Sub
Protected Overrides Sub InitLayout()
MyBase.InitLayout()
Me.isInitialized = True
Me.myContainer = Me.Parent
End Sub
Private Sub myContainer_SizeChanged( _
ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles myContainer.SizeChanged
Me.Refresh()
End Sub
End Class
Thanks all,
--William