Picking the right control

M

Michel Vanderbeke

Hello,

I have a set of textboxes and labels, created by

Dim txtSign as TextBox
dim lblSign as Label

Do
txtSign = New Textbox
lblSign = New Label
Loop until ...

AddHandler txtSign.TextChanged, AddressOf Sign_TextboxChanged

In the Private Sub Sign_TextboxChanged, I want to determine in which
TextBox the Change took place, so it can affect the corresponding Label.

Any idea on how to achieve this?

Many thanks and greetings,

Michel
 
R

Robin Tucker

Michel,

If you don't want to go down the route of deriving a class with a new
property, you can use the "Tag" property of the textbox to associate it with
a label. The example below isn't exactly as you require because I'm not
dynamically creating the control, but you may see the concept. In "New" I
add a reference to each label into the tag property of the box. When the
box text changes, I retrieve the label by casting the tag property of the
text box. This example requires two text boxes and two labels on a form
(Label1, Label2, TextBox1, TextBox2).


Robin




Public Class Form1

Public Sub New()

' This call is required by the Windows Form Designer.

InitializeComponent()

' Add any initialization after the InitializeComponent() call.

Me.TextBox1.Tag = Me.Label1

Me.TextBox2.Tag = Me.Label2

End Sub

Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox1.TextChanged

Dim theLabel As Label = CType(TextBox1.Tag, Label)

theLabel.Text = TextBox1.Text

End Sub

Private Sub TextBox2_TextChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox2.TextChanged

Dim theLabel As Label = CType(TextBox2.Tag, Label)

theLabel.Text = TextBox2.Text

End Sub

End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top