Field names

hi all. i want to know what code to use to change the name of a field (of say a checkbox) if a condition is met while the program is running.
is it therefore possible to name 5 checkboxes: P1, P2, P3...P5, and then use a variable, (eg : choice) to assign functions to the fields. eg: P(choice).value = True
thank-you for your help.


Answer this question

Field names

  • Kvltv

    From what it sounds like, you want to have an array of controls that you can index through.  There are two ways to do this. First, you could get out the vb6 compatibility dll and use vb6 style control arrays, which I would discourage.  Second, you could place the controls on your form and then create an array which you populate in the constructor or load event of the form.  That would look something like this:


    Dim boxes As New List(Of Checkbox)

    Private Sub MakeArray()
        boxes.Add(check1)
        boxes.Add(check2)
        boxes.Add(check3)
        ...
    End Sub

     


    All you have to do then is call the MakeArray function in one of the loading routines and you should be set.  You could also add more checkboxes to your array by simply creating the checkbox and adding it to the boxes list, just make sure you add it to the form's (or panel's or whatever container) Controls collection or it won't show up.

  • Aceofspades

    You can't rename a control on run time. But you can change other properties.

    Best regards.

    Fernando Cardoso

  • IainMc

    Hi George,

    This sounds like a job for my old favourite, the Controls collection.  This is a collection of all of the controls on the form.

    If you have an array of checkboxes, say P1, P2, P3 ... you can address them, by name, in a variety of manners:

    me.Controls("P1").value=true

    me.Controls("P" &"2").value=true

    Choice=3

    me.Controls("P" & Choice).value=true

    or whatever else you want to do with them.

    I hope this gives you an idea or two.

    Regards

    Nigel


  • Srefae

    i am using VB.Net 2005 with Natinal Instruments Measurement Studio; and i am having trouble creating an array of controls and then indexing through them. using your example i run into issues with the

    dim x as new list(of NIcontrol)

    the NIcontol is an un declared object and i have not been sucessful in geting this to work; any ideals

    thenks

    george


  • Field names