473,388 Members | 1,408 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Accessing dynamic controls by name?

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,

--
Anil Gupte
www.keeninc.net
www.icinema.com
Oct 30 '06 #1
5 2049
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

On Mon, 30 Oct 2006 08:39:18 +0100, Anil Gupte <an*******@icinema.com
wrote:
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,


--
Happy Coding!
Morten Wennevik [C# MVP]
Oct 30 '06 #2
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

On Mon, 30 Oct 2006 08:48:33 +0100, Morten Wennevik
<Mo************@hotmail.comwrote:
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

On Mon, 30 Oct 2006 08:39:18 +0100, Anil Gupte <an*******@icinema.com>
wrote:
>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,




--
Happy Coding!
Morten Wennevik [C# MVP]
Oct 30 '06 #3
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

"Morten Wennevik" <Mo************@hotmail.comwrote in message
news:op***************@tr024.bouvet.no...
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

On Mon, 30 Oct 2006 08:48:33 +0100, Morten Wennevik
<Mo************@hotmail.comwrote:
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

On Mon, 30 Oct 2006 08:39:18 +0100, Anil Gupte <an*******@icinema.com>
wrote:
>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,




--
Happy Coding!
Morten Wennevik [C# MVP]
Oct 30 '06 #4
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.
--
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 30 '06 #5
'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)
On Mon, 30 Oct 2006 16:58:54 +0100, Anil Gupte <an*******@icinema.com
wrote:
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,


--
Happy Coding!
Morten Wennevik [C# MVP]
Oct 31 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jonathan Williams | last post by:
Hi, I have an object which inherits from WebControl (CUSTOM : WebControl) In this object I have code in which I add child contols: protected override void CreateChildControls() {...
3
by: Leo J. Hart IV | last post by:
OK, here's another question for the experts: I am building a multi-step (3 steps actually) form using a panel for each step and hiding/displaying the appropriate panel/panels depending on which...
3
by: Bijoy Naick | last post by:
Need help with inserting and accessing web controls and user controls dynamically. How can I programatically insert webcontrols and user controls based on data I get from a database? Also, how...
4
by: Paul W | last post by:
i have some dynamic controls that i add to a webform (actually, i dynamiclly build a table and add the controls to cells). some of these controls are HtmlSelect controls with the multiple property...
7
by: msdev | last post by:
Hello, I am creating my own webbrowser to learn VB .Net. I am stuck on an issue with regards to dynamically-created controls, in this case tabs on a tabcontrol and webbrowsers created within...
5
by: Anil Gupte | last post by:
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 =...
9
by: Tarscher | last post by:
hi all, I have this seemingly simple problem. I have lost a lot of time on it though. When a user selects a value from a dropdownlist (static control) a dynamic control is generated. I have...
3
by: Andreas Wöckl | last post by:
Hi Group! I have a web form that is created dynamically - so I create Textboxes, RadioButtonLists, CheckBoxLists and so on - I have found some articles that there may be some problems with...
2
by: bharathi228 | last post by:
my code for retrieving values from database Dim da As New SqlDataAdapter("select parameter_name,parameter_units from sys_params", con) If con.State = ConnectionState.Closed Then con.Open()...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.