Results 1 to 11 of 11

Thread: Programmatically set dimensions of MSFlexGrid based on columns/rows

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Posts
    236

    Programmatically set dimensions of MSFlexGrid based on columns/rows

    I've found a few answers to this on the net but nothing is very accurate. Some leave scrollbars and others leave gray background area. I'm using VB6 and an MSFLexGrid control.

    This is the basic code for setting up the grid

    Code:
    with grid
    
        .cols=3
    
        .ColWidth(0) = 400
        .Row = 0
        
        .Col = 1
        .ColWidth(1) = 800
                
        .Col = 2
        .ColWidth(2) = 0
    
    end with
    Code:
    For X = 0 To .Cols - 1
          .Width = .Width + (.ColWidth(X) + Screen.TwipsPerPixelX))
     Next
    
    
     - and -
    
    
    For X = 0 To .Rows - 1
          .Height = .Height + (.ColHeight(X) + Screen.TwipsPerPixelY))
     Next
    That's the basic routine I'm using. It is not very accurate. For the width, for instance, it leaves horizontal scroll bars.

    However, if I add the line

    Code:
    .Width = .Width + 23
    
    - and -
    
    .Height = .Height-140
    to the end of the code, the scroll bar disappears.

    Overall I'm using about 10 MSFlexGrids in my project and find the calculations differ on each table depending on the number of rows and columns each has. I have not adjusted the height of any rows so they remain at the default (240).

    Oddly enough, when I step through the column calculations the .ColWidth(x) set at 400 comes out reading '405" and the one set to 800 reads '795" ... ???

    Surely there must be a consistent way to accurately figure the dimensions?
    TIA
    Ken

  2. #2
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    Re: Programmatically set dimensions of MSFlexGrid based on columns/rows

    JG


    ... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...

  3. #3
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Programmatically set dimensions of MSFlexGrid based on columns/rows

    Those dimensions are in Twips but must come out to a whole number of the equivalent in pixels, which varies by the current DPI setting you have for the display. If you use an "off" size it gets rounded to the nearest pixel.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Posts
    236

    Re: Programmatically set dimensions of MSFlexGrid based on columns/rows

    I haven't tried that code JG .. but thanks for sharing. From what I gather from the responses here, it seems odd to me that you can resize columns to fit an oversize grid but not fit the grid to existing columns. I decided to redesign all the forms in my project based on a lower screen resolution rather than fight with the resizing. I had been using listboxes extensively and found I used half or even less the amount of code by using MSFlexGrids instead ... and things look better -with the exception of having to fiddle with fixing the height and width. If the code works I'll come back and mark the thread as resolved although it's sort of a backwoods workaround ...

    Thanks for replying dilettante. From what you are saying I gather there is no way to determine the screen resolution and adjust the grid size based on that. So my method of adding to the dimensions by using an arbitrary number seems the only way to go. Oh well .. it is what it is and all we can do is work within the limitations. Thanks again.

  5. #5
    Frenzied Member
    Join Date
    Apr 2012
    Posts
    1,272

    Re: Programmatically set dimensions of MSFlexGrid based on columns/rows

    Don't forget that you need to allow for grid line width when adding up your column widths. Try with a large grid line width and you'll see....
    If you don't know where you're going, any road will take you there...

    My VB6 love-children: Vee-Hive and Vee-Launcher

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Programmatically set dimensions of MSFlexGrid based on columns/rows

    What Colin said...
    That was my first thought too... not only between the cells but also the border around the grid itself too... when you set the width of a cell, it sets the inside width... assuming that all measurements are in px (which I know they aren't in VB6, but for argument sake we'll jsut roll with it) if the border width is 1px... and the cell is 200px... the total width of the cell is 202... 1px for left and 1px for the right, plus the inside 200px... and since the text doesn't just slam right up against the border, there's an internal margin too... not sure if that's part of the 200px as well, or if it adds to it....and the left 1px border is the right 1px of the cell before it... so...ya know...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Posts
    236

    Re: Programmatically set dimensions of MSFlexGrid based on columns/rows

    Thanks guys .. in the end the only way seems to just add a figure to the width/height as I did. I was aware of the line width which is why I was adding something to the grid.width. I found that adding 23 to the width removed horizontal scroll bars and anything less left them in place.

    JG - that code did not work out for me at all - I couldn't get it to run ... when entering the call to the sub VB kept telling me I had to set the call = something. In other words I could not simply call the subroutine like I do every other one. No biggie .. I'll just go on adding numbers to force the grid into it's gridbox without scroll bars.

    Ken

  8. #8
    Frenzied Member
    Join Date
    Apr 2012
    Posts
    1,272

    Re: Programmatically set dimensions of MSFlexGrid based on columns/rows

    OK, but if your users have their scrollbars set to anything other than the default size then your 'magic' 23 will not be so magic after all...
    If you don't know where you're going, any road will take you there...

    My VB6 love-children: Vee-Hive and Vee-Launcher

  9. #9
    Frenzied Member
    Join Date
    Apr 2012
    Posts
    1,272

    Re: Programmatically set dimensions of MSFlexGrid based on columns/rows

    Been playing around with this as it should (you'd imagine!) be simple.

    Anyway, if you have a flat grid with no borders, then take the sum of all .ColWidths and size the grid to that total, you will find that you will always have to add one pixel (15 twips) to the flexgrid's width in order to make the scrollbar go away. This one pixel appears to relate to a border that you can see but can't make go away via any of the controls properties.

    Therefore, to solve your problem, you need to allow for;

    1) the size of the border that is drawn when the control is 3D as opposed to flat (or put it inside a picture box and use the pictures border for that 3d look)
    2) allow for that one pixel described above
    3) allow for the size of the scrollbar which you can obtain via a GetSystemMetrics API call

    This should fix ya...
    If you don't know where you're going, any road will take you there...

    My VB6 love-children: Vee-Hive and Vee-Launcher

  10. #10
    Frenzied Member
    Join Date
    Apr 2012
    Posts
    1,272

    Re: Programmatically set dimensions of MSFlexGrid based on columns/rows

    Oh, and the grid line width is NEVER a factor, to my surprise...
    If you don't know where you're going, any road will take you there...

    My VB6 love-children: Vee-Hive and Vee-Launcher

  11. #11
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Programmatically set dimensions of MSFlexGrid based on columns/rows

    Another solution - to heck with sizing the grid... jsut let the form dictate the size and if there's scrollbars... so be it...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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