Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Actual size of column in MSFlexgrid control 1

Status
Not open for further replies.

MickeD

Programmer
Feb 20, 2000
54
Hi All! How can I recognize an actual size of column in MSFlexgrid if I bind it to database on the fly. I don't know what is actual length of field and font! size variable too.<br>
I try to put a cell text in label with autosize=true and resize the column width with the label's one. but anyway I get smaller column.<br>
Any idea?
 
Use the LEN statement to find the length of the data before putting it in. X = Len(MyField) x will return an integer<br>
then use the grids column width property to set the width. <p> DougP<br><a href=mailto: dposton@universal1.com> dposton@universal1.com</a><br><a href= > </a><br>
 
And have you restart the appliction you have the same problems !<br>
<br>
If you change the width of collumns you must to save the registery ,and will load to most read the registery<br>
<br>
Eric<br>
<p>Eric De Decker<br><a href=mailto:vbg.be@vbgroup.nl>vbg.be@vbgroup.nl</a><br><a href= Visual Basic Center</a><br>
 
Try DoEvents after. It makes a world of difference.
 
Thanks for your assistance, but it don't work!<br>
================================<br>
Len(X) -return number of characters but I need something like this :<br>
Grid.ColWidth = Len(X) * SomeFunction(Grid.Font)<br>
where &lt;SomeFunction&gt; - return width of letter in twip for given font<br>
Is there any function that do it?<br>
=================================<br>
I can't save anything in registry because this is will be an aplication for browsing database tables ( and it's not fixed with database will be open)<br>
<br>
=================================<br>
Yes, DoEvents is very helpfull but not in this case.
 
NO I don't think there is a &quot;Function&quot; that is in VB to give the width of a font in Twips.<br>
But guess what the reason you like VB so much its because of it's so flexible.<br>
So write your own.<br>
Start by making a table of font names and twips sizes.<br>
<br>
Now understand that every Video card and every monitor manufacturer and lets throw in Resolution size, is slightly differnt so you need a fudge factor. <p> DougP<br><a href=mailto: dposton@universal1.com> dposton@universal1.com</a><br><a href= > </a><br>
 
This isn't exactly what you want you hear but if you increase the column width based on the length and a &quot;fudge factor&quot; of .1 you should be able to accomodate most fonts and waste only a little screen acreage.
 
You can do what you need using an API function. Try this:

Put the following in your form's General section:

Declare Function GetTextExtentPoint32 Lib &quot;gdi32&quot; _
Alias &quot;GetTextExtentPoint32A&quot; (ByVal hDC As Long, _
ByVal lpsz As String, ByVal cbString As Long, _
lpSize As Size) As Long

Public Type Size
cx As Long
cy As Long
End Type

-----------------------------------------------------------
On MyForm , set the Font attribute to the same font name, size and style that you will be using in the FlexGrid on the form, then ...

Define the following function:

Public Function GetPTextWidth(strTest As String) As Long

'calculate length of text in twips
Dim lngRV As Long
Dim cellSize As Size
Dim lngX As Long
Dim lnHeight As Long

lngRV = GetTextExtentPoint32(MyForm.hDC, strTest, Len(strTest), cellSize)

With cellSize
GetPTextWidth = .cx * Screen.TwipsPerPixelX
lnHeight = .cy * Screen.TwipsPerPixelY
End With

If GetPTextWidth < 1100 Then GetPTextWidth = 1100

End Function

-----------------------------------------------------------
Put the follwing code where you need to extract the text width to set the FlexGrid column width:

Dim inContentWidth As Integer
inContentWidth = GetPTextWidth(strTemp)

MyForm!MyGrid.ColWidth(MyForm.MyGrid.Col) = inContentWidth + 150


-----------------------------------------------------------
NOTE: If you define the function GetPTextWidth as Size instead of Long, you can retrieve the text height and use this to calculate a row height if you want to spread the cell contents over multiple lines in a narrower column. You will need to calculate where the line breaks are going to occur, but that is another story ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top