Results 1 to 8 of 8

Thread: Autosize Flexgrid Columns

  1. #1
    nookta
    Guest

    Autosize Flexgrid Columns

    I'd like a large pepperoni pizza... oh wait, no flexgrid. I've got one, just started learning how to use the buggers, they're kinda neat. I'd like to have some code that will make the widths all fit to text. Like when you double click on the resizing bar in most microsoft programs. Any thoughts? Thanks!

  2. #2
    Si_the_geek
    Guest
    ** EDIT - this is very old and dubious code, see here for a much better version! **

    Here's the code you need (you can just chuck it into a module or form). I've forgotten which programs actually use it, so you'll have to work out for yourself when the mouse is clicked in the right place!
    VB Code:
    1. Sub flxAutoSizeColumns(gridName, owner_hwnd, _
    2.                        Optional ByVal include_headers As Boolean = True, _
    3.                        Optional ByVal min_col = 0, Optional max_col)
    4. 'Set flexgrid column widths to the minimum for viewing all text
    5.  
    6.   With gridName
    7.     If include_headers Then       'set top row to check
    8.       first_row = 0
    9.     Else
    10.       first_row = .fixed_rows
    11.     End If
    12.     If IsMissing(max_col) Then max_col = .Cols - 1
    13.    
    14.     For tmp_col = min_col To max_col      'for each column...
    15.       curr_max = 0
    16.       For tmp_row = first_row To .Rows - 1        '...find widest text
    17.         tmp_max = GetVisualLength(.TextMatrix(tmp_row, tmp_col), owner_hwnd)
    18.         If curr_max < tmp_max Then curr_max = tmp_max
    19.       Next tmp_row
    20.       .ColWidth(tmp_col) = 50 + (curr_max * 16.5) '...and resize the column
    21.     Next tmp_col
    22.   End With
    23.  
    24. End Sub
    Last edited by si_the_geek; Nov 21st, 2008 at 08:39 AM.

  3. #3
    Si_the_geek
    Guest
    oops.. just realised you need this module too!
    VB Code:
    1. Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
    2. Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hDC As Long) As Long
    3. Private Declare Function GetTextExtentPoint32 Lib "gdi32" Alias "GetTextExtentPoint32A" _
    4.     (ByVal hDC As Long, ByVal lpsz As String, ByVal cbString As Long, lpSize As SIZE) As Long
    5. Private Type SIZE
    6.   cx As Long
    7.   cy As Long
    8. End Type
    9.  
    10.  
    11. Function GetVisualLength(ByVal Text, ByVal form_hWnd) As Long
    12. 'sjg:  not needed in VB6, can use  <form/printer>.TextHeight
    13.  
    14. Dim hDC As Long
    15. Dim sz As SIZE
    16.  
    17.    hDC = GetDC(form_hWnd)
    18.    Call GetTextExtentPoint32(hDC, Text, Len(Text), sz)
    19.    GetVisualLength = sz.cx
    20.    Call ReleaseDC(form_hWnd, hDC)
    21.  
    22. End Function

  4. #4
    nookta
    Guest
    Thanks for the code!
    That changed the sizes, but most of the columns aren't tightly fitted. Some are about 1/3 too long.
    Any ideas?
    Attached Images Attached Images  

  5. #5
    Si_the_geek
    Guest
    yep, if you have vb6 you can drop the "GetVisualLength" stuff, replace the line in the first module with <formname>.TextWidth, and change the last line to: .ColWidth(tmp_col) = 50 + curr_max

    (or you could just change the multiplier in the last bit!)

  6. #6
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    Here is a Generic routine I've put together a while back:
    VB Code:
    1. Public Sub ResizeGrid(pGrid As MSFlexGrid, pForm As Form)
    2.     Dim intRow As Integer
    3.     Dim intCol As Integer
    4.    
    5.     With pGrid
    6.         For intCol = 0 To .Cols - 1
    7.             For intRow = 0 To .Rows - 1
    8.                 If .ColWidth(intCol) < pForm.TextWidth(.TextMatrix(intRow, intCol)) + 100 Then
    9.                    .ColWidth(intCol) = pForm.TextWidth(.TextMatrix(intRow, intCol)) + 100
    10.                 End If
    11.             Next
    12.         Next
    13.     End With
    14. End Sub

    Then just call this routine like this:


    ResizeGrid MSFlexGrid1, Form1

  7. #7
    Member
    Join Date
    Nov 2010
    Posts
    40

    Re: Autosize Flexgrid Columns

    Quote Originally Posted by Serge View Post
    Here is a Generic routine I've put together a while back:
    VB Code:
    1. Public Sub ResizeGrid(pGrid As MSFlexGrid, pForm As Form)
    2.     Dim intRow As Integer
    3.     Dim intCol As Integer
    4.    
    5.     With pGrid
    6.         For intCol = 0 To .Cols - 1
    7.             For intRow = 0 To .Rows - 1
    8.                 If .ColWidth(intCol) < pForm.TextWidth(.TextMatrix(intRow, intCol)) + 100 Then
    9.                    .ColWidth(intCol) = pForm.TextWidth(.TextMatrix(intRow, intCol)) + 100
    10.                 End If
    11.             Next
    12.         Next
    13.     End With
    14. End Sub

    Then just call this routine like this:


    ResizeGrid MSFlexGrid1, Form1
    For some reason the procedure doesnt seem to recognise my flexigrid. Stepping thru the code it lists my flexigrid as ""

  8. #8
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Autosize Flexgrid Columns

    There is no reason to ressurrect the dead! This thread is 9 years old. Try the solution linked to in post #2.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width