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]