Reference dynamically created control by name

G

Guest

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)
 
C

Cor Ligthert

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
 
G

Guest

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)
 
C

Cor Ligthert

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
 
G

Guest

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?
 
C

Cor Ligthert

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top