Casting a string to a user control

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kto
    New Member
    • Sep 2007
    • 6

    Casting a string to a user control

    I have user controls named as userCtrlxx where xx is a two-digit number. I'm adding these controls dynamically based on certain strings that are also two-digit numbers parsed from strings. I've tried using CType and DirectCast but no lucks so far.
    Has anyone done things like that? Any suggestions? Thank you in advance.

    Sample codes:
    ...
    Dim userCtrl As UserControl

    'Adding based upon a certain 2-digit string
    userCtrl = DirectCast("use rCtrl" & 2DigitString, UserControl)
    Me.PanelBox.Con trols.Add(userC trl)

    'Adding using hard-coded name of user control
    userCtrl = New userCtrl20
    Me.PanelBox.Con trols.Add(userC trl)

    I'm using VB Express 2005 on WinXP.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Just set the ID property of your control to that string, then you will be able to search and find them.

    userCtrl.ID = "userCtrl" & 2DigitString
    Me.PanelBox.Con trols.Add(userC trl)

    Comment

    • kto
      New Member
      • Sep 2007
      • 6

      #3
      Thanks Plater for the response. However, I did not find an ID property for the User Control. Did you mean its tag?

      Thanks.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        If this is a windows application I think you would use "Name" oopsy.
        Just change where I put "ID" to "Name"

        Comment

        • kto
          New Member
          • Sep 2007
          • 6

          #5
          Using the Name property would make sense. However, I will still need to instantiate it as a New type of user control as in my samples for the hard-coded method. Do you have any pointers how to accomplish that? Thanks

          i.e.
          This did not work
          Dim tCtrl as UserControl
          tCtrl.Name = "userCtrl" & 2DigitString
          userCtrl = New tCtrl
          Me.PanelBox.Con trols.Add(userC trl)

          How about a way to get all user controls so that I can loop thru and find a matched one? Any ideas?

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Ah.
            [code=vb]
            Dim tCtrl as UserControl
            tCtrl = New UserControl()
            tCtrl.Name = "userCtrl" & 2DigitString
            Me.PanelBox.Con trols.Add(tCtrl )
            [/code]

            Unless you are trying to get the class name by string (which I am starting to think that you are) and not the object name.
            There is something like "reflection " that you can use, although I am entirely clueless on that subject.

            Comment

            • kto
              New Member
              • Sep 2007
              • 6

              #7
              Already tried that after my response earlier but did not work. May have to learn about the reflection that you're talking about. Thanks

              Comment

              Working...