Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Beginners Help with array

Status
Not open for further replies.

docslack33

Programmer
Nov 5, 2001
7
US
Okay, here's my problem...my teacher came in to my Beginning VB class last week and drew a form on the board with 25 text boxes arranged 5x5. Then he wrote numbers in the text boxes like so:
1 2 3 4 5
skip 1 2 3 4
skip skip 1 2 3
skip skip skip 1 2
skip skip skip skip 1
Each number and skip represents a text box. Also, there's a command button below the text boxes that when clicked, it will display the numbers like I wrote above...Now, I think that it might be a two-dimensional array, but I am not sure...also, the teacher told us that it should be around 7 LOC...I also think that it is a loop (nested, maybe) that would go through and put the numbers in each text box...
If there's anyone that can help on this, it would be much appreciated! Thank you in advanced...
 
Wanna cheat??? Set the tag property of each textbox to what it will be. Then cycle through the controls collection of the form and fill the text in with the tag:

Dim oCtl as Control

For Each oCtl in Controls
If TypeOf oCtl Is TextBox then
oCtl.Text = oCtl.Tag
End If
Next

Set oCtl = Nothing

Put that in the button click event. -Chris Didion
Matrix Automation, MCP
 
As much as 'cheating' is tempting...I really do need to know how to do this..See, here's the problem...whatever needs to be done, it is something that hasn't been taught in the class before, and we (students) were supposed to "figure it out". It's not in our book that we were supposed to have for the class (our teacher doesn't 'teach by the book') and I haven't found anything like this on the internet...so, I turned to a forum!
 
This may help. I don't necessarily support getting your
homework off the internet, but maybe it will
encourage you with your VB programming.

Do you know about "control arrays"?
If not, you should learn about them, as this program
uses one, and your teacher will surely know you had some help, if you don't understand them.

Basically a control array is an array of controls
e.g. an array of text boxes
All of the text boxes have the same name (e.g. Text1)
and you refer to the individual items in the array
by using the objects "index". The index comes after
the object's name and in round brackets.
e.g. refer to the fifth item in your text box array Text1
like this: Text1(4) - remember that arrays start with zero. So, to set the value of this item type Text1(4).text = "whatever".
How to create a control array? Easy - just put a text box
on your form, select the text box and copy and paste.
The first time you copy a text box, it will ask if you want
to make a control array - click YES.
A copy of the text box is copied on top of the original.
You can grab it and move it to another position on the form.

For your project, you need to make 25 copies of the same text box(Which is called Text1 by default - rename if you like). Make the caption of all the text boxes blank. The position of the boxes is crucial to the project working. They must be in index order from left to right and top to bottom in five rows of five boxes. So, the original text box with index 0 is at the top left on the form and the last box with index 24 is at the bottom right.
Like this:

0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
20 21 22 23 24

You can check the index of a text box in the properties window. If the index has no value, it means it is not part of the control array. Make sure they are all part of the array.

Add a command button to the form, double click the command button and add this code to it...

Private Sub Command1_Click()
Dim x As Integer
Dim y As Integer

For y = 0 To 4
For x = y To 4
Text1((5 * y) + x).Text = x - (y - 1)
Next x
Next y
End Sub


Finally, read the code and make sure you understand what's happening - i.e. a double loop which goes through textboxes 0 to 24 and sets the value depending on what x and y are equal to. Note that it never visits the text boxes whose value is left at nothing.

Hope this gets to you in time. Enjoy your programming :)I
 
I have to agree that a control array is crutial to the process and part of the foundation of the lesson. Also understanding the methods to translate a two dimensional index to one dimension and visa-versa.

The nested for translation using 2d references to generate the 1d references solves the problem in it's simplest form but there are other ways. It may even be the fastest???

Here's the opposite viewpoint. Above you are generatng the 2D indecies to change. Below I'll show how to use the 1D index to generate your values.

The MOD function creates repeating sequences and genereates a value to represent your minor dimension. Integer division provides the major index value. See, opposite from above.
Index = (Major Value * 5) + MinorValue

I read left to right so I find the above arrangement easiest to work with. Assuming you're the same and arranged left to right as instructed above.

Index mod 5 gives you the COLUMN position.
and Int(Index /5) gives you the ROW.

Now we look at the values...

The base values for each row are 1, 2, 3, 4, 5 or column + 1. These values shift down by 1 in each row, and non-positive values are not displayed. The down shift corresponds to the row number.

So determining the value goes:
[tt]
x = 1 + (COLUMN) - (ROW)
y = iif(x <= 0, &quot;Skip&quot;, format(x, &quot;0&quot;))
[/tt]
Read the second line
[tt]
if x <= 0 then
y = &quot;Skip&quot;
else
y = format(x, &quot;0&quot;)
end if
[/tt]

on the other hand, what if rows are not the major index?
Here the 2D indecies are switched, but the formulae stay pretty much unchanged.

Index mod 5 gives you the ROW position.
and Int(Index /5) gives you the COLUMN.

On to the data, does it change?
Set 1 is 1, skip...
Set 2 is 2, 1, skip...
Set 3 is 3, 2, 1, skip...

It's easy to see that the sequence starts at 1+int(index/5) and counts down. Again values of zero or less are skipped. This gives us, drum roll please...
Get ready to be amazed...
[tt]
x = 1 + (COLUMN) - (ROW)
y = iif(x <= 0, &quot;Skip&quot;, format(x, &quot;0&quot;))
[/tt];-)

As you can see the trick is not arranging the items, the trick is understanding how that arrangement impacts how the two indecies relate to the location in one dimensional space. This is one of programming's most important lessons, as memory is a 1 dimensional space.

Please no-one start that segment:eek:ffset = 2D debate.

Wil Mead
wmead@optonline.net

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top