Results 1 to 6 of 6

Thread: How to Reference a control through a variable ?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2001
    Posts
    79

    Question How to Reference a control through a variable ?

    I am working on a small project and I need to reference controls that they are created during runtime. The name of the control is saved in a variable, is there a way to refer to this control.

    Example

    dim A as string
    dim B as textbox

    somewhere in code A="SOMENAME", this name is created randomly

    B= new textbox
    B.name=A

    pnlcontrols.controls.add(B)

    Later in the program I need to refer to the SOMENAME.text
    but the value SOMENAME is held in the variable A, A.text is not possible. Is there a way to do this. I need to replace A with the value of A at runtime.


    Thanks

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    The name isn't going to do much for ya. Why are you not just storing a reference to the control itself instead of its name?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2001
    Posts
    79
    That was an option but I tried to look up how it is done and could not find an easy way so I thought that it is not possible or very difficult to do.

    If you can guide me how to do this or where to look up in the help I will very much appreciate it.

    many thanks.

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    When you create the new textbox then you are probably already assigning it to a variable you just have to persist that variable instead of just the name.

    What does this mean 'somewhere in code A="SOMENAME", this name is created randomly'?

    Did you create a control/textbox in there somewhere or just a name?

    Post the code you are using to make the new textbox.

    Controls are objects like everything else so they already are variables and you can assign them to other variables too if you want.
    VB Code:
    1. Dim txt As New TextBox
    2. Dim a As TextBox
    3.  
    4. a=txt
    5. 'now both a and txt reference the same textbox instance

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2001
    Posts
    79
    Thanks for your help but I connot post the code as I am not at home.

    What I am doing is creating controls, actually combo boxes to be used with crystal and I give each the name of the crystal parameter.

    suppose parameter name was FromDate

    dim mycont as combo

    mycont.name= paramater.name

    pnlparameters.controls.add(mycont)

    This will create the parameters.

    later I need to refer to mycont to get its value, so from the parameter name FromDate I need to get the value of mycont whose name is FromDate.

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Different controls can have the same name or no name at all so storing the name of the control will do you no good unless you can reference either the control or the container of the control.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width