-
50x50 grid problems!
First off i just want to say thanks to anyone viewing this post i'm new here and this is my first ever question!
I have currently been given a small program that im trying to get working for my dissertation (i'm a biology student!!! and i have to code!) What im trying to do is place a grid of 50x50 squares, or 100x100 squares in a frame and then according to a number in an array that i have generated change the colours of the shapes.
Now i thought this would be pretty simple but i was wrong, i need to be able to click on the shapes (or other control if its easier) and place a new number into the array.
So i guess what i want to know is how do i go about changing the backcolour of a grid of 50x50 shapes/picture boxes. Using a for next loop such as the one i tried below.
Code:
' a is the array of (50, 50, 2)
' Sizer is set by the user so for this it is 50.
For X = 1 to Sizer
For Y = 1 to Sizer
If a(X, Y, 1) = 1 then
Shape(need some way of placing X and Y co-ords in here).backcolour = red
Else
Shape(X, Y).backcolour = White
End if
Next Y
Next X
i have tried using picture boxes but it wont allow me to have 2500 of them :S, i have also tried using control arrays of shapes such as Shape(0)1 - 50 as row 1 and Shape(1)1 as row 2 all the way to 49 but couldnt figure out how to place the co-ordinates in the code.
Hopefully someone knows an easier way of doing this as its holding up my entire project.
Thanks for helping again! look forward to reading some suggestions
-
Re: 50x50 grid problems!
try using a flexgrid control, sort of like a mini spreadsheet, you should be able to set the size of the cells and they have a cellbackcolor property that you can change the colour
otherwise it would be possible to do with labels, but i would be a lot of labels, the code to create and position them at runtime is fairly simple is fairly simple
-
Re: 50x50 grid problems!
thanks for the quick reply, would that also allow me to click on the cells?
what i need is when i click the shape/cell for it to increment the number inside the array by one.
(oh and sorry for the idiotic post but where do i find the flexgrid in VB?)
-
Re: 50x50 grid problems!
yeah i belive you can click a cell in a flexgrid to edit it by code or manually
-
Re: 50x50 grid problems!
thanks ill give it a shot now, any other suggestions are welcome :)
-
Re: 50x50 grid problems!
Ok it works well, but....its far too slow i need it to be alot faster and i just dont have the know how to make it so, is there some way i can generate the whole grid at once.
The program i am making is running as a model over 50 - 100 - 150 years and updating the colours to show what is present in that grid square at that current time. At the moment it takes 3 minutes to generate the colours in the grid for just 1 year so you can imagine how long it will take for 50 years :S
any tips? the code atm is...
Code:
For X = 1 To Sizer ' Populate the Grid
For Y = 1 To Sizer
If a(X, Y, 1) = 0 Then
MSFlexGrid1.Col = X
MSFlexGrid1.Row = Y
MSFlexGrid1.CellBackColor = vbYellow
End If
If a(X, Y, 1) = 1 Then
MSFlexGrid1.Col = X
MSFlexGrid1.Row = Y
MSFlexGrid1.CellBackColor = vbGreen
End If
If a(X, Y, 1) = 2 Then
MSFlexGrid1.Col = X
MSFlexGrid1.Row = Y
MSFlexGrid1.CellBackColor = vbRed
End If
Next Y
Next X
thanks in advance.
Johno
p.s if anyone knows what it is im designing a Spatially Explicit Model.
-
Re: 50x50 grid problems!
A Select Case should run faster here. If you have the most likely return as the first case statemnet that would help also.
Code:
For X = 1 To Sizer ' Populate the Grid
For Y = 1 To Sizer
MSFlexGrid1.Col = X
MSFlexGrid1.Row = Y
Select Case a(X, Y, 1)
Case 0
MSFlexGrid1.CellBackColor = vbYellow
Case 1
MSFlexGrid1.CellBackColor = vbGreen
Case 2
MSFlexGrid1.CellBackColor = vbRed
End Select
Next Y
Next X
-
Re: 50x50 grid problems!
Individually defining cell colors does take time. Although 3 minutes does seem awfully long.
You might shorten the name of your FlexGrid and not use the default name. (EG name your control FG instead of MSFlexGrid1. You might also use Select Case instead of multiple If-then statements. Don't reset the column value every time. You only need to set it when X changes. You might also use color codes directly instead of having VB having to look up the value.
Code:
For X = 1 To Sizer ' Populate the Grid
FG.Col=X
For Y = 1 To Sizer
FG.Row=Y
Select Case a(X, Y, 1)
Case 0
FG.CellBackColor = &HFFFF& 'yellow
Case 1
FG.CellBackColor = &HFF00& 'green
Case 2
FG.CellBackColor = &HFF& 'red
End select
Next Y
Next X
-
Re: 50x50 grid problems!
See if this is appreciably faster
Code:
With MSFlexGrid1
For X = 1 To Sizer ' Populate the Grid
For Y = 1 To Sizer
If a(X, Y, 1) = 0 Then
.Col = X
.Row = Y
.CellBackColor = vbYellow
End If
If a(X, Y, 1) = 1 Then
.Col = X
.Row = Y
.CellBackColor = vbGreen
End If
If a(X, Y, 1) = 2 Then
.Col = X
.Row = Y
.CellBackColor = vbRed
End If
Next Y
Next X
And BTW
http://www.vbforums.com/attachment.p...id=47243&stc=1
-
Re: 50x50 grid problems!
set flexgrid.redraw = false prior to running your code, then true on completion
-
1 Attachment(s)
Re: 50x50 grid problems!
Since the grid is primarily a graphical display, best solution may be to draw it using the Rectangle API function. Using a picturebox as a container for the grid, the MouseUp event will provide cooridinates you can use to determine which cell the mouse is over when the user clicks on the picturebox, thus making the entire grid clickable. I have attached an example.
-
Re: 50x50 grid problems!
Westconn1 your my hero works like a charm, if i turn it off and on for the start and finish of every year it loads it instantly <3
thanks you so much for your help everyone!!