Solved

Autosize columns in an a (hierarchical) flexgrid

Posted on 2001-07-01
7
548 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
  • 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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ms access #TYPE! error on report when no data 4 75
Hide vba in gp 7 94
How to read File Date Created using VB6 8 39
Export Data to Different .csv Files 26 105
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

770 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