Solved

Auto Sizing MSFlexGrid rows to data

Posted on 2004-04-29
4
312 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

752 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