Solved

Cell height on flexgrid

Posted on 2006-06-26
9
1,161 Views
Last Modified: 2009-07-29
Hi Experts.

I have a flexgrid on my form written in vb. Sometimes the data is too long to fit into the cell. I have switched wordwrap on but this just makes part of it disappear. I have had a look on the Microsoft website and there is a way of doing it but I couldn't get my head around it. Typical Microsoft jargon. Can someone explain / show me the code to do this please.

Many Thanks
Lee
0
Comment
Question by:ljhodgett
9 Comments
 
LVL 8

Expert Comment

by:hiteshgupta1
ID: 16983059
call this procedure in the Form_Load event or whereever u may like to

 Private Sub widths()
    Dim INTROW As Integer
    Dim INTCOL As Integer
   Dim aryWidths() As Double
   ReDim aryWidths(MSHFlexGrid1.Cols - 1)
   For INTROW = 0 To MSHFlexGrid1.Rows - 1
       For INTCOL = 0 To MSHFlexGrid1.Cols - 1
           If Form1.TextWidth(MSHFlexGrid1.TextMatrix(INTROW, INTCOL)) > aryWidths(INTCOL) Then
               aryWidths(INTCOL) = Form1.TextWidth(MSHFlexGrid1.TextMatrix(INTROW, INTCOL))
           End If
       Next
   Next
   For INTCOL = 0 To MSHFlexGrid1.Cols - 1
       MSHFlexGrid1.ColWidth(INTCOL) = aryWidths(INTCOL) + 250
   Next
End Sub

let us know if it works
0
 

Author Comment

by:ljhodgett
ID: 16983338
Hi hiteshgupta1

Thanks for getting back to me.

Its not quite what I'm looking for. The Widths are fixed as the flexgrid need to be printed onto A4. As a result if the text in the cell goes above a cetain amount I want it to wrap but be on 2 lines in the same cell.

Cheers
Lee.
0
 
LVL 20

Expert Comment

by:alainbryden
ID: 16984688
Allright. As I'm sure you know, you can't increase the height of individual cells, that would just look weird, but you can increase the height of on entire row. To do this use the following code:

MyFlexGrid.RowHeight(0) = MyFlexGrid.RowHeight(0) * 2

This will double the height of the first row. You can do it for any given row as long as you have the right index.
0
 
LVL 9

Expert Comment

by:dancebert
ID: 16985099
Autosize RowHeight with WordWrap = True

This works with MsFlexGrid, is supposed to work the MSHFlexgrid, just change the As MSFlexGrid to As MSHFlexGrid
 
For this example you need a Form with 2 controls:
1. MSFlexGrid1
2. Label1

The Label1 control will be used the determine the correct height of the cell.
-----------------------------------------------------------------------

Option Explicit

Private Sub Form_Load()
  Dim lCol As Long, lRow As Long
 
  ' just add some data
  With MSFlexGrid1
    .Cols = 8
    .Rows = 8
    .WordWrap = True
    For lRow = 1 To .Rows - 1
      For lCol = 1 To .Cols - 1
        .TextMatrix(lRow, lCol) = "This line is to long and should wrap to the next line"
      Next lCol
    Next lRow
  End With
 
  FlexGridAutosizeRows MSFlexGrid1, Label1
End Sub


Public Sub FlexGridAutosizeRows(p_ctlGrid As MSFlexGrid, _
                        p_ctlLabel As Label, _
                        Optional ByVal p_lFirstRow As Long = -1, _
                        Optional ByVal p_lLastRow As Long = -1)

    '   For all cells:
    '   Stuff cell contents into a label that has autosize and wordwrap turned on, then
    ' read resulting height from label and apply to cell.

    Dim lCol As Long, lRow As Long
    Dim lCurCol As Long, lCurRow As Long
    Dim lRowHeight As Long

    ' Faster, if set at design time
