loading multidimensional control arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vfiroiu
    New Member
    • May 2007
    • 13

    loading multidimensional control arrays

    I am having problems creating a multi-dimensional control array (specifically, labels) in VB6. I also want to do this entirely from the code, without creating objects on the form and copy-pasting. I know to create the first label, index (0,0) , using "Set", and then use "Load" to create the other labels. However, i am getting "Subscript out of range" as an error message when i run it. My current code is this:
    _______________ _______________ ______________
    Option Explicit

    Dim columns, rows As Integer

    Dim Square() As Control
    Dim size As Integer

    -----------------------------------------------------------

    Private Sub Form_activate()
    Dim colcount, rowcount As Integer

    ReDim Square(0, 0)
    Set Square(0, 0) = frmMineSweeper. Controls.Add("V B.Label", "Square", frmMineSweeper)

    For rowcount = 0 To rows
    For colcount = 0 To columns
    If (colcount <> 0) Or (rowcount <> 0) Then
    Load Square(colcount , rowcount)
    End If

    With Square(colcount , rowcount)

    .Height = size

    .Width = size

    .Top = 10 + size * rowcount

    .Left = 10 + size * colcount

    .Visible = True

    .BackColor = vbBlue

    .Caption = "N"

    End With

    Next colcount
    Next rowcount

    End Sub
    ---------------------------------
    Private Sub Form_Load()
    columns = 10
    rows = 10
    size = 500
    End Sub
    _______________ _______________ _______________

    Any help is appreciated!
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    To the best of my knowledge, there is no such concept as a multi-dimensional control array. Unless I'm mistaken, you would need to create your controls in a "normal" array, and just position them so they look like a 2D one.

    That being said, I suppose you might be able to create a 2D array internally, just using Dim/Private/Public. However, I'm not sure how you'd go about getting them to display on the form.

    What about this - create them as a regular 1D array, and Set elements of a 2D array to point to them. Since they are then pointers to the same object, you can use the 2D array in your code, just as though the form did support a 2D control array.

    (As you can see, I'm just sort of "thinking out loud" here.)

    Comment

    • pureenhanoi
      New Member
      • Mar 2007
      • 175

      #3
      Originally posted by vfiroiu
      I am having problems creating a multi-dimensional control array (specifically, labels) in VB6. I also want to do this entirely from the code, without creating objects on the form and copy-pasting. I know to create the first label, index (0,0) , using "Set", and then use "Load" to create the other labels. However, i am getting "Subscript out of range" as an error message when i run it. My current code is this:
      _______________ _______________ ______________
      Option Explicit

      Dim columns, rows As Integer

      Dim Square() As Control
      Dim size As Integer

      -----------------------------------------------------------

      Private Sub Form_activate()
      Dim colcount, rowcount As Integer

      ReDim Square(0, 0)
      Set Square(0, 0) = frmMineSweeper. Controls.Add("V B.Label", "Square", frmMineSweeper)

      For rowcount = 0 To rows
      For colcount = 0 To columns
      If (colcount <> 0) Or (rowcount <> 0) Then
      Load Square(colcount , rowcount)
      End If

      With Square(colcount , rowcount)

      .Height = size

      .Width = size

      .Top = 10 + size * rowcount

      .Left = 10 + size * colcount

      .Visible = True

      .BackColor = vbBlue

      .Caption = "N"

      End With

      Next colcount
      Next rowcount

      End Sub
      ---------------------------------
      Private Sub Form_Load()
      columns = 10
      rows = 10
      size = 500
      End Sub
      _______________ _______________ _______________

      Any help is appreciated!
      See ya. When you ReDim the Array to (0,0), this mean that there is only one element in the array. Error messae "Subscript are out of range" means that: you are using a not existing element of array. As in your code, the error occures when you are loading the Square(1,0) element.
      To resolve this problem, replace the code
      Redim Square(0,0) by ReDim Square(rows, columns)
      When you edit this line, you will see the Error "Subscript are out of range" does not occure again. But the other error will be. you will ask "Why?".
      See again, the first element of Square array (Square(0,0)) already been loaded by adding this to Controls array. But, how about other elements. You tried Load Square(colcount , rowcount). But the error will occure here. Remember: except first element, all other elements now is nothing. So you cant load them into memory.
      Now, replace this line:
      Load Square(colcount ,rowcount) by
      Set Square(colcout, rowcount) = frmMineSweeper. Controls.Add(.. .) again
      Remember, each controls have distinct name, so you must use the diffent name each time you call Controls.Add() method.
      If you try creating an array of label (e.x: Label(1), Label(2)) by Controls.Add() method, so you are using wrong way.
      If you just create a greate amount of Label, so you can modify the code like this:
      Code:
      Option Explicit
      
      Dim columns, rows As Integer
      
      Dim Square() As Control
      Dim size As Integer
      
      -----------------------------------------------------------
      
      Private Sub Form_activate()
      Dim colcount, rowcount As Integer
      
      ReDim Square(rows, columns)
      
      For rowcount = 0 To rows
      For colcount = 0 To columns
      Set Square(rowcount, colcount) = frmMineSweeper.Controls.Add("VB.Label", "Square" & rowcount & colcount, frmMineSweeper)
      With Square(rowcount,colcount)
          .Height = size
          
          .Width = size
      
          .Top = 10 + size * rowcount
          
          .Left = 10 + size * colcount
          
          .Visible = True
      
          .BackColor = vbBlue
          
          .Caption = "N"
      
      End With
      
      Next colcount
      Next rowcount
      
      End Sub
      ---------------------------------
      Private Sub Form_Load()
      columns = 10
      rows = 10
      size = 500
      End Sub

      Comment

      • vfiroiu
        New Member
        • May 2007
        • 13

        #4
        Thanks pureenhanoi! the code you gave me does seem to create the array properly. However, i am having problems using the labels in procedures (specifically, MouseUp). I want an action to happen when i click on one of the labels in the array Square(). The code i have is as follows

        Code:
        Private Sub Square_MouseUp(rowcount As Integer, colcount As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
        Square(rowcount, colcount).Caption = Button
        End Sub
        This same code works for a normal, 1-dimensional array formed by copy-pasting. However, nothing happens when i click on the labels in Square(). Again, any help is appreciated!

        Comment

        • vfiroiu
          New Member
          • May 2007
          • 13

          #5
          by the way, how do you delete posts?

          Comment

          • vfiroiu
            New Member
            • May 2007
            • 13

            #6
            I could also just make a 1 dimensional control array, and have a 1-to-1 function from the one index to two indexes. For example, Square(x, y) could have index (y*columns+x) and the index (z) would correspond to
            Square(z mod columns, z \ columns)
            so that a 5x3 grid would have the following indexes:
            (rows = 3, columns = 5)
            .....0...1...2. ..3...4
            ______________
            0 | 0 | 1 | 2 | 3 | 4 |
            1 | 5 | 6 | 7 | 8 | 9 |
            2 |10|11|12 |13|14|

            Comment

            • pureenhanoi
              New Member
              • Mar 2007
              • 175

              #7
              Originally posted by vfiroiu
              Thanks pureenhanoi! the code you gave me does seem to create the array properly. However, i am having problems using the labels in procedures (specifically, MouseUp). I want an action to happen when i click on one of the labels in the array Square(). The code i have is as follows

              Code:
              Private Sub Square_MouseUp(rowcount As Integer, colcount As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
              Square(rowcount, colcount).Caption = Button
              End Sub
              This same code works for a normal, 1-dimensional array formed by copy-pasting. However, nothing happens when i click on the labels in Square(). Again, any help is appreciated!
              This is complicated problem. I've read some book that writing about adding control to Controls Array. These can create Event for each element was created but the Event works for only one element. This means that, if you create 5x5 Matrix of Label, you must write 25 Event for each Label (i dont see Event that written for Array of controls - perhaps i should learn more).
              If you are practicing about Controls Array, maybe you must search any more to find out the sollution. Sorry, but i cant help
              If you're trying make a small game like MineSweeper in Windows OS, there is other way with less coplexing. 5months ago, i already made an MineSweeper game, like the Windows does. Ofcourse, it does not act as well as the Microsoft does, but i think that not bad.
              If you need, i will give you the code via Email. Maybe, the code have some varriable, and function name not wrote in enlish.
              If you really want to practice, i will describe the method in latter post.

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                Originally posted by vfiroiu
                by the way, how do you delete posts?
                Sorry, I've been too busy to read this thread. But I did spot this brief question (one I had time to read). You delete a post by hitting the Edit/Delete button below it. But unless you're a Moderator, you can only do so for a certain amount of time after posting it. The limit used to be five minutes, but I've just been informed it has now increased to one hour.

                Comment

                • vfiroiu
                  New Member
                  • May 2007
                  • 13

                  #9
                  Originally posted by pureenhanoi
                  This is complicated problem. I've read some book that writing about adding control to Controls Array. These can create Event for each element was created but the Event works for only one element. This means that, if you create 5x5 Matrix of Label, you must write 25 Event for each Label (i dont see Event that written for Array of controls - perhaps i should learn more).
                  If you are practicing about Controls Array, maybe you must search any more to find out the sollution. Sorry, but i cant help
                  If you're trying make a small game like MineSweeper in Windows OS, there is other way with less coplexing. 5months ago, i already made an MineSweeper game, like the Windows does. Ofcourse, it does not act as well as the Microsoft does, but i think that not bad.
                  If you need, i will give you the code via Email. Maybe, the code have some varriable, and function name not wrote in enlish.
                  If you really want to practice, i will describe the method in latter post.
                  Well, the labels appear normally, so there is hope for the multidimensiona l control array. The only problem seems
                  to be that the array subroutine doesn't function. Does anyone know a way to fix this?

                  Comment

                  Working...