FlexGrid autosize columns
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: FlexGrid autosize columns

  1. #1
    Join Date
    Mar 2003
    Posts
    16

    FlexGrid autosize columns

    I am VB begginer and having trouble getting my FlexGrid columns to autosize. I have a sub that I am using from someone who posted it online. The error I am getting is: 'Sub or function not defined'

    Do I have to create another function using TextWidth or will the code work without it. If I do, could you provide an example of what the Sub should look like?

    Thanks for your help!

    Here is the code I am using.

    Public Sub gridSize(ByRef msFG As MSFlexGrid, Optional maxCol As Integer) 'optional To specify max col width
    Dim numRows As Integer 'track num of rows
    Dim numCols As Integer 'track num of cols
    Dim fldText As String 'current field text
    Dim I As Integer 'counter For rows
    Dim l As Integer 'counter For cols
    numRows = msFG.Rows
    numCols = msFG.Cols


    For I = 0 To numRows - 1


    For l = 0 To numCols - 1
    fldText = msFG.TextMatrix(I, l)


    If TextWidth(fldText) > msFG.ColWidth(l) Then


    If maxCol > 0 Then


    If (TextWidth(fldText) + 240) > maxCol Then 'set field width To max specified
    msFG.ColWidth(l) = maxCol
    End If
    Else 'else make it the width of the text
    msFG.ColWidth(l) = TextWidth(fldText) + 240
    End If
    End If
    Next l
    Next I
    End Sub

  2. #2
    Join Date
    May 2002
    Location
    Montreal
    Posts
    450
    Make sure that you have the Microsoft FlexGrid Control 6.0 checked as a component in your project.

    Other than that the code looks fine. You may want to indicate which line generates the error that you get.

    Cheers,
    Laurent

  3. #3
    Join Date
    Mar 2003
    Posts
    16
    The section of code that is causing the error is

    TextWidth(fldText)

    It is telling me that the TextWidth sub or function is not defined.

    I don't have a function or sub TextWidth because I am using this code from a user posting on a VB forum. Do I need to create a TextWidth function to make it work. If I do could you give me an example on how to do this?

    thanks

  4. #4
    Join Date
    Mar 2003
    Posts
    16
    I figured out the TextWidth problem. I needed to say frmWatch.TextWidth(). frmWatch being the name of my form. Now I am getting another error.

    Error: Object variable or with block variable not set

    Any Suggestions?


    Public Sub gridSize(ByRef msFG As MSFlexGrid, Optional maxCol As Integer) 'optional To specify max col width

    Dim numRows As Integer 'track num of rows
    Dim numCols As Integer 'track num of cols
    Dim fldText As String 'current field text
    Dim I As Integer 'counter For rows
    Dim l As Integer 'counter For cols
    numRows = msFG.Rows 'code stops here
    numCols = msFG.Cols


    For I = 0 To numRows - 1


    For l = 0 To numCols - 1
    fldText = msFG.TextMatrix(I, l)


    If frmWatch.TextWidth(fldText) > msFG.ColWidth(l) Then


    If maxCol > 0 Then


    If (frmWatch.TextWidth(fldText) + 240) > maxCol Then 'set field width To max specified
    msFG.ColWidth(l) = maxCol
    End If
    Else 'else make it the width of the text
    msFG.ColWidth(l) = frmWatch.TextWidth(fldText) + 240
    End If
    End If
    Next l
    Next I
    End SubnumRows = msFG.Rows

  5. #5
    Join Date
    May 2002
    Location
    Montreal
    Posts
    450
    What if you put that code on the same form as where your flexgrid is, in this case, you wouldn't have to pass in for grid as a parameter.
    Other than that, the only thing I can see is that the flexgrid component is not reference in your project, but you would be able to have a flexgrid on your form either.

    Cheers,
    Laurent

  6. #6
    Join Date
    Jan 2001
    Posts
    486
    Here's some code I used for this purpose that works. I set up a variable for each column to hold the largest width. Each entry in a column is then compared to that and if it's width is larger, the variable is updated. This code works, just update to fit your form/column names. If you have any questions, just ask.

    Code:
    'Declare varibles for flexgrid column width sizing
    Public intID As Integer
    Public intDate As Integer
    Public intPosition As Integer
    Public intCompany As Integer
    Public intLocation As Integer
    Public intEmail As Integer
    Public intPhone As Integer
    Public intSource As Integer
    
    Private Sub SizeColumns()
    
        'Reset all column widths
        intID = 0
        intDate = 0
        intPosition = 0
        intCompany = 0
        intLocation = 0
        intPhone = 0
        intEmail = 0
        intSource = 0
         
       'Find largest width of each flexgrid column
        Dim i As Integer
        For i = 0 To MSFlexGrid.Rows - 1
            If TextWidth(MSFlexGrid.TextMatrix(i, 0)) > intID Then
                intID = TextWidth(MSFlexGrid.TextMatrix(i, 0))
            End If
            If TextWidth(MSFlexGrid.TextMatrix(i, 1)) > intDate Then
                intDate = TextWidth(MSFlexGrid.TextMatrix(i, 1))
            End If
            If TextWidth(MSFlexGrid.TextMatrix(i, 2)) > intPosition Then
                intPosition = TextWidth(MSFlexGrid.TextMatrix(i, 2))
            End If
            If TextWidth(MSFlexGrid.TextMatrix(i, 3)) > intCompany Then
                intCompany = TextWidth(MSFlexGrid.TextMatrix(i, 3))
            End If
            If TextWidth(MSFlexGrid.TextMatrix(i, 4)) > intLocation Then
                intLocation = TextWidth(MSFlexGrid.TextMatrix(i, 4))
            End If
            If TextWidth(MSFlexGrid.TextMatrix(i, 5)) > intPhone Then
                intPhone = TextWidth(MSFlexGrid.TextMatrix(i, 5))
            End If
            If TextWidth(MSFlexGrid.TextMatrix(i, 6)) > intEmail Then
                intEmail = TextWidth(MSFlexGrid.TextMatrix(i, 6))
            End If
            If TextWidth(MSFlexGrid.TextMatrix(i, 7)) > intSource Then
                intSource = TextWidth(MSFlexGrid.TextMatrix(i, 7))
            End If
        Next
         
        'Set column widths for flex grid to largest entry in
        'the column and align all data in columns to the left
        MSFlexGrid.ColWidth(0) = intID + 90
        MSFlexGrid.ColAlignment(0) = flexAlignLeftCenter
        MSFlexGrid.ColWidth(1) = intDate + 90
        MSFlexGrid.ColAlignment(1) = flexAlignLeftCenter
        MSFlexGrid.ColWidth(2) = intPosition + 90
        MSFlexGrid.ColAlignment(2) = flexAlignLeftCenter
        MSFlexGrid.ColWidth(3) = intCompany + 90
        MSFlexGrid.ColAlignment(3) = flexAlignLeftCenter
        MSFlexGrid.ColWidth(4) = intLocation + 90
        MSFlexGrid.ColAlignment(4) = flexAlignLeftCenter
        MSFlexGrid.ColWidth(5) = intPhone + 90
        MSFlexGrid.ColAlignment(5) = flexAlignLeftCenter
        MSFlexGrid.ColWidth(6) = intEmail + 90
        MSFlexGrid.ColAlignment(6) = flexAlignLeftCenter
        MSFlexGrid.ColWidth(7) = intSource + 90
        MSFlexGrid.ColAlignment(7) = flexAlignLeftCenter
    
    End Sub
    [Cimperiali: edited for colorizing and preserving indentation purpouse]
    Last edited by Cimperiali; March 28th, 2003 at 08:13 AM.
    If kids were left to their own devices, would they ever come up with a thing like war?......The Wheel / Todd Rundgren

    Do canibals not eat clowns because they taste funny?

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center