Results 1 to 4 of 4

Thread: auto resize flexgrid cols

  1. #1

    Thread Starter
    New Member desflynn's Avatar
    Join Date
    Feb 2002
    Posts
    11

    auto resize flexgrid cols

    Hey y'all

    Is it possible to resize a flexgrid's column width to fit the widest item in the column, or is it possible to loop through a recordset and find the widest string and size it according to that?

    Cheers

    Des
    ____________________________________
    Des Flynn - mailto:[email protected]
    http://www.desflynn.com - Thin Lizzy

  2. #2

    Thread Starter
    New Member desflynn's Avatar
    Join Date
    Feb 2002
    Posts
    11
    ok, thanks for the help y'all... (sarcastically)(just kiddin...)

    Found it

    VB Code:
    1. For y = 0 To Grid.Cols - 1
    2. objRS.MoveFirst
    3. Grid.Col = y
    4. z = 0
    5.  
    6. For x = 0 To Grid.Rows - 1
    7. Grid.Row = x
    8. If TextWidth(Grid.Text) > z Then z = TextWidth(Grid.Text)
    9. Next x
    10.  
    11. Grid.ColWidth(y) = z +100 ' 100 or whatever extra
    12. Next y

    turn off redraw first, turn it on again after, et voila.

    Des
    ____________________________________
    Des Flynn - mailto:[email protected]
    http://www.desflynn.com - Thin Lizzy

  3. #3
    Lively Member BurnMan2003's Avatar
    Join Date
    Feb 2003
    Posts
    99
    Where does this code go..In the FormLoad?

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    Where does this code go..In the FormLoad?
    It goes after you load the data into the grid (which depends on your program.


    Anyway desflynn, your code does the job, but this is slightly faster (as it doesn't change the row/column):
    VB Code:
    1. Dim max_width as Long
    2. Dim tmp_width as Long
    3. With Grid
    4.   .Redraw = False
    5.   For y = 0 To .Cols - 1
    6.     max_width = 0
    7.     For x = 0 To .Rows - 1
    8.       tmp_width = TextWidth(.TextMatrix(x,y))
    9.       If tmp_width > max_width Then max_width = tmp_width
    10.     Next x
    11.     .ColWidth(y) = max_width +100 ' 100 or whatever extra
    12.   Next y
    13.   .Redraw = True
    14. End With

    And rather than 100 I would recommend using Screen.TwipsPerPixelX * 5, as it will be the same size on all computers (the usual measurement unit - Twips - can vary between computers)

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