472,371 Members | 1,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 4909
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
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
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
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
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
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
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
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
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
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...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

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.