msflexgrid col resize

How can i increase the size of a msflexgrid column to see the longest "string" completly and reduce the size if the column is too big compared to the longest "string"?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I'm assuming it is possible to set the size of an msflexgrid and that you know how to do this.

1 Make sure the font of your msflexgrid is the same as the font on your form. If in doubt do
Set me.Font = YourFlexGrid.Font

2 Loop through all the cells in the column, I assume you know how to do this.

3 Dim dblWidth As Double
  dblWidth = 0
  'Loop through all the cells in the column:
       if me.TextWidth(<text in current cell>) > dblWidth Then
              dblWidth = me.TextWidth... (etcetera)
       End If
  'end of loop

Now you have the maximum text width in the cell.  
you may have to add a constant to take care of borders and a little bit of white space around the text so it looks nice. And finally, set the width of the column to this new size.

Good luck!!


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Here's an example. Stick an msflexgrid on a new form and paste in the following code. See how it works...

Private Sub Form_Load()

Dim intRow As Integer
Dim intCol As Integer
Dim dblWidth As Double

With Me.MSFlexGrid1

    'Example: fills the grid with text, but puts something especially
    'large in the middle cell.
    'Let's try a grid with 3 rows and 3 columns
    .Cols = 3
    .FixedCols = 0
    .Rows = 3
    .FixedRows = 0
    For intRow = 0 To .Rows - 1
        For intCol = 0 To .Cols - 1
            .Row = intRow
            .Col = intCol
            .Text = "Blah blah blah " & CStr(intRow) & ":" & CStr(intCol)
            If .Row = 1 And .Col = 1 Then
                'put in something long to show how it works
                .Text = "lots & lots & lots of text, honest, guv!"
            End If
    'This is where the relevant bit starts:
    Set .Font = Me.Font
    For intCol = 0 To .Cols - 1
        dblWidth = 0
        For intRow = 0 To .Rows - 1
            .Row = intRow
            .Col = intCol
            If Me.TextWidth(.Text) > dblWidth Then
                dblWidth = Me.TextWidth(.Text)
            End If
        .ColWidth(intCol) = dblWidth + 120

End With

End Sub
This question was awarded, but never cleared due to the JSP-500 errors of that time.  It was "stuck" against userID -1 versus the intended expert whom you awarded.  This corrects that and the expert will now receive these points, all verified.

Please click on your Member Profile, select "View Question History" to navigate through any open or locked questions you may have to update and finalize them.
Moderator @ Experts Exchange
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.