Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Auto Sizing MSFlexGrid rows to data

Posted on 2004-04-29
4
Medium Priority
?
324 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
3 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 480 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

876 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