Link to home
Start Free TrialLog in
Avatar of rband1
rband1

asked on

Auto Sizing MSFlexGrid rows to data

Is there a way to have an MSFlexGrid row auto size to fit data?  I have some rows that will have multiple lies of data and other will not.

Thanks.
Avatar of surturz
surturz

Here you go...

Public Sub MSHFlexGridAutoSize(ThisForm As Form, ThisFlexGrid As MSHFlexGrid)
    'NOTE!! This routine only works is VB6 Service Pack 3 or greater is installed.
    'The MSHFlexgrid that ships with VB6 doesn't support the .Clip property correctly
   
    'Sizes each column in grid to size of widest cell in column
   
    Dim OldCol&, OldColSel&, OldRow, OldRowSel
    Dim OldFixedRows&, OldFixedCols&, OldTopRow&, OldLeftCol&
    Dim ThisRow&, ThisCol&
    Dim OldFont As StdFont
    Dim tFont As StdFont
   
    'We need to change the form's font object so we can use the textwidth etc methods
    'which are unavailable with the mshflexgrid control
    Set tFont = ThisForm.Font
    Set OldFont = New StdFont
    Call CopyFont(tFont, OldFont)
    Set tFont = Nothing: Set tFont = ThisFlexGrid.Font
    Call CopyFont(tFont, ThisForm.Font)
   
    With ThisFlexGrid
        OldLeftCol& = .LeftCol: OldTopRow& = .TopRow
        OldCol& = .Col: OldColSel& = .ColSel
        OldRow = .Row: OldRowSel = .Row
        OldFixedRows& = .FixedRows: OldFixedCols& = .FixedCols
        .FixedRows = 0: .FixedCols = 0
        For ThisCol& = 0 To .Cols - 1
            .Col = ThisCol&: .ColSel = ThisCol&
            .Row = 0: .RowSel = .Rows - 1
            .ColWidth(ThisCol&) = ThisForm.TextWidth(.Clip) + ThisForm.TextWidth("W") 'make column as wide as widest cell in column + one character (a wide one :-)
        Next ThisCol&
        'returns settings to normal
        .FixedRows = OldFixedRows&: .FixedCols = OldFixedCols&
        .Row = OldRow: .RowSel = OldRowSel
        .Col = OldCol&: .ColSel = OldColSel&
        .LeftCol = OldLeftCol&: .TopRow = OldTopRow&
    End With
    'Restore the form's font object
    Set tFont = Nothing: Set tFont = New StdFont
    Call CopyFont(OldFont, tFont)
    Set ThisForm.Font = tFont
    Set tFont = Nothing
End Sub
ASKER CERTIFIED SOLUTION
Avatar of surturz
surturz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry, having a brain tumour. You'll need a separate loop for the rows.