Connecting Tech Pros Worldwide Forums | Help | Site Map

loading multidimensional control arrays

Newbie
 
Join Date: May 2007
Posts: 13
#1: Aug 5 '07
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("VB.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!

Moderator
 
Join Date: Oct 2006
Location: Australia
Posts: 7,748
#2: Aug 6 '07

re: loading multidimensional control arrays


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.)
pureenhanoi's Avatar
Familiar Sight
 
Join Date: Mar 2007
Location: VietNam
Posts: 175
#3: Aug 6 '07

re: loading multidimensional control arrays


Quote:

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("VB.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:
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Dim columns, rows As Integer
  4.  
  5. Dim Square() As Control
  6. Dim size As Integer
  7.  
  8. -----------------------------------------------------------
  9.  
  10. Private Sub Form_activate()
  11. Dim colcount, rowcount As Integer
  12.  
  13. ReDim Square(rows, columns)
  14.  
  15. For rowcount = 0 To rows
  16. For colcount = 0 To columns
  17. Set Square(rowcount, colcount) = frmMineSweeper.Controls.Add("VB.Label", "Square" & rowcount & colcount, frmMineSweeper)
  18. With Square(rowcount,colcount)
  19.     .Height = size
  20.  
  21.     .Width = size
  22.  
  23.     .Top = 10 + size * rowcount
  24.  
  25.     .Left = 10 + size * colcount
  26.  
  27.     .Visible = True
  28.  
  29.     .BackColor = vbBlue
  30.  
  31.     .Caption = "N"
  32.  
  33. End With
  34.  
  35. Next colcount
  36. Next rowcount
  37.  
  38. End Sub
  39. ---------------------------------
  40. Private Sub Form_Load()
  41. columns = 10
  42. rows = 10
  43. size = 500
  44. End Sub
  45.  
Newbie
 
Join Date: May 2007
Posts: 13
#4: Aug 7 '07

re: loading multidimensional control arrays


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

Expand|Select|Wrap|Line Numbers
  1. Private Sub Square_MouseUp(rowcount As Integer, colcount As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
  2. Square(rowcount, colcount).Caption = Button
  3. End Sub
  4.  
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!
Newbie
 
Join Date: May 2007
Posts: 13
#5: Aug 7 '07

re: loading multidimensional control arrays


by the way, how do you delete posts?
Newbie
 
Join Date: May 2007
Posts: 13
#6: Aug 7 '07

re: loading multidimensional control arrays


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|
pureenhanoi's Avatar
Familiar Sight
 
Join Date: Mar 2007
Location: VietNam
Posts: 175
#7: Aug 7 '07

re: loading multidimensional control arrays


Quote:

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

Expand|Select|Wrap|Line Numbers
  1. Private Sub Square_MouseUp(rowcount As Integer, colcount As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
  2. Square(rowcount, colcount).Caption = Button
  3. End Sub
  4.  
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.
Moderator
 
Join Date: Oct 2006
Location: Australia
Posts: 7,748
#8: Aug 7 '07

re: loading multidimensional control arrays


Quote:

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.
Newbie
 
Join Date: May 2007
Posts: 13
#9: Aug 8 '07

re: loading multidimensional control arrays


Quote:

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 multidimensional control array. The only problem seems
to be that the array subroutine doesn't function. Does anyone know a way to fix this?
Reply