Connecting Tech Pros Worldwide Help | Site Map

Reference dynamically created control by name

mef526
Guest
 
Posts: n/a
#1: Nov 21 '05
I would like to reference a dynamically created control and I know the name.
I would like to use the following:

Dim strName as String = "txtControl1"
' This is the ".Name" used when textbox was dynamically created

dim c as control = me.Controls(strName)

Instead I have to do this:

Public Function ControlByName _
(ByVal strName As String _
, ByVal ctrlCol As Windows.Forms.Control.ControlCollection _
) As Control
If ctrlCol Is Nothing Then Return Nothing
For Each c As Control In ctrlCol
If c.Name = strName Then Return c
Next
Return Nothing
End Function

dim c as control = ControlByName(str, Me.Controls)


Isn't there a better way???

+++++++++++++++++++++++++++++++++++++++++++++++

Next Item:

When cloning a control I do something like this:

For Each c0 As Control In t0.Controls
Dim c As System.Windows.Forms.Control
Select Case c0.GetType.ToString
Case "System.Windows.Forms.TextBox"
c = New TextBox
CType(c, TextBox).TextAlign = CType(c0, TextBox).TextAlign
strText = c.Text
Case "System.Windows.Forms.Label"
c = New Label
CType(c, Label).TextAlign = CType(c0, Label).TextAlign
strText = c.Text
Case "System.Windows.Forms.ComboBox"
c = New ComboBox
For Each p As Object In CType(c0, ComboBox).Items
CType(c, ComboBox).Items.Add(p)
Next
CType(c, ComboBox).SelectedItem = CType(c0, ComboBox).SelectedItem
strText = CStr(CType(c0, ComboBox).SelectedItem)
End Select
c.Visible = True 'c0.Visible '<- BUG IN CONTROL
Next

Note the last line: I have to set ".Visible" to true since the c0 has the
wrong value as retreived from the control collection. Is this a bug or is it
something I'm doing???

Is it because ">Visible" is really
c0.Visible = (c0.Parent.Parent.Visible AND c0.Parent.Visible AND c0.Visible)

Cor Ligthert
Guest
 
Posts: n/a
#2: Nov 21 '05

re: Reference dynamically created control by name


Mef,

When you want this it is in my opinion easier to create a hashtable where
you place the name in the key and the reference to the control in the value.

http://msdn.microsoft.com/library/de...classtopic.asp

I hope this helps?

Cor

"mef526" <mef526@discussions.microsoft.com>
[color=blue]
>I would like to reference a dynamically created control and I know the
>name.
> I would like to use the following:
>
> Dim strName as String = "txtControl1"
> ' This is the ".Name" used when textbox was dynamically created
>
> dim c as control = me.Controls(strName)
>
> Instead I have to do this:
>
> Public Function ControlByName _
> (ByVal strName As String _
> , ByVal ctrlCol As Windows.Forms.Control.ControlCollection _
> ) As Control
> If ctrlCol Is Nothing Then Return Nothing
> For Each c As Control In ctrlCol
> If c.Name = strName Then Return c
> Next
> Return Nothing
> End Function
>
> dim c as control = ControlByName(str, Me.Controls)
>
>
> Isn't there a better way???
>
> +++++++++++++++++++++++++++++++++++++++++++++++
>
> Next Item:
>
> When cloning a control I do something like this:
>
> For Each c0 As Control In t0.Controls
> Dim c As System.Windows.Forms.Control
> Select Case c0.GetType.ToString
> Case "System.Windows.Forms.TextBox"
> c = New TextBox
> CType(c, TextBox).TextAlign = CType(c0, TextBox).TextAlign
> strText = c.Text
> Case "System.Windows.Forms.Label"
> c = New Label
> CType(c, Label).TextAlign = CType(c0, Label).TextAlign
> strText = c.Text
> Case "System.Windows.Forms.ComboBox"
> c = New ComboBox
> For Each p As Object In CType(c0, ComboBox).Items
> CType(c, ComboBox).Items.Add(p)
> Next
> CType(c, ComboBox).SelectedItem = CType(c0, ComboBox).SelectedItem
> strText = CStr(CType(c0, ComboBox).SelectedItem)
> End Select
> c.Visible = True 'c0.Visible '<- BUG IN CONTROL
> Next
>
> Note the last line: I have to set ".Visible" to true since the c0 has the
> wrong value as retreived from the control collection. Is this a bug or is
> it
> something I'm doing???
>
> Is it because ">Visible" is really
> c0.Visible = (c0.Parent.Parent.Visible AND c0.Parent.Visible AND
> c0.Visible)
>[/color]


