|
I created a User Control under VB.net and I want to add the control dynamically. Thats all working. I get the control added and I have managed to access the standard properties, however I am not able to access the Custom Properties I have defined on my User Control. It has to do with the way I access the User Control.
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. | | MigrationUser 1 Saturday, March 15, 2003 4:20 AM | Several problems so far. First of all, you named your control instance TextBox, not TextBox1 (which is what it appears you think you named it, from later comments). I would STRONGLY suggest not naming the variable the same as the type, in Visual Basic. It makes it extremely hard to read the code, and on top of that, naming your class the same as a built-in type (and not even inheriting from the type) is confusing, as well.
You can't access the ValRequired property in your loop because the control you're using as the loop enumerator is a Control, not a OOApp.TextBox. In order to "see" that property, you would need to cast the enumerator as the correct type. This is no different than if you have a CheckedListBox on the form, and you want to see the CheckedItems property in such a loop--you wouldn't be able to, because your enumerator is a plain old Control.
If you want to loop through all the controls and take different action based on the type of the control, you'll need some type of control structure (maybe a Select Case) that determines the type of the enumerator, and then does what you need to do based on the type. Something like this:
Dim objControl As Control
For Each objControl In Me.Controls MessageBox.Show(objControl.GetType.FullName)
If TypeOf (objControl) Is OOApp.TextBox Then Dim x As OOApp.TextBox = DirectCast(objControl, OOApp.TextBox) MessageBox.Show(x.ValRequired)
ElseIf TypeOf (objControl) Is Button Then Dim x As Button = DirectCast(objControl, Button) ' Get some property that only buttons have... MessageBox.Show(x.DialogResult) End If Next
I would suggest reconsidering naming your control TextBox, or at least, rather than making it a user control, simply make a new class that inherits from TextBox, and add your properties to it. | | MigrationUser 1 Saturday, March 15, 2003 11:49 AM | Thanks Ken!
Matthew Krzan, (NT4 & 2k) MCP, MCSE (NT4) MCP+I, MCSE+I MCDBA ACISS Systems, Inc. | | MigrationUser 1 Saturday, March 15, 2003 6:42 PM | Is there anyway to reference the Control by name rather than a loop? Since its added programaticly. Thanks for your help.
Matthew Krzan, (NT4 & 2k) MCP, MCSE (NT4) MCP+I, MCSE+I MCDBA ACISS Systems, Inc. | | MigrationUser 1 Saturday, March 15, 2003 7:12 PM | Not via the built-in Controls collection. You can either maintain your own collection of controls yourself, or (since you put the controls on the form in code), maintain references to the controls you'll need. I think the point here is that if you put the controls there yourself, you have a reference to each control already. Why maintain another reference?
In other words, the Controls collection doesn't work the way you want. You'll need to either create one yourself, or use a different technique for referring to the controls. | | MigrationUser 1 Sunday, March 16, 2003 2:29 PM |
|