Solved

Autosize columns in an a (hierarchical) flexgrid

Posted on 2001-07-01
7
544 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now