473,511 Members | 15,131 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Access dynamic controls by name?

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
5 4978
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3492
by: Bil Muh | last post by:
Esteemede Developers, I would like to Thank All of You in advance for your sincere guidances. I am developing a software using Visual C++ .NET Standard Edition with Windows Form (.NET)...
3
3684
by: CSDunn | last post by:
Hello, I have a situation with MS Access 2000 in which I need to display report data in spreadsheet orientation (much like a datasheet view for a form). If you think of the report in terms of what...
49
14282
by: Yannick Turgeon | last post by:
Hello, We are in the process of examining our current main application. We have to do some major changes and, in the process, are questionning/validating the use of MS Access as front-end. The...
3
6799
by: MikeY | last post by:
Hi Everyone, I am working in C#, windows forms.My question is this. All my button dynamic controls properties are present and accounted for except for the"FlatStyle" properties. I can't seem to...
1
7536
by: Shourie | last post by:
I've noticed that none of the child controls events are firing for the first time from the dynamic user control. Here is the event cycle. 1) MainPage_load 2) User control1_Load user clicks a...
1
3136
by: sleigh | last post by:
Hello, I'm building a web application that will build a dynamic form based upon questions in a database. This form will have several different sections that consist of a panel containing one to...
4
3285
by: Venus | last post by:
Hello, Thanks for your reply. I understand that a control can be created dynamically in several ways: 1) using StringBuilder 2) using Controls.Add 3) using ASP PlaceHolder But this is just...
0
1447
by: Venus | last post by:
Hello, After trying some ways to do it I wanted to use something like the code below but for some reason is not working (I have to generate the entire form dynamically (not only the controls)):...
3
3948
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...
0
7137
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
7349
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
7417
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...
1
7074
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5063
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4734
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3219
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3210
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.