Various Bug Fixes for Column Resizing and Preventing Crashes when adding and removing Columns

Summary

Id:13703.12
Type:Bug Bug
Current Status:Resolution Identified

Detail

6 Feb 2004Resolution IdentifiedThierry Waty

I have fixed some bugs in ssGrid 2 that have been carried forward from version 1.

Like Resising columns with bigger headers, and some headers where truncated, and some other minor bugs. Here are the fixes, only the code extracted:

1) Header Text Width

' *** TWA
Private Property Get plHeaderEvaluateTextWidth( _
   ByVal lCol As Long, _
   ByVal lMaxWidth As Long _
   ) As Long
   Dim tR               As RECT
   Dim sCopy            As String
   Dim sOrig            As String
   Dim iCol             As Long
   Dim lCCol            As Long
   Dim eFlags           As ECGTextAlignFlags
   Dim lLastRight       As Long
 
   ' Find the index of lCol in the columns array:
   For iCol = 1 To m_iCols
      If (m_tCols(iCol).lCellColIndex = lCol) Then
         lCCol = iCol
         Exit For
      End If
   Next iCol
 
   ' Evaluate the text in the cell:
   If Not (IsMissing(m_tCols(lCol).sHeader)) Then
      sCopy = m_tCols(lCol).sHeader
   End If
   If (m_tCols(lCCol).sFmtString <> "") Then
      sCopy = Format$(sCopy, m_tCols(lCCol).sFmtString)
   End If
   eFlags = m_tCols(lCol).eTextAlign Or DT_CALCRECT
 
   ' For multi line we specify the right so we get a height:
   If (eFlags And DT_WORDBREAK) = DT_WORDBREAK Then
      tR.right = m_tCols(lCCol).lWidth
      If (lMaxWidth > tR.right) Then
         tR.right = lMaxWidth
      End If
   End If
 
   sOrig = sCopy
   DrawText m_hDC, sCopy & "... " & vbNullChar, -1, tR, eFlags
   If (eFlags And DT_WORDBREAK) = DT_WORDBREAK Then
      sCopy = sOrig
      ' Extend in blocks of 16 until we fit...
      tR.right = tR.right + 16
      lLastRight = tR.right
      DrawText m_hDC, sCopy & "... " & vbNullChar, -1, tR, eFlags
      tR.right = lLastRight
   End If
 
   plHeaderEvaluateTextWidth = tR.right - tR.left
 
End Property
' *** TWA

2) Selected Row

Public Property Let SelectedRow(ByVal lRow As Long)
   Dim iCol             As Long
   Dim iRow             As Long
   
   ' *** TWA
   'If (m_iSelCol = 0) Then
      'm_iSelCol = plGetFirstVisibleColumn()
   'End If
   
   If m_iRows = 0 Then
      Exit Property
   End If
   ' *** TWA
   If (lRow > 0) And (lRow <= m_iRows) Then
      m_iSelRow = lRow
      If (m_bMultiSelect) Then

3) RemoveColumn:

         ' Having removed the column, rebuild the grid cells:
         ' *** TWA
         'ReDim m_tCells(1 To m_iCols, 1 To m_iRows) As tGridCell
         ReDim m_tCells(1 To m_iCols, 1 To m_iRows + 1) As tGridCell
         ' *** TWA
         For iRow = 1 To m_iRows
            For iCol = 1 To m_iCols
               If (iCol >= lCol) Then
                  lGridCol = iCol + 1
               Else
                  lGridCol = iCol
               End If
               LSet m_tCells(iCol, m_tRows(iRow).lGridCellArrayRow) = _
                  tGridCopy(lGridCol, m_tRows(iRow).lGridCellArrayRow)
            Next iCol
         Next iRow

4) AutoWidthColumn:

Public Sub AutoWidthColumn(ByVal vKey As Variant)
   Dim iRow             As Long
   Dim lWidth           As Long
   Dim lMaxWidth        As Long
   Dim lMaxTextWidth    As Long
   Dim iCol             As Long
   Dim iCCol            As Long
 
   iCol = ColumnIndexByVariant(vKey)
   If (iCol > 0) Then
      iCCol = m_tCols(iCol).lCellColIndex
      ' *** TWA
      ' *** Keep the width of the column header
      lMaxWidth = plHeaderEvaluateTextWidth(iCCol, lMaxTextWidth)
      ' *** TWA
      For iRow = 1 To m_iRows

5) ColumnWidthChanged (this increases performance):

   If Not bCancel Then
      ' *** TWA
      If ColumnWidth(lColIndex) <> lWidth Then
         ColumnWidth(lColIndex) = lWidth
      End If
      ' *** TWA
      m_bDirty = True
      Draw
      pResizeHeader
   End If