Dynamic User Controls, Custom Properties
Here is a copy of my user control:
Public Class TextBox
Inherits System.Windows.Forms.UserControl
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Private components As System.ComponentModel.IContainer
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox()
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(10, 10)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = ""
'
'TextBox
'
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.TextBox1})
Me.Name = "TextBox"
Me.Size = New System.Drawing.Size(120, 40)
Me.ResumeLayout(False)
End Sub
Private bolValRequired As Boolean
Public Overrides Property Text() As String
Get
Return TextBox1.Text
End Get
Set(ByVal strValue As String)
TextBox1.Text = strValue
End Set
End Property
Public Property ValRequired() As Boolean
Get
Return bolValRequired
End Get
Set(ByVal bolValue As Boolean)
bolValRequired = bolValue
If bolValue Then
Me.TextBox1.BackColor = System.Drawing.Color.LightPink
End If
End Set
End Property
End Class
And here is how I create the User Control on another Form:
Dim TextBox As OOApp.TextBox = New OOApp.TextBox()
TextBox.Location = New System.Drawing.Point(20, 20)
TextBox.Name = "TextBox1"
TextBox.TabIndex = 1
TextBox.ValRequired = True
Me.Controls.Add(TextBox)
Here is how I talk to the User Control:
Dim objControl As Control
For Each objControl In Me.Controls
MsgBox(objControl.Name & " " & objControl.Text)
Next
I can access objControl.Text. What I cant do is request objControl.ValRequired since its not a known property. I need some sort of objControl.GetProperty("ValRequired"), but I dont see that. Any ideas?
Also it would be nice to access the control by name "TextBox1". Me.Controls("TextBox1") will not work since it just accepts integers. Thanks for any suggestions you can provide.
Matthew Krzan,
(NT4 & 2k) MCP, MCSE
(NT4) MCP+I, MCSE+I
MCDBA
ACISS Systems, Inc.

