Solved

Auto Sizing MSFlexGrid rows to data

Posted on 2004-04-29
4
306 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now