Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 574
  • Last Modified:

Autosize columns in an a (hierarchical) flexgrid

Hi all,

I want my Flexgrid to automatically size the columns to the content in the columns when the user double clicks on the separator on the top row of the flexgrid. In other words: same behavior as Excel.

thanks,
Stefan
0
StefanD
Asked:
StefanD
  • 3
  • 2
  • 2
1 Solution
 
Richie_SimonettiCommented:
ping...
0
 
TimCotteeCommented:
This snippet is not perfect, there is no specific event for clicking/ dblclicking on the sizing handle for the column, the event will fire when the header row is dblclicked. It also relies on having the same font (inc size) used by default on the form and in the grid. If there are different fonts on the grid then this can be handled as well but requires some slight modification.

Private Sub MSFlexGrid1_DblClick()
   Dim dblWidth As Double
   Dim intCol As Integer
   Dim intRow As Integer
   intRow = MSFlexgrid1.MouseRow
   intCol = MSFlexgrid1.MouseCol
   If intRow = 0 Then 'Clicked on header row
       For intRow = 1 To MSFlexGrid1.Rows - 1
           If Me.TextWidth(MSFlexGrid1.TextMatrix(intRow, intCol)) > dblWidth Then
               dblWidth = Me.TextWidth(MSFlexGrid1.TextMatrix(intRow, intCol))
           End If
       Next
       MSFlexGrid1.ColWidth(intCol) = dblWidth + 100
   End If
End Sub
0
 
StefanDAuthor Commented:
Thanks for your post, but I really need to know whether the user doubleclicked on the sizing handle. The column headers are already used for sorting data in that column.

Stefan
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Richie_SimonettiCommented:
Stefan, this is not what you asked in first place!
0
 
StefanDAuthor Commented:
I tried to make clear I wanted to know whether a user double clicked on the separator between two columns. I just didn't know that it's called a sizing handle. I thought the 'just like excel' part made things clear, but apperntly it didn't. Sorry.

Stefan

0
 
TimCotteeCommented:
StefanD, this seems to work for me, it is not a "NICE" way but as there is no event for this we have to work around it. It relies on the fact that the sizer handle is present when the mouse is within the last two pixels of the column header the code essentially finds out where the mouse is in relation to the screen and decides whether this is in the appropriate area of the column header if so (in this example) it adds 100 to the width of the column but you should add the appropriate auto-sizing code instead.

Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long

Private Type POINTAPI
    x As Long
    y As Long
End Type

Private Sub MSFlexGrid1_DblClick()
    Dim ptMouse As POINTAPI
    Dim x As Double
    Dim y As Double
    Dim intCol As Integer
    intCol = MSFlexGrid1.MouseCol
    If MSFlexGrid1.MouseRow <> 0 Then Exit Sub
    Call GetCursorPos(ptMouse)
    With MSFlexGrid1
        Debug.Print ptMouse.x * Screen.TwipsPerPixelX - Me.Left - .Left - IIf(Me.BorderStyle = 0, 0, 45) - 45 * .BorderStyle, .ColPos(intCol) + .ColWidth(intCol)
        If (.ColPos(intCol) + .ColWidth(intCol)) - (ptMouse.x * Screen.TwipsPerPixelX - Me.Left - .Left - IIf(Me.BorderStyle = 0, 0, 45) - 45 * .BorderStyle) <= 30 Then
            .ColWidth(intCol) = .ColWidth(intCol) + 100
        End If
    End With
End Sub
0
 
StefanDAuthor Commented:
Indeed, a working solution. This code does exactly what I was looking for. Thank you very much, you deserved the points.

Stefan.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now