Custom Deserialization

Posted on 2004-11-18
Last Modified: 2010-04-23
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?

Question by:rdavis101

    Author Comment

    Geez...I made a mistake in the question. Should just say "I have four strings..."  Forget about where they come from.
    LVL 85

    Accepted Solution

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    Article by: jpaulino
    XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String ( Literal, only instead of starting and ending with w…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now