Herfried K. Wagner [MVP]
Guest
 
Posts: n/a
#3: Nov 21 '05

re: Reference dynamically created control by name


"mef526" <mef526@discussions.microsoft.com> schrieb:[color=blue]
> I would like to reference a dynamically created control
> and I know the name.[/color]

Accessing controls by their names or indices
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=controlbynameindex&lang=en>

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>


Cor Ligthert
Guest
 
Posts: n/a
#4: Nov 21 '05

re: Reference dynamically created control by name


Herfried,

That recursive method is I thought one which I created and showed the first
time in this newsgroups in this way (it is a little bit changed however not
basicly).

I avoided that expresly in this case because with dynamicly created controls
the hasttable is in my opinion better when the controls are not already
placed in an array of controls ( as I do because in that case because I can
add the handlers as well in a loop).

In that case it is finding the name just looping through that array.

Cor

"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at>[color=blue]
> "mef526" <mef526@discussions.microsoft.com> schrieb:[color=green]
>> I would like to reference a dynamically created control
>> and I know the name.[/color]
>
> Accessing controls by their names or indices
> <URL:http://dotnet.mvps.org/dotnet/faqs/?id=controlbynameindex&lang=en>
>
> --
> Herfried K. Wagner [MVP]
> <URL:http://dotnet.mvps.org/>
>
>[/color]


mef526
Guest
 
Posts: n/a
#5: Nov 21 '05

re: Reference dynamically created control by name


Thank you for your answer

I think that the method I am now using will work best given that the
ControlContainer in the code, a TabPage, only has 5 items. It is dynamically
created as a clone of the selected TabPage.

What about Item 2:
Note the last line: I have to set ".Visible" to true since the c0 has the
wrong value as retreived from the control collection. Is this a bug or is it
something I'm doing???

Is it because ".Visible" is really
c0.Visible = (c0.Parent.Parent.Visible AND c0.Parent.Visible AND c0.Visible)


"Cor Ligthert" wrote:
[color=blue]
> Herfried,
>
> That recursive method is I thought one which I created and showed the first
> time in this newsgroups in this way (it is a little bit changed however not
> basicly).
>
> I avoided that expresly in this case because with dynamicly created controls
> the hasttable is in my opinion better when the controls are not already
> placed in an array of controls ( as I do because in that case because I can
> add the handlers as well in a loop).
>
> In that case it is finding the name just looping through that array.
>
> Cor
>
> "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at>[color=green]
> > "mef526" <mef526@discussions.microsoft.com> schrieb:[color=darkred]
> >> I would like to reference a dynamically created control
> >> and I know the name.[/color]
> >
> > Accessing controls by their names or indices
> > <URL:http://dotnet.mvps.org/dotnet/faqs/?id=controlbynameindex&lang=en>
> >
> > --
> > Herfried K. Wagner [MVP]
> > <URL:http://dotnet.mvps.org/>
> >
> >[/color]
>
>
>[/color]
Cor Ligthert
Guest
 
Posts: n/a
#6: Nov 21 '05

re: Reference dynamically created control by name


Mef526

When I understand your message right, than I think that I have nothing to
disagree with you. (You can set an "exit for", when you think on more
controls than 5 when found, which needs a little bit change of the "then"
clause). I do not like that inline thens so maybe that is the reason I do
that directly.

:-)

Cor


