Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Two Dimensional Control Arrays in Visual Basic

Options
  • 10-05-2003 1:19pm
    #1
    Closed Accounts Posts: 102 ✭✭


    I’m trying to complete a small project in visual basic and it would be useful to be able to make a 2 dimensional control array. I would be grateful if anyone could tell me how I may achieve this, thanks.


Comments

  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Sorry about the delay in getting back to you (if you are atually Fuller).

    Okay, you can create a control array if you want to, in code, you can't do it with the IDE's screen painter. I've created a 2 dimensional array below. Basically what you want to do is create an array of type Control (step1 in the example). Redim that array to the dimensions you want (step 2) here I've created a 2 dimension array with 2 for the first dimension and text1.count for the second dimension. Its up to you to work out why. <EASYHINT>Each textbox has a corresponding label</EASYHINT>

    Then all you have to do is set the array elements (you do have to use Set) so (0,1) is the first label in the label array and (1,0) is set to the first textbox in the textbox array etc etc. You can add extra dimensions in this style. The Debug.Print commands simply print the default property for each control.
    '// Step 1: create an array of type control
    Dim oControl() As Control
    
        '// Step 2: redim the array 
        ReDim oControl(2, Text1.Count) As Control
        
       '// Step 3: add control arrays elements to 
       '// multidimensional array
        Set oControl(0, 0) = Label1(0)
        Set oControl(0, 1) = Label1(1)
        Set oControl(1, 0) = Text1(0)
        Set oControl(1, 1) = Text1(1)
        
        Debug.Print oControl(0, 0)
        Debug.Print oControl(0, 1)
        Debug.Print oControl(1, 0)
        Debug.Print oControl(1, 0)
    

    I did try and add the label and textbox arrays themselves but this didn't seem to work, it only worked by adding each label and textbox arrays' individual elements (step3). There may be a way of doing it but I don't need to know really.

    Why are you using 2 dimensional control arrays?


  • Registered Users Posts: 15,443 ✭✭✭✭bonkey


    /me is wondering if you might not be able to shorten the coding a bit by writing a clever loop which processes the items in the Form.Controls collection.

    At the very least, I would try looping over the control array, rather than coding each item specifically as Phil did, but its essentially the same idea....and I'd ask the same question....

    what do you want a 2d control array for?

    jc


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Looping would be the best way to do it - this was only for demonstration purposes (homework etc ;))


  • Closed Accounts Posts: 102 ✭✭Something


    Ahh, thx EvilPhill :D (sorry for delayed thx), as for the reason for 2 dimentional control arrays i was trying to use them to greate a grid :D

    I'm probbaly barking up the wrong tree tho :|


Advertisement