473,406 Members | 2,390 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

loading multidimensional control arrays

13
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!
Aug 5 '07 #1
8 2986
Killer42
8,435 Expert 8TB
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.)
Aug 6 '07 #2
pureenhanoi
175 100+
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.  
Aug 6 '07 #3
vfiroiu
13
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!
Aug 7 '07 #4
vfiroiu
13
by the way, how do you delete posts?
Aug 7 '07 #5
vfiroiu
13
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|
Aug 7 '07 #6
pureenhanoi
175 100+
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.
Aug 7 '07 #7
Killer42
8,435 Expert 8TB
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.
Aug 7 '07 #8
vfiroiu
13
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?
Aug 8 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Golf Nut | last post by:
I am finding that altering and affecting values in elements in multidimensional arrays is a huge pain in the ass. I cannot seem to find a consistent way to assign values to arrays. Foreach would...
2
by: Terry | last post by:
Hi, can someone plz tell me how multidimensional arrays (like a 2-D array) are stored in memory? Are they like single dimensional arrays? Stored sequentially in one "row", so to say? Thanks ...
9
by: Charles Banas | last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to get some opinions and comments on it, hopefully so i can gain some sort of insight as to why this works. at the top of the...
3
by: Claire | last post by:
I have a multidimensional array defined as private double myArray = new double; The first column of the array contains X values, the other contains Y values I have a charting function defined as...
3
by: Ravi Singh (UCSD) | last post by:
Hello all I am trying to use jagged and multi-dimensional arrays in C++. In C# these work fine // for jagged arrays string jaggedArray = new string ; //for multidimensional arrays string...
21
by: utab | last post by:
Hi there, Is there a way to convert a double value to a string. I know that there is fcvt() but I think this function is not a part of the standard library. I want sth from the standard if...
10
by: | last post by:
I'm fairly new to ASP and must admit its proving a lot more unnecessarily complicated than the other languages I know. I feel this is because there aren't many good official resources out there to...
2
by: oopsatwork | last post by:
Ok...so, I have been outside of the C world for a _very_ long time...but not so long as to remember how to do multidimensional arrays. So, let me state that I know how to malloc pointers to...
9
by: Slain | last post by:
I need to convert a an array to a multidimensional one. Since I need to wrok with existing code, I need to modify a declaration which looks like this In the .h file int *x; in a initialize...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.