"mef526" <mef526@discussions.microsoft.com>
[color=blue]
> Thank you for your answer
>
> I think that the method I am now using will work best given that the
> ControlContainer in the code, a TabPage, only has 5 items. It is
> dynamically
> created as a clone of the selected TabPage.
>
> What about Item 2:
> Note the last line: I have to set ".Visible" to true since the c0 has the
> wrong value as retreived from the control collection. Is this a bug or is
> it
> something I'm doing???
>
> Is it because ".Visible" is really
> c0.Visible = (c0.Parent.Parent.Visible AND c0.Parent.Visible AND
> c0.Visible)
>
>
> "Cor Ligthert" wrote:
>[color=green]
>> Herfried,
>>
>> That recursive method is I thought one which I created and showed the
>> first
>> time in this newsgroups in this way (it is a little bit changed however
>> not
>> basicly).
>>
>> I avoided that expresly in this case because with dynamicly created
>> controls
>> the hasttable is in my opinion better when the controls are not already
>> placed in an array of controls ( as I do because in that case because I
>> can
>> add the handlers as well in a loop).
>>
>> In that case it is finding the name just looping through that array.
>>
>> Cor
>>
>> "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at>[color=darkred]
>> > "mef526" <mef526@discussions.microsoft.com> schrieb:
>> >> I would like to reference a dynamically created control
>> >> and I know the name.
>> >
>> > Accessing controls by their names or indices
>> > <URL:http://dotnet.mvps.org/dotnet/faqs/?id=controlbynameindex&lang=en>
>> >
>> > --
>> > Herfried K. Wagner [MVP]
>> > <URL:http://dotnet.mvps.org/>
>> >
>> >[/color]
>>
>>
>>[/color][/color]


mef526
Guest
 
Posts: n/a
#7: Nov 21 '05

re: Reference dynamically created control by name


I'm not sure I understand your reply so let em explain my thought- the
COntrolCOnainer that is serched only HAS 5 items in it so the search would
not be too bad, probably this is FASTER than using a hash, since there are so
few items.

What about the .Visible property?

Is it read differently than set?

"Cor Ligthert" wrote:
[color=blue]
> Mef526
>
> When I understand your message right, than I think that I have nothing to
> disagree with you. (You can set an "exit for", when you think on more
> controls than 5 when found, which needs a little bit change of the "then"
> clause). I do not like that inline thens so maybe that is the reason I do
> that directly.
>
> :-)
>
> Cor
>
>
> "mef526" <mef526@discussions.microsoft.com>
>[color=green]
> > Thank you for your answer
> >
> > I think that the method I am now using will work best given that the
> > ControlContainer in the code, a TabPage, only has 5 items. It is
> > dynamically
> > created as a clone of the selected TabPage.
> >
> > What about Item 2:
> > Note the last line: I have to set ".Visible" to true since the c0 has the
> > wrong value as retreived from the control collection. Is this a bug or is
> > it
> > something I'm doing???
> >
> > Is it because ".Visible" is really
> > c0.Visible = (c0.Parent.Parent.Visible AND c0.Parent.Visible AND
> > c0.Visible)
> >
> >
> > "Cor Ligthert" wrote:
> >[color=darkred]
> >> Herfried,
> >>
> >> That recursive method is I thought one which I created and showed the
> >> first
> >> time in this newsgroups in this way (it is a little bit changed however
> >> not
> >> basicly).
> >>
> >> I avoided that expresly in this case because with dynamicly created
> >> controls
> >> the hasttable is in my opinion better when the controls are not already
> >> placed in an array of controls ( as I do because in that case because I
> >> can
> >> add the handlers as well in a loop).
> >>
> >> In that case it is finding the name just looping through that array.
> >>
> >> Cor
> >>
> >> "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at>
> >> > "mef526" <mef526@discussions.microsoft.com> schrieb:
> >> >> I would like to reference a dynamically created control
> >> >> and I know the name.
> >> >
> >> > Accessing controls by their names or indices
> >> > <URL:http://dotnet.mvps.org/dotnet/faqs/?id=controlbynameindex&lang=en>
> >> >
> >> > --
> >> > Herfried K. Wagner [MVP]
> >> > <URL:http://dotnet.mvps.org/>
> >> >
> >> >
> >>
> >>
> >>[/color][/color]
>
>
>[/color]
Cor Ligthert
Guest
 
