Solved

Auto Sizing MSFlexGrid rows to data

Posted on 2004-04-29
4
308 Views
Last Modified: 2010-05-02
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.
0
Comment
Question by:rband1
  • 3
4 Comments
 
LVL 1

Expert Comment

by:surturz
ID: 10954083
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
0
 
LVL 1

Accepted Solution

by:
surturz earned 120 total points
ID: 10954609
Sorry, I didn't provide the CopyFont sub...

Public Sub CopyFont(SourceFont As StdFont, DestinationFont As StdFont)
    'Copies a font object's properties to another font object. ("Set" makes the two
    'fonts identical i.e. changes to one are reflected in the other)
    DestinationFont.Name = SourceFont.Name
    DestinationFont.Size = SourceFont.Size
    DestinationFont.Bold = SourceFont.Bold
    DestinationFont.Italic = SourceFont.Italic
    DestinationFont.Charset = SourceFont.Charset
    DestinationFont.Strikethrough = SourceFont.Strikethrough
    DestinationFont.Underline = SourceFont.Underline
    DestinationFont.Weight = SourceFont.Weight
End Sub

If you want to autosize the row heights, you'll need to add  a line after the ".ColWidth(ThisCol&) = ..." line using the .RowHeight property of the MSHFlexGrid
0
 
LVL 1

Expert Comment

by:surturz
ID: 10954615
Sorry, having a brain tumour. You'll need a separate loop for the rows.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question