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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MSFlexgrid cell resize 1

Status
Not open for further replies.

eequalsmc2plus1

Programmer
Aug 26, 2002
28
ZA
Hey,

I got a flex grid, put some stuff in it and then I want to resize each column to be a specific length, depends on length of string added to column.
But msflexgrids use twips, cant change it.
What the best way to dynamically resize the columns ?
Is there a setting for it ? cant find it if there is.
Only way I can think of is 2 get the length of the text added to the column, say 10 chars and * it by ??? to get it into twips. 1cm = +-500 twips so ??? probably about 166.
Isnt there a better way ?

t1 = " t2 = "iiiiiiiiii"
t3 = "MMMMMMMMMM"
MSFlexGrid1.Col = 1
MSFlexGrid1.Row = 1
MSFlexGrid1.Text = t1
MSFlexGrid1.ColWidth(1) = Len(t1) * 166
MSFlexGrid1.Col = 2
MSFlexGrid1.Row = 1
MSFlexGrid1.Text = t2
MSFlexGrid1.ColWidth(2) = Len(t2) * 166
MSFlexGrid1.Col = 3
MSFlexGrid1.Row = 1
MSFlexGrid1.Text = t3
MSFlexGrid1.ColWidth(3) = Len(t3) * 166

Doesnt work well cause the iiiii's take up much less space then ???
 
I would suggest that you look at the .TextWidth and .TextHeight methods of the form.

If you set the form's font settings to that the grid, then execute those methods, with a specific cell text as the arguement, you'll get twips dimensions of that string. From there, you can set the Row and Column sizes of the Flexgrid. Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Below is the code I use to automatically resize the columns of a Flexgrid based on the length of values in the cells/columns.


Private Sub MSFlexGrid1_DblClick()
Const MARGIN = 55 '// twips

Dim i As Integer, j As Integer
Dim l_strHighText As String
Dim l_lngFieldValue As Long
Dim l_lngFieldValueHigh As Long

On Error GoTo ERR_Handler

For i = 0 To MSFlexGrid1.Cols - 1
l_lngFieldValue = 0
l_lngFieldValueHigh = 0
For j = 0 To MSFlexGrid1.Rows - 1
l_lngFieldValue = Len(MSFlexGrid1.TextMatrix(j, i))
If l_lngFieldValue > l_lngFieldValueHigh Then
l_lngFieldValueHigh = l_lngFieldValue
l_strHighText = MSFlexGrid1.TextMatrix(j, i)
End If
Next j
l_lngFieldValueHigh = PixelsToTwips(Find_TextWidthInPixels(l_strHighTextt, 8, "MS Sans Serif", False))
MSFlexGrid1.ColWidth(i) = (MARGIN * 2) + l_lngFieldValueHigh
Next i
Exit Sub

ERR_Handler:
MsgBox Err.Number & "; " & Err.Description
End Sub

Private Function Find_TextWidthInPixels(Msg As String, Optional FSize As Integer = 8, Optional TheFont As String = "MS Sans Serif", Optional Bold As Boolean = False) As Single
Dim F As StdFont, S As Integer

S = Me.ScaleMode
Me.ScaleMode = vbPixels
Set F = Me.Font
Me.FontBold = Bold
Me.FontSize = FSize
Me.Font = TheFont
Find_TextWidthInPixels = Me.TextWidth(Msg)
Me.Font = F
Me.ScaleMode = S
End Function

Function PixelsToTwips(Pixels As Long) As Long
PixelsToTwips = Pixels * Screen.TwipsPerPixelX
End Function
Thanks and Good Luck!

zemp
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top