Solved

MSFlexGrid AUTO Rowheight and Colwidth

Posted on 2000-03-12
5
1,036 Views
Last Modified: 2008-03-17
Is there someone who know how to auto set the control's column width and row height after one have populated it with data. If possible, a module PLSE
0
Comment
Question by:beneke
[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
  • 2
5 Comments
 
LVL 14

Expert Comment

by:mcrider
ID: 2611275
There is no way to autoset the column width and height, BUT, you can do something like this... This makes sure the column width is wide enough for the text...

Dim lWidth As Long

MSFlexGrid1.Row = 1
MSFlexGrid1.Col = 1
MSFlexGrid1.Text = "This is a test of the emergency broadcast system"
Form1.FontName = MSFlexGrid1.CellFontName
Form1.FontSize = MSFlexGrid1.CellFontSize
Form1.FontBold = MSFlexGrid1.CellFontBold
lWidth = Form1.TextWidth(MSFlexGrid1.Text) + 120
If MSFlexGrid1.ColWidth(MSFlexGrid1.Col) < lWidth Then
    MSFlexGrid1.ColWidth(MSFlexGrid1.Col) = lWidth
End If



Cheers!®©
0
 

Author Comment

by:beneke
ID: 2611427
Hi MCRIDER

Your code work fine for the width, no idea how to set the height? My problem is that some of the rows might have newline characters and I want to display these rows.

PS. If I don't get anything extraordinary I will reward you!
0
 
LVL 14

Accepted Solution

by:
mcrider earned 50 total points
ID: 2611933
Here you go... This does both height and width.  Width gets set with the width of the longest line in multiple line text...


    Dim lWidth As Long
    Dim lHeight As Long
    Dim iVal As Long
    Dim jVal As Long
    Dim lBuf As String
   
    With MSFlexGrid1
        .Row = 1
        .Col = 1
        Form1.FontName = .CellFontName
        Form1.FontSize = .CellFontSize
        Form1.FontBold = .CellFontBold
        .Text = "This is a test of the" + vbCrLf + "emergency broadcast system" + vbCrLf + "This is only a test"
        iVal = 1
        Do
            jVal = InStr(iVal, .Text, vbCrLf)
            If jVal = 0 Then
                lBuf = Mid$(.Text, iVal)
                If lWidth < Form1.TextWidth(lBuf) + 120 Then
                    lWidth = Form1.TextWidth(lBuf) + 120
                End If
                Exit Do
            End If
            lBuf = Mid$(.Text, iVal, jVal - iVal)
            If lWidth < Form1.TextWidth(lBuf) + 120 Then
                lWidth = Form1.TextWidth(lBuf) + 120
            End If
            iVal = jVal + 2
        Loop
        If .ColWidth(.Col) < lWidth Then
            .ColWidth(.Col) = lWidth
        End If
        lHeight = Form1.TextHeight(.Text) + 120
        If .RowHeight(.Row) < lHeight Then
            .RowHeight(.Row) = lHeight
        End If
    End With




Cheers!®©
0
 

Author Comment

by:beneke
ID: 2612333
GREAT!
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2612533
Thanks for the points! Glad I could help!


Cheers!®©
0

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…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…

749 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