Accessing dynamic controls by name?

A

Anil Gupte

How does one access dynamic controls by name (or wahtever 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

TextBoxSlice1BeginH.Text = x

End Sub


Thanx,
 
M

Morten Wennevik

Hi Anil,

You don't use the name. sender is a reference to your control so your
code would translate to something like this (may contain typos)

Dim tb as TextBox = CType(sender, TextBox)
tb.Text =

Dim x As String

x = tb.Text

If(x > 60)
x=x/2
End if

tb.Text = x
 
M

Morten Wennevik

Well, ignore the tb.Text = line. Forgot to proofread it.

I might add that it is possible to locate your control by name, but
TextBoxSlice1BeginH is just a name for the reference to the TextBox and
not a name of the TextBox itself. This name is forgotten in the code once
the method is finished.

If you set a name on the TextBox like you do on the Button you can loop
through all controls to find the one with the correct name on it. This
page will show you how.

http://steveorr.net/faq/ControlTreeRecursion.aspx

Finally, you have posted your question in the CSharp newsgroup, yet your
code is VB.Net so I assumed you wanted a VB.Net answer, but for future
reference you might want to go to the vb group
(microsoft.public.dotnet.languages.vb) if you want the answers to be in
VB.Net
 
A

Anil Gupte

Thanx, yes, I just realized I posted to the wrong group, but I appreciate
the response.

I am not sure what

Dim tb as TextBox = CType(sender, TextBox)

would do. Are you saying I need to put that line in the handler routine? I
can't do that because the textboxes (actually there are several but I
simplified to ask the question) are actually created with or even before the
buton.

Thanx again,
--
Anil Gupte
www.keeninc.net
www.icinema.com

Well, ignore the tb.Text = line. Forgot to proofread it.

I might add that it is possible to locate your control by name, but
TextBoxSlice1BeginH is just a name for the reference to the TextBox and
not a name of the TextBox itself. This name is forgotten in the code once
the method is finished.

If you set a name on the TextBox like you do on the Button you can loop
through all controls to find the one with the correct name on it. This
page will show you how.

http://steveorr.net/faq/ControlTreeRecursion.aspx

Finally, you have posted your question in the CSharp newsgroup, yet your
code is VB.Net so I assumed you wanted a VB.Net answer, but for future
reference you might want to go to the vb group
(microsoft.public.dotnet.languages.vb) if you want the answers to be in
VB.Net
 
A

Anil Gupte

Below is copied from the VB forum....

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.
 
M

Morten Wennevik

'sender' is of type 'object' when you get it, but we know that it is
actually a TextBox (unless you are using this event for other controls as
well). To be able to access the Text property we first need to cast the
'sender' reference from type 'object' to type 'TextBox', which is what
CType(sender, TextBox) does.

An alternativ if you use this event method for various types of controls
is to cast 'sender' to 'Control' as Control has the Text property as well.

Dim ct as Control = CType(sender, Control)
 

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