Results 1 to 5 of 5

Thread: [RESOLVED] Help with my Grid class

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2009
    Posts
    90

    Resolved [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.

    Attached Images Attached Images  
    Attached Files Attached Files

  2. #2
    Junior Member
    Join Date
    Dec 2008
    Posts
    17

    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

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2009
    Posts
    90

    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. 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...

  4. #4
    Junior Member
    Join Date
    Dec 2008
    Posts
    17

    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
    Last edited by tfbasta; Jan 15th, 2009 at 11:55 AM.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2009
    Posts
    90

    Re: Help with my Grid class

    Sorry for taking so long to reply. Thanks tfbasta, everything works perfect now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width