MSFlexgrid - Set column width to text width..-VBForums
Results 1 to 14 of 14

Thread: MSFlexgrid - Set column width to text width..

  1. #1

    Thread Starter
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,389

    MSFlexgrid - Set column width to text width..

    How do I set the column width to be = to the amount of text

    (the text length varies)
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  2. #2
    Hyperactive Member
    Join Date
    Nov 2001
    Location
    Albany, NY
    Posts
    489
    Very good question.

    Although I've never tried the way I'd approach is:

    Check all Rows for Largest number of Characters
    Use Length of that word to and then You need to
    Figure out the Twips per Character.

    I've only seen Twips Per Pixel so I'm not sure where to
    go from here.

  3. #3

    Thread Starter
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,389
    thats what im doing...
    but ..the TextWidth() command isnt quite enough

    it comes up with 1125 for one col where the column needs about 1400 to fit it...


    hmmm...

    thanks
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  4. #4

    Thread Starter
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,389
    nevermind..now that I removed the cellfontbold = true... it works

    the bold was messing up the textwidth()

    thanks
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  5. #5
    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

  6. #6

    Thread Starter
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,389
    similar to what I did..

    But.. As I added each row...I checked...if the textwidth + 100 > current column width the make the colwidth = textwidth + 100

    only problem is some of the cols have to be bold. Im working on a solution...
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  7. #7
    Hyperactive Member rplcmint's Avatar
    Join Date
    Jan 2001
    Location
    Stockton, CA
    Posts
    333
    This seemed to work for me. Scrolling through every cell in that column, finding the max Twip size and then adjusting the cell for the max size:

    Private Sub mnuAdjust_Click(Index As Integer)
    Dim i As Integer
    Dim maxTwips As Integer
    Dim Twips As Integer

    With Me.MSFlexGrid1
    Me.ScaleMode = vbTwips
    For i = 0 To .Rows - 1
    Twips = Me.TextWidth(.TextMatrix(i, .ColSel))
    If Twips > maxTwips Then
    maxTwips = Twips
    End If
    Next i

    .ColWidth(.ColSel) = maxTwips * Me.MSFlexGrid1.Font.Size / Me.Font.Size + 100 '* Screen.TwipsPerPixelX

    End With


    End Sub


    Thanks for the help.

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    30,868
    Originally posted by geoff_xrx
    similar to what I did..

    But.. As I added each row...I checked...if the textwidth + 100 > current column width the make the colwidth = textwidth + 100

    only problem is some of the cols have to be bold. Im working on a solution...
    When using TextWidth, it uses the Font settings of the object - your form in this case. If you set your form's font settings to the same as the font settings of the col you are trying set the width on, you'll get the right numbers.

    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??? *

  9. #9
    Lively Member
    Join Date
    Aug 2017
    Location
    South Africa (in the middle)
    Posts
    92

    Re: MSFlexgrid - Set column width to text width..

    This is a very old thread, but some beginners would still like to know an easier method:
    Place the MSFlexGrid on the form
    Double-click on it
    It will automatically opened in the _Click Section. In the right-hand list box, select EnterCell

    Write or paste the following code:
    Me.Caption = MSFlexGrid.CellWidth

    In the properties of the toolbox, make sure the following is set:
    AllowUserResizing to 1-FlexResizeColumns

    Run the program.
    Now you can drag the column width to your choice, and the width when you click on the column will be reflected in the form's caption. Write it down to place it in the code part of your prg.

    Example (for column1 [[2]]):
    ~~~~~~~~~~~~~~~~~~
    MSFlexGrid.ColWidth(1) = 1410

    To quickly set headers, you can use this property:

    ' Set column and row headers.
    s$ = "|Name|Address|Telephone|Social Security#"
    s$ = s$ + ";|Robert|Jimmy|Bonzo|John Paul"
    MSFlexGrid.FormatString = s$

    but, it will not set the widths. It all depends on the font you use, with formatting set etc. So it's easier to do it in the above drag-method.

    Hope it helps.

    When in windows 10, the help system does not work, but when you run the winhelp32.exe program, you can click on File (alt+f), open. And it will run the .HLP file.
    Copy from windows XP:
    [drive]:\windows\winhlp32.exe
    to a folder of your choice
    Right click on the file, properties (at the bottom of the list)
    Compatibility Tab
    Run as XP ServicePack 2

    The MSDN help files are still saved under the.CHM extention, which will open in windows 10:

    Under the MSDN section on your VB6 disc, goto:
    \msdn\
    where you will find the file:
    MSHFLX98.CHM

    There are more help files in the same folder.

    Enjoy.

  10. #10
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    6,129

    Re: MSFlexgrid - Set column width to text width..

    When in windows 10, the help system does not work,
    Obviously you do not know how to install it properly. Check on this Forum and you will find out how to make it work (if yours does not)!!!

    As for the autosizing of width of Flexgrids, see this Planet link (for YOU and for all of the folks who haven't figured it out in the past 13 years!!!!) ~smile~

    http://www.planetsourcecode.com/vb/s...=8547&lngWId=1

  11. #11
    PowerPoster
    Join Date
    Feb 2006
    Posts
    17,209

    Re: MSFlexgrid - Set column width to text width..

    To be fair there is a big glitch with the parts of Help that are based on the older WinHelp technology.

    When you try to use those you'll often get an error dialog and your default browser gets opened to a Microsoft page saying "sorry, suck wind."

    That's because so far they have not released a WinHelp install package for Windows 10. We had them for Vista, Win7, Win8, and maybe even Win 8.1 and Win8.1 Fire Two, but not for Windows 10.

    A few people have posted hacked versions of the Win7 or Win8 packages that bypass the version check and supposedly those will work. I'm still digging in my heels though, there is no reason to let Microsoft off the hook for this. "Bathless" Anders "VB Killer" Hejlsberg and his gang of cronies needs to be opposed at every turn. Post as much noise as you can about this through comments on MSDN and MS KB pages.

  12. #12
    Fanatic Member
    Join Date
    Dec 2008
    Posts
    993

    Re: MSFlexgrid - Set column width to text width..

    Here is another example of sizing Cols to width of text -
    http://www.vb-helper.com/howto_fit_flexgrid.html

  13. #13
    Lively Member
    Join Date
    Aug 2017
    Location
    South Africa (in the middle)
    Posts
    92

    Thumbs up Re: MSFlexgrid - Set column width to text width..

    Quote Originally Posted by SamOscarBrown View Post
    Obviously you do not know how to install it properly. Check on this Forum and you will find out how to make it work (if yours does not)!!!

    My VB was installed on Win 7 where it worked correct. Then I upgraded to windows 10, therefore I have struggle, but I do not really care.. :-)

    As for the autosizing of width of Flexgrids, see this Planet link (for YOU and for all of the folks who haven't figured it out in the past 13 years!!!!) ~smile~

    http://www.planetsourcecode.com/vb/s...=8547&lngWId=1
    I do not really use the flexGrid, but like to play with a lot of things. Yes, I'm still a bit stupid after 13 years ha ha ha

    Thanx 4 the q reply

  14. #14
    Lively Member
    Join Date
    Aug 2017
    Location
    South Africa (in the middle)
    Posts
    92

    Re: MSFlexgrid - Set column width to text width..

    The biggest problem when adding a fixed size after the width, (automatic width estimation) comes when the user/programmer use different fonts (think courier, Sans etc) and sizes.

    Therefore I still believes the programmer have an easier task to drag the width and make the width a fixed size.

    But each of us have their own perceptions and ideas. In reality, there's not a right or wrong answer. It's what's easier for each programmer.

    And above all, I'm too lazy to write a huge piece of code to do a simple heading, unless writing a user control, and I'm far too stupid to write one anyway.

    [Edited]
    Here is a small, very confused piece of code I wrote (compiled):
    http://thezir.com/GTScode/QLaw.exe
    Last edited by Inside; Aug 7th, 2017 at 10:15 AM. Reason: Adding Info

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.