MSFlexGrid Dynamically Autosize row height

Posted on 2005-02-24
Medium Priority
Last Modified: 2012-08-13
I populate the MSflexGrid from database table it contains Col1 Date Col2 Note (memo Field )
The memo field that contains a large amount of text and need to be able to read it all.
Some rows  display 100% and some less depends on the size of the field.
I need that they all will be displayed correctly and allow the user to easily read all the note
So My question is How to dynamically resize the row height to match the memo field.
Question by:mayajhon1
  • 2
  • 2

Expert Comment

ID: 13395656

Author Comment

ID: 13395954
Thanks  Smallint for your reply,
But this link is about how to Make a FlexGrid's columns fit its data
I'm thinking more about how to Make a FlexGrid's Rows fit its data
I want to adjust the height not the width.
LVL 18

Expert Comment

ID: 13399227

Accepted Solution

Smallint earned 1000 total points
ID: 13401670
Give it a shot.

Code assumes:

* WordWrap property of Flexgrid is enabled.
* Font used is flexgrid is the same than form where is placed

This code will not work when column width is very little because flexgrid begins to show strange behaviour.


Private Function FindTextHeight(flx As MSFlexGrid, r As Long, C As Long) As Long
    Dim text_Height As Long
    Dim cell_width As Long
    Dim n As Long
    Dim lNextBlank As Long
    Dim lCurrentBlank As Long
    Dim lLastBlank As Long
    'A little bit higher due to spacing between lines
    FindTextHeight = TextHeight(" ") * 1.2
    text_Height = FindTextHeight
    lCurrentBlank = 1
    n = lCurrentBlank
    While n < Len(flx.TextMatrix(r, C))
         lNextBlank = InStr(n, flx.TextMatrix(r, C), " ")
         If lNextBlank > 0 Then
            If TextWidth(Mid(flx.TextMatrix(r, C), lCurrentBlank, lNextBlank - lCurrentBlank)) > flx.ColWidth(C) Then
                FindTextHeight = FindTextHeight + text_Height
                If lCurrentBlank <> 1 Then
                    lCurrentBlank = lLastBlank + 1
                    lCurrentBlank = lNextBlank + 1
                End If
                lLastBlank = lNextBlank
                n = lNextBlank + 1
                lLastBlank = lNextBlank
                n = lNextBlank + 1
            End If
            FindTextHeight = FindTextHeight + text_Height
            n = Len(flx.TextMatrix(r, C))
        End If

End Function

Private Sub AutoFitFlexGridHeight(ByVal flx As MSFlexGrid)
Dim r As Long
Dim C As Long
Dim cell_Height As Single
Dim row_Height As Single

    For r = 0 To flx.Rows - 1
        row_Height = 0
        For C = 0 To flx.Cols - 1
            cell_Height = FindTextHeight(flx, r, C)
            If row_Height < cell_Height Then row_Height = cell_Height
        flx.RowHeight(r) = row_Height
End Sub


Author Comment

ID: 13403499
Thanks Sethi but the code didn't work but I appreciate the help.
Thank you Smallint the code worked and the key point for me was that the font was too big as soon as I reduced it it worked!
Thank you,

Featured Post

Independent Software Vendors: 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 about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
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…
Suggested Courses
Course of the Month7 days, 23 hours left to enroll

615 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