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
Last edited by Johno2090; Nov 13th, 2007 at 11:41 AM.
Reason: Resolved
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
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
yeah i belive you can click a cell in a flexgrid to edit it by code or manually
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
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.
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
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
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
set flexgrid.redraw = false prior to running your code, then true on completion
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
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.