Home All Groups Group Topic Archive Search About

Access dynamic controls by name?

Author
30 Oct 2006 3:51 PM
Anil Gupte
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,




Author
30 Oct 2006 3:54 PM
Marina Levit [MVP]
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.

Show quoteHide quote
"Anil Gupte" <anil-l***@icinema.com> wrote in message
news:edQSVtD$GHA.2300@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
>
>
Author
30 Oct 2006 4:08 PM
Anil Gupte
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.
Show quoteHide quote
"Marina Levit [MVP]" <someone@nospam.com> wrote in message
news:eftSyuD$GHA.748@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" <anil-l***@icinema.com> wrote in message
> news:edQSVtD$GHA.2300@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
>>
>>
>
>
Author
30 Oct 2006 4:34 PM
DarrenV
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:
Show quoteHide quote
> 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
Author
30 Oct 2006 5:37 PM
Cor Ligthert [MVP]
Anil,

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

http://www.vb-tips.com/dbpages.aspx?ID=e7510e67-92f3-49f6-b902-03abce36aa60

I hope this helps,

Cor

Show quoteHide quote
"Anil Gupte" <anil-l***@icinema.com> schreef in bericht
news:edQSVtD$GHA.2300@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
>
>
Author
31 Oct 2006 6:48 AM
Anil Gupte
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.
Show quoteHide quote
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
news:uLsuKnE$GHA.4708@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?ID=e7510e67-92f3-49f6-b902-03abce36aa60
>
> I hope this helps,
>
> Cor
>
> "Anil Gupte" <anil-l***@icinema.com> schreef in bericht
> news:edQSVtD$GHA.2300@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
>>
>>
>
>