?
Solved

Autosize columns in an a (hierarchical) flexgrid

Posted on 2001-07-01
7
Medium Priority
?
567 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 1000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

719 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