Posts: n/a
#8: Nov 21 '05

re: Reference dynamically created control by name


Mef,

That performance is where I did mean that I cannot disagree with that when
we are talking about 5 items.

However it did seem to me a kind of generic function and therefore I added
something more..

Than to complete it to get it about the visible property.

You made it in my opinion as a generic search function . Normally this would
be done as this.
\\\
For Each ctr As Control In ctrlPar.controls
If ctr.Name = strName Then
ctr.visible = false
exit for
end if
Next
///

Using your functions it can be something as
\\\\
dim c as control = ControlByName("mycontrolname",Panel1.controls)
if Not c Is Nothing then
c.visible = false
end if
////

All is typed in this message so watch typos.

I hope this helps?

Cor

"mef526" <mef526@discussions.microsoft.com>
[color=blue]
> I'm not sure I understand your reply so let em explain my thought- the
> COntrolCOnainer that is serched only HAS 5 items in it so the search would
> not be too bad, probably this is FASTER than using a hash, since there are
> so
> few items.
>
> What about the .Visible property?
>
> Is it read differently than set?
>
> "Cor Ligthert" wrote:
>[color=green]
>> Mef526
>>
>> When I understand your message right, than I think that I have nothing to
>> disagree with you. (You can set an "exit for", when you think on more
>> controls than 5 when found, which needs a little bit change of the "then"
>> clause). I do not like that inline thens so maybe that is the reason I do
>> that directly.
>>
>> :-)
>>
>> Cor
>>
>>
>> "mef526" <mef526@discussions.microsoft.com>
>>[color=darkred]
>> > Thank you for your answer
>> >
>> > I think that the method I am now using will work best given that the
>> > ControlContainer in the code, a TabPage, only has 5 items. It is
>> > dynamically
>> > created as a clone of the selected TabPage.
>> >
>> > What about Item 2:
>> > Note the last line: I have to set ".Visible" to true since the c0 has
>> > the
>> > wrong value as retreived from the control collection. Is this a bug or
>> > is
>> > it
>> > something I'm doing???
>> >
>> > Is it because ".Visible" is really
>> > c0.Visible = (c0.Parent.Parent.Visible AND c0.Parent.Visible AND
>> > c0.Visible)
>> >
>> >
>> > "Cor Ligthert" wrote:
>> >
>> >> Herfried,
>> >>
>> >> That recursive method is I thought one which I created and showed the
>> >> first
>> >> time in this newsgroups in this way (it is a little bit changed
>> >> however
>> >> not
>> >> basicly).
>> >>
>> >> I avoided that expresly in this case because with dynamicly created
>> >> controls
>> >> the hasttable is in my opinion better when the controls are not
>> >> already
>> >> placed in an array of controls ( as I do because in that case because
>> >> I
>> >> can
>> >> add the handlers as well in a loop).
>> >>
>> >> In that case it is finding the name just looping through that array.
>> >>
>> >> Cor
>> >>
>> >> "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at>
>> >> > "mef526" <mef526@discussions.microsoft.com> schrieb:
>> >> >> I would like to reference a dynamically created control
>> >> >> and I know the name.
>> >> >
>> >> > Accessing controls by their names or indices
>> >> > <URL:http://dotnet.mvps.org/dotnet/faqs/?id=controlbynameindex&lang=en>
>> >> >
>> >> > --
>> >> > Herfried K. Wagner [MVP]
>> >> > <URL:http://dotnet.mvps.org/>
>> >> >
>> >> >
>> >>
>> >>
>> >>[/color]
>>
>>
>>[/color][/color]


Closed Thread