Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 155
  • Last Modified:

Custom Deserialization

I have four strings which I'm getting from...

1) The name of a control, say, "LabelA"
2) The name of several properties belonging to the control, say, "Text-Align" and "Text"
3) The value of each property...
4) The type of control...say "Label"

Using only the above, how can I quickly reference LabelA in order to assign it its properties? Somehow System.Reflection has to be used...but how?

1 Solution
rdavis101Author Commented:
Geez...I made a mistake in the question. Should just say "I have four strings..."  Forget about where they come from.
Mike TomlinsonMiddle School Assistant TeacherCommented:
If this is a WinForms app then you can iterate the controls collection until you find a control with a matching name:

    Private Sub Foo()
        Dim c As Control
        Dim cName As String = "LabelA"
        Dim lbl as Label
        c = getControlByName(cName)
        If Not (c Is Nothing) Then
                 lbl = CType(c, Label)
                 ' do something with lbl
                 lbl.Text = "something"
        End If
     End Sub

    Private Function getControlByName(ByVal controlName As String) As Control
        Dim i As Integer
        Dim ctl As Control

        controlName = controlName.ToLower
        For Each ctl In Me.Controls
            If ctl.Name.ToLower.Equals(controlName) Then
                Return ctl
            End If
        Next ctl
    End Function

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now