'    With p_ctlLabel
'        .AutoSize = True
'        .WordWrap = True
'        .Visible = False
'    End With

    With p_ctlGrid
        .Redraw = False
        lCurCol = .Col
        lCurRow = .Row

        If p_lFirstRow = -1 Then p_lFirstRow = 0
        If p_lLastRow = -1 Then p_lLastRow = .Rows - 1

        For lRow = p_lFirstRow To p_lLastRow
            lRowHeight = 0
            .Row = lRow
            For lCol = 0 To .Cols - 1
                .Col = lCol
                lRowHeight = ResizeCellHeight(lRow, lCol, p_ctlGrid, p_ctlLabel, lRowHeight)
            Next lCol
           
            ' showtime
            ' Wg was chosen (I guess) because that's as wide and low as letters get
            .RowHeight(lRow) = lRowHeight + Me.TextHeight("Wg") / 5
        Next lRow

        .Row = lCurRow
        .Col = lCurCol
        .Redraw = True
    End With

End Sub



Private Function ResizeCellHeight(ByVal p_lRow As Long _
                , ByVal p_lCol As Long _
                , p_ctlGrid As MSFlexGrid _
                , p_ctlLabel As Label _
                , ByVal p_lRowHeight As Long) _
                As Long
               
    Dim lReturn As Long
   
    With p_ctlGrid
        p_ctlLabel.FontBold = .CellFontBold
        p_ctlLabel.FontName = .CellFontName
        p_ctlLabel.FontSize = .CellFontSize
        p_ctlLabel.FontBold = .CellFontBold
        ' Uncomment if using any of them
        'p_ctlLabel.FontItalic = .CellFontItalic
        'p_ctlLabel.FontStrikethru = .CellFontStrikeThrough
        'p_ctlLabel.FontUnderline = .CellFontUnderline
       
        p_ctlLabel.Width = .ColWidth(p_lCol)
        p_ctlLabel.Caption = .TextMatrix(p_lRow, p_lCol)
       
        If p_ctlLabel.Height > p_lRowHeight Then
            lReturn = p_ctlLabel.Height
        Else
            lReturn = p_lRowHeight
        End If
    End With
   
    ResizeCellHeight = lReturn
End Function


0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 20

Expert Comment

by:alainbryden
ID: 16985217
Sorry dancebert, all that's incorrect.

WordWrap = True does not autosize the height of rows in a flexgrid, as the author of the question allready stated.

Quote: "I have switched wordwrap on but this just makes part of it disappear. "

The only way to change the height is to have the following code:

Dim I As Integer 'The index of the row
MyFlexGrid.RowHeight(i)= MyFlexGrid.RowHeight(i * 2
0
 
LVL 20

Accepted Solution

by:
alainbryden earned 250 total points
ID: 16985231
I also think it's awful, dancebert, that you copied your ENTIRE response from this webpage:

http://www.xtremevbtalk.com/showthread.php?threadid=35110

for shame.
0
 
LVL 9

Expert Comment

by:dancebert
ID: 16985804
alainbryden:

What's your problem?  I never claimed the answer was my code.  When I do provide an answer I prefer to do it as text rather than a link because links become dead over time.  

0
 
LVL 9

Expert Comment

by:dancebert
ID: 16985980
What this comment should say is:
' If you haven't set these at design time, this code must be uncommented.

    ' Faster, if set at design time
'    With p_ctlLabel
'        .AutoSize = True
'        .WordWrap = True
'        .Visible = False
'    End With

0
 
LVL 9

Expert Comment

by:dancebert
ID: 16985985
alainbryden:

>WordWrap = True does not autosize the height of rows in a flexgrid,
>as the author of the question allready stated.

You're right.  But the code works.  If you read it, you'd see that it stuffs the text into a label that has WordWrap and Autosize = true.  It then reads the height of the label and applies it to the FlexGrid's row height.  Oh, but don't take my word for it, just run the sample code.


>MyFlexGrid.RowHeight(0) = MyFlexGrid.RowHeight(0) * 2
>This will double the height of the first row. You can do it for
>any given row as long as you have the right index.

The original question said "Sometimes the data is too long to fit into the cell".  Your solution of hardcoding the row height will work if the length of the data is less than or equal to twice the cell width.  The downsides to your solution is that it will 1) double the row height whether needed or not, 2) fail to handle cell contents that require a 3rd (or more) row.  

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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…
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now