2 Attachment(s)
[RESOLVED] Help with my Grid class
I'm making a grid class to draw a grid on a form. I also want to be able to fill in blocks of the grid when you click one.
One problem is, the clicking isn't very accurate, and sometimes fills in the block next to it...and also, when resizing the form, blocks will be filled that were not filled before.
I'm keep track of each block's position in a 2D RECT array, and also each block's state (filled, not filled) in a 2D byte array (1 = filled, 0 = not).
I'm attaching the project...any help would be appreciated. :sick:
http://www.vbforums.com/attachment.p...5&d=1231510344
Re: Help with my Grid class
I fixed some part of your code. Here is it... Please try it and let me know.
Thanks,
Code:
Public Sub FillBlock(ByVal X As Single, ByVal Y As Single)
Dim sinX As Single, sinY As Single
sinX = Fix(X / m_BlockWidth) + 1
sinY = Fix(Y / m_BlockHeight) + 1
bytFilled(CLng(sinX), CLng(sinY)) = 1
FillRect m_hDC, udtBlocks(CLng(sinX), CLng(sinY)), lonBrush
End Sub
Re: Help with my Grid class
Quote:
Originally Posted by tfbasta
I fixed some part of your code. Here is it... Please try it and let me know.
Thanks,
Code:
Public Sub FillBlock(ByVal X As Single, ByVal Y As Single)
Dim sinX As Single, sinY As Single
sinX = Fix(X / m_BlockWidth) + 1
sinY = Fix(Y / m_BlockHeight) + 1
bytFilled(CLng(sinX), CLng(sinY)) = 1
FillRect m_hDC, udtBlocks(CLng(sinX), CLng(sinY)), lonBrush
End Sub
Thanks, tf. :D That fixed the accuracy problem.
Anyone have any ideas about why some blocks appear filled in when resizing?
I think the problem is in the ReDimFilled() sub, which is to redimension the byte array that keeps track of filled blocks, while preserving existingly filled in ones... :confused:
Re: Help with my Grid class
Replace your RedimFilled sub with the one below. This should solve your problem.
Code:
Private Sub ReDimFilled(ByVal OldX As Long, ByVal OldY As Long, ByVal NewX As Long, ByVal NewY As Long)
If NewX > 0 And NewY > 0 Then
Dim bytOld() As Byte
Dim lX As Long, lY As Long
bytOld() = bytFilled
ReDim bytFilled(1 To NewX, 1 To NewY) As Byte
For lX = LBound(bytFilled, 1) To UBound(bytFilled, 1)
For lY = LBound(bytFilled, 2) To UBound(bytFilled, 2)
If lX <= UBound(bytOld, 1) And lY <= UBound(bytOld, 2) Then
bytFilled(lX, lY) = bytOld(lX, lY)
Else
bytFilled(lX, lY) = 0
End If
Next
Next
End If
End Sub
Re: Help with my Grid class
Sorry for taking so long to reply. Thanks tfbasta, everything works perfect now. :)