Saturday, August 10, 2013

Auto Resize Flexgrid Column Depending On Content In Visual Basic 6

Flexgrid is one of the most used controls for report generation in our programming firm. Recently, we required to have a function the will automatically resize the Flexgrid column depends on the column content. If you are somehow problematic about this method then you may try the code below if it will suit to your need.

Public Sub AutoFitFlexGrid(FForm As Form, ByVal FFlexGrid As MSFlexGrid) 
 Dim FGRow As Long 
 Dim FGColumn As Long 
 Dim FGCellWidth As Single 
 Dim FGColumnWidth As Single 
 Dim FGMaxColumns As Long 
 Dim FGMaxRows As Long 

 FGMaxColumns = vFlexGrid.Cols - 1 
 FGMaxRows = vFlexGrid.Rows - 1 

 For FGColumn = 0 To FGMaxColumns 
  FGColumnWidth = 0: FGCellWidth = 0 
  For FGRow = 0 To FGMaxRows 
   FGCellWidth = vForm.TextWidth(vFlexGrid.TextMatrix(FGRow, FGColumn)) 
   If FGColumnWidth < FGCellWidth Then FGColumnWidth = FGCellWidth 
  Next 
  vFlexGrid.ColWidth(FGColumn) = FGColumnWidth + 120 
 Next 
End Sub