Access dynamic controls by name?

A

Anil Gupte

How does one access dynamic controls by name (or whatever other means)? I
have the following:

Dim newbtnPick As New Button
newbtnPick.Name = "SliceButton" & CurSliceNum
newbtnPick.Location = New Point(SILoc.SIPickbtn, SILoc.SIY3)
newbtnPick.Size = New Size(SILoc.btnW, SILoc.btnH)
newbtnPick.Text = "Pick->"
Sender.Controls.Add(newbtnPick)
AddHandler newbtnPick.Click, AddressOf newbtnPick_Click
Dim TextBoxSlice1BeginH As New TextBox
TextBoxSlice1BeginH.Location = New Point(SILoc.SIX0, SILoc.SIY3)
TextBoxSlice1BeginH.Size = New Size(SILoc.TextBoxSizeW, SILoc.TextBoxSizeH)
Sender.Controls.Add(TextBoxSlice1BeginH)

Now in the handler how do I address the textbox?:

Private Sub newbtnPick_Click(ByVal sender As Object, ByVal e As EventArgs)
' How do I dosomething like this:
dim x as string
x = TextBoxSlice1BeginH.Text
if x > 60
x=x/2
end if
TextBoxSlice1BeginH.Text = x
End Sub

Is there a web based tutorial or article that deals with this?

Thanx,
 
M

Marina Levit [MVP]

The sender is the object that raised the event. Cast it to the appropriate
type.

You can't reference the control unless the variable for it is declared at
the class level. If you declare a reference to it in one method, then you
cannot access that variable in another.
 
A

Anil Gupte

Thanx, that did it! I moved the Dim statements outside the method (which
was the New method i.e. constructor for this class) and left the rest inside
the method. Now I can access the textbox(es) in the event handler for the
button.

Great! Appreciate the help.
 
D

DarrenV

Hi Anil,

You could set the tag property on the button to be the textbox. Then
in your click handler you can cast the sender to a button and then
again cast the tag to the textbox. This aproach allows you to have
many pairs of Button/Textbox.

Dim newbtnPick As New Button
newbtnPick.Name = "SliceButton" & CurSliceNum
newbtnPick.Location = New Point(SILoc.SIPickbtn, SILoc.SIY3)
newbtnPick.Size = New Size(SILoc.btnW, SILoc.btnH)
newbtnPick.Text = "Pick->"
Sender.Controls.Add(newbtnPick)
AddHandler newbtnPick.Click, AddressOf newbtnPick_Click
Dim TextBoxSlice1BeginH As New TextBox
TextBoxSlice1BeginH.Location = New Point(SILoc.SIX0, SILoc.SIY3)
TextBoxSlice1BeginH.Size = New Size(SILoc.TextBoxSizeW,
SILoc.TextBoxSizeH)
Sender.Controls.Add(TextBoxSlice1BeginH)
' Set the tag property of the button
newbtnPick.Tag = TextBoxSlice1BeginH

Private Sub newbtnPick_Click(ByVal sender As Object, ByVal e As
EventArgs)
' Cast the sender, which will be the button control
Dim sliceTextBox as TextBox = DirectCast(DirectCast(sender,
Button).Tag, TextBox)

' Then you can do the following:
dim x as string
x = sliceTextBox.Text
if x > 60
x=x/2
end if
sliceTextBox.Text = x
End Sub

You could always write a simple composite control with a button and
textbox on.

Good luck,
Regards
Darren
 
A

Anil Gupte

Thanx for the code sample - I learned something new (also from Darren in the
previous message) that there is a Tag property on a control. I wonder if I
can address the control using that. It would be very useful to address the
control using the tag later, after leaving the creation code. For example,
based on its value and the value of the next set of controls, I may want to
relocate it on the form.
 

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

Similar Threads


Top