By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,234 Members | 1,869 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,234 IT Pros & Developers. It's quick & easy.

Access dynamic controls by name?

P: n/a
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,

--
Anil Gupte
www.keeninc.net
www.icinema.com
Oct 30 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.

"Anil Gupte" <an*******@icinema.comwrote in message
news:ed**************@TK2MSFTNGP04.phx.gbl...
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,

--
Anil Gupte
www.keeninc.net
www.icinema.com


Oct 30 '06 #2

P: n/a
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

Anil Gupte wrote:
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,

--
Anil Gupte
www.keeninc.net
www.icinema.com
Oct 30 '06 #3

P: n/a
Anil,

Is this simple enough, there are as well more advanced ones on our website.

http://www.vb-tips.com/dbpages.aspx?...2-03abce36aa60

I hope this helps,

Cor

"Anil Gupte" <an*******@icinema.comschreef in bericht
news:ed**************@TK2MSFTNGP04.phx.gbl...
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,

--
Anil Gupte
www.keeninc.net
www.icinema.com


Oct 30 '06 #4

P: n/a
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.
--
Anil Gupte
www.keeninc.net
www.icinema.com

"Marina Levit [MVP]" <so*****@nospam.comwrote in message
news:ef*************@TK2MSFTNGP02.phx.gbl...
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.

"Anil Gupte" <an*******@icinema.comwrote in message
news:ed**************@TK2MSFTNGP04.phx.gbl...
>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,

--
Anil Gupte
www.keeninc.net
www.icinema.com



Oct 31 '06 #5

P: n/a
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.
--
Anil Gupte
www.keeninc.net
www.icinema.com

"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:uL**************@TK2MSFTNGP05.phx.gbl...
Anil,

Is this simple enough, there are as well more advanced ones on our
website.

http://www.vb-tips.com/dbpages.aspx?...2-03abce36aa60

I hope this helps,

Cor

"Anil Gupte" <an*******@icinema.comschreef in bericht
news:ed**************@TK2MSFTNGP04.phx.gbl...
>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,

--
Anil Gupte
www.keeninc.net
www.icinema.com



Oct 31 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.