Solved

Autosize columns in an a (hierarchical) flexgrid

Posted on 2001-07-01
7
559 Views
Last Modified: 2007-11-27
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
Comment
Question by:StefanD
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6243112
ping...
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 6244226
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
 
LVL 1

Author Comment

by:StefanD
ID: 6245683
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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6245962
Stefan, this is not what you asked in first place!
0
 
LVL 1

Author Comment

by:StefanD
ID: 6247284
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
 
LVL 43

Accepted Solution

by:
TimCottee earned 250 total points
ID: 6247782
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
 
LVL 1

Author Comment

by:StefanD
ID: 6249753
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month10 days, 14 hours left to enroll

631 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question