We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

# MSFLEXGRID CONTROL.  WORDWRAP?

on
Medium Priority
1,031 Views
MSFLEXGRID  QUESTION.
From: Tim Brockway   Email: BROCKWAYJT&AOL.COM    (860) 242-3947

(VB5 Win 95)

HOW DOES ONE DETERMINE THE HEIGHT OF A ROW NECESSARY TO SHOW ALL WRAPPED AROUND TEXT?

The MsFlexGrid allows a row  to wrap text around vertically if it won't fit in a cell horizontally. But the cell does not autosize vertically to display the lower portion of the wrapped text.  The user has to size the rows height to show all the wrapped text. After assigning the text to the cell the rowheight property remains the same

How can one know the necessary row height?.

MSFLEXGRID  QUESTION.
From: Tim Brockway   Email: BROCKWAYJT&AOL.COM    (860) 242-3947

(VB5 Win 95)

HOW DOES ONE DETERMINE THE HEIGHT OF A ROW NECESSARY TO SHOW ALL WRAPPED AROUND TEXT?

The MsFlexGrid allows a row  to wrap text around vertically if it won't fit in a cell horizontally. But the cell does not autosize vertically to display the lower portion of the wrapped text.  The user has to size the rows height to show all the wrapped text. After assigning the text to the cell the rowheight property remains the same

How can one know the necessary row height?.

Comment
Watch Question

## View Solution Only

Commented:
well, i have an idea,
tell me what u think:
divide the length of the data, by
the width of a cell (no. of chars)
this gives u the no. of lines.
set the height propert to the no. of lines
multiple by the height of each line
e.g.
if u have a cell at a width of 8 chars. the height of
each char is 240 points (in vb) and u have a
text of 20 chars (incl. spaces)
then do the following:
20/8= 2.5 -> int(2.5)=3
3*240=720.
in order to show 3 lines, which are the no. of lines
needed, set the height property to 720...

Commented:
Sorry, I know this "calculating" idea seems plausible but it breaks down in practice owing to the details.

For example we are dealing with word wrap which functions in entire words or hyphenated words. Thus a character by character count does not give the column width capacity. Other complications such as centering etc put the problem beyond the scope of a calculated solution of the kind you recommend. (Unless I want to spend years on it!).

Thanks for the idea.

I have also tried the following.

Use a dummy invisible label object (which  word wraps and DOES autosize vertically). By making the dummy label the same size as the grid cell then assigning the text to the label and seeing what it's height becomes. Then use that height for the row. This trick works great most of the time but when the col width gets narrow (less than say 2 inches) the label control autosizes its WIDTH as well as it's height. This is because if a single word or string gets large in a small label it autosizes width as well as height. My trick therefor does not work at these small sizes.

This lack of autosize  is a major flaw in MSFlexGrid. Any solution to this problem would be a boost to thousands of users.

Is there an API of some kind?

Thanks again msiman. Keep up the good work.

Commented:
Use the .CellFontWidth property
When u change the font's width, its height remains the same, as specified with the CellFontSize property
You have the .CellWidth & .CellHeight properties to determine the exact size of the cell and use it to paste a TextBox control over the cell u need to edit, and after the user pressed the Enter key of the TextBox control lose the focus, recopy its contents to the cell and hide the text control. (Use the CellEnter event too)

Commented:
Thanks for the idea.

I should have explained more clearly that  I am using the cell for display only. There is no question of inputing text. The problem is knowing how high the row must be to display the wrapped text. There is no autosize property on the Flexgrid.

Calculations based on Font size etc are out of the question since there are a thousand subtle problems when you start combining wordwrap and font changes. (Its difficult enough even if you stick to one font because you never know how wide a row will be in characters because of word wrap. Think about it).

But let us say I use the text box locked as an internal dummy to calculate the height. I'm not sure how that or an overlaid text box will give me the required height any better than a dummy autosizing label..

As I mentioned in a previous message I tried using the autosize facility of the label control with the inadequate outcome I described. An overlaid textbox would suffer the same shortcomings unless I'm missing something..

Thanks again for the idea.

We are going to solve this one somehow. I've got a feeling its an API

Keep those ideas rolling in and I'll test them out.

Commented:
I beg the solution is in the GetSystemMetrics ?

Commented:
Sorry GetSystemMetrics does not pertain although I agree it sounds as if it should.

Thanks.

Keep the ideas coming.

Tim.

Commented:
Good luck then ;-)
CERTIFIED EXPERT
Commented:
Below are the functions that I use.  I am using the regular Grid 1.0 but this should work for you also...  I also noticed that I have to wordwrap the text on my own..  I have a wordwrap function if you need that too....

Type TEXTMETRIC
tmHeight As Long
tmAscent As Long
tmDescent As Long
tmAveCharWidth As Long
tmMaxCharWidth As Long
tmWeight As Long
tmOverhang As Long
tmDigitizedAspectX As Long
tmDigitizedAspectY As Long
tmFirstChar As Byte
tmLastChar As Byte
tmDefaultChar As Byte
tmBreakChar As Byte
tmItalic As Byte
tmUnderlined As Byte
tmStruckOut As Byte
tmPitchAndFamily As Byte
tmCharSet As Byte
End Type

Public TextAttrib As TEXTMETRIC

Declare Function GetTextMetrics Lib "gdi32" Alias "GetTextMetricsA" (ByVal hdc As Long, lpMetrics As TEXTMETRIC) As Long

Call GetTextMetrics(Form1.hdc, TextAttrib)

End Sub

Public Sub UpDateGridSize(ByVal WhichGrid As Control, ByVal TextLength As Integer, ByVal ColumnNumber As Integer)

Dim Counter As Integer
Dim CRs As Integer
Dim CRPosition As Integer

CRs = 1
CRPosition = 0

If TextLength * TextAttrib.tmMaxCharWidth * Screen.TwipsPerPixelX > WhichGrid.ColWidth(ColumnNumber) Then

WhichGrid.ColWidth(ColumnNumber) = TextLength * TextAttrib.tmMaxCharWidth * Screen.TwipsPerPixelX

End If

For Counter = 1 To TextLength 'Len(WhichGrid.Text)

CRPosition = InStr(CRPosition + 1, WhichGrid.Text, Chr(13), 0)
If CRPosition <> 0 Then

CRs = CRs + 1

Else

Exit For

End If

Next Counter

If WhichGrid.RowHeight(WhichGrid.Row) < TextAttrib.tmHeight * Screen.TwipsPerPixelY * CRs Then

WhichGrid.RowHeight(WhichGrid.Row) = TextAttrib.tmHeight * Screen.TwipsPerPixelY * CRs

End If

End Sub

Not the solution you were looking for? Getting a personalized solution is easy.

Commented:
Thanks a million for the suggestion. I have done something similar and
I might take you up on the word wrap code if all else fails. If so I will give you all the points I have and even unto the seventh generation of my camels. For now I'm looking for a "non-calculating" answer.

But I'm simply astonished we have to get down to reinventing the wheel like this. It 1997 Microsoft !!! We did wordwrap 20 years ago. Where is the point of VB5 if we have to do this.?

Sorry to gripe.

But I really am  looking for a 'non-calculating" option. My dummy label trick works quite well so I'll stick with it for now.

Many thanks anyway.

Tim..
##### Thanks for using Experts Exchange.

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile