shovels
asked on
Decide FlexGrid colum widths at run time
Hi,
Has anyone any ideas or suggestions as to how I can decide the width of my FlexGrid colums at run time. The FlexGrid is being populated by information from an Access DB.
e.g. An E-mail address column needs to be wider than a Telephone column. How can I allocate the correct colum width??
Any suggestions or help would be greatly appreciated.
cheers,
shovels
Has anyone any ideas or suggestions as to how I can decide the width of my FlexGrid colums at run time. The FlexGrid is being populated by information from an Access DB.
e.g. An E-mail address column needs to be wider than a Telephone column. How can I allocate the correct colum width??
Any suggestions or help would be greatly appreciated.
cheers,
shovels
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Add a Picture box to your project and set its Visible property to False. The following routine will then got through the FlexGrid and Autosize the columns.. <smile>.
<----- Code Begin ----->
Private Sub x_MsFlexGrid_AutoSize()
With MSFlexGrid1
MSFlexGrid1.Visible = False
Picture1.Visible = False
Dim lngBorder As Long
Dim lngCol As Long
Dim lngRow As Long
lngBorder = 6 * Screen.TwipsPerPixelX
' Set all Column Widths to Minimum
For lngCol = 0 To .Cols - 1
.ColWidth(lngCol) = lngBorder
Next lngCol
' Autosize
For lngRow = 0 To .Rows - 1
.Row = lngRow
For lngCol = 0 To .Cols - 1
.Col = lngCol
If .Text <> "" _
Then
Picture1.FontBold = .CellFontBold
Picture1.FontItalic = .CellFontItalic
Picture1.FontName = .CellFontName
Picture1.FontSize = .CellFontSize
Picture1.FontStrikethru = .CellFontStrikeThrough
Picture1.FontUnderline = .CellFontUnderline
If (Picture1.TextWidth(.Text) + lngBorder) _
> .ColWidth(lngCol) _
Then
.ColWidth(lngCol) = (Picture1.Width + lngBorder)
End If
End If
Next lngCol
Next lngRow
MSFlexGrid1.Visible = True
End With
End Sub
<----- Code End ----->
<----- Code Begin ----->
Private Sub x_MsFlexGrid_AutoSize()
With MSFlexGrid1
MSFlexGrid1.Visible = False
Picture1.Visible = False
Dim lngBorder As Long
Dim lngCol As Long
Dim lngRow As Long
lngBorder = 6 * Screen.TwipsPerPixelX
' Set all Column Widths to Minimum
For lngCol = 0 To .Cols - 1
.ColWidth(lngCol) = lngBorder
Next lngCol
' Autosize
For lngRow = 0 To .Rows - 1
.Row = lngRow
For lngCol = 0 To .Cols - 1
.Col = lngCol
If .Text <> "" _
Then
Picture1.FontBold = .CellFontBold
Picture1.FontItalic = .CellFontItalic
Picture1.FontName = .CellFontName
Picture1.FontSize = .CellFontSize
Picture1.FontStrikethru = .CellFontStrikeThrough
Picture1.FontUnderline = .CellFontUnderline
If (Picture1.TextWidth(.Text)
> .ColWidth(lngCol) _
Then
.ColWidth(lngCol) = (Picture1.Width + lngBorder)
End If
End If
Next lngCol
Next lngRow
MSFlexGrid1.Visible = True
End With
End Sub
<----- Code End ----->
If you are populating the grid in code, then you could try something like this:
Set the Font on either a Form or a PictureBox to match the font in your grid. Then check the TextWidth property for the size in twips, add a buffer, and assign that width to the column.
This example assumes a PictureBox (Pic1) with the font set correctly:
Dim strTmp as String
msFlexGrid1.Row = I
msFlexGrid1.Col = J
strTmp = rs!eMailAddr
msFlexGrid1.Text = strTmp
lngTmp = Pic1.TextWidth(strTmp) + 200 ' 200 twip margins around text
If lngTmp > msFlexGrid1.ColWidth(J) Then
msFlexGrid1.ColWidth(J) = lngTmp
End If
.....
If you just want to set the widths to some fixed value:
In Form_Load, add code like this:
With msFlexGrid1
.ColWidth(0) = 100
.ColWidth(1) = 350
.ColWidth(2) = 2000
End With