Solved

Flex Greed

Posted on 2001-08-10
5
499 Views
Last Modified: 2008-02-26
During loding  values, i want to resize a column of flex greed control. How can i do that nd what property i have to use.
0
Comment
Question by:sycale
  • 4
5 Comments
 
LVL 3

Accepted Solution

by:
andysalih earned 30 total points
ID: 6374728
this automaticaly resizes the flex grid


Public Sub AutosizeGridColumns(ByRef msFG As MSFlexGrid, ByVal MaxRowsToParse As Integer, ByVal MaxColWidth As Integer)
    Dim I, J As Integer
    Dim txtString As String
    Dim intTempWidth, BiggestWidth As Integer
    Dim intRows As Integer
    Const intPadding = 150


    With msFG


        For I = 0 To .Cols - 1
            ' Loops through every column
            .Col = I
            ' Set the active colunm
            intRows = .Rows
            ' Set the number of rows
            If intRows > MaxRowsToParse Then intRows = MaxRowsToParse
            ' If there are more rows of data, reset
            ' intRows to the MaxRowsToParse constant
            '    
           
            intBiggestWidth = 0
            ' Reset some values to 0


            For J = 0 To intRows - 1
                ' check up to MaxRowsToParse # of rows a
                '     nd obtain
                ' the greatest width of the cell content
                '     s
               
                .Row = J
               
                txtString = .Text
                intTempWidth = TextWidth(txtString) + intPadding
                ' The intPadding constant compensates fo
                '     r text insets
                ' You can adjust this value above as des
                '     ired.
               
                If intTempWidth > intBiggestWidth Then intBiggestWidth = intTempWidth
                ' Reset intBiggestWidth to the intMaxCol
                '     Width value if necessary
            Next J
            .ColWidth(I) = intBiggestWidth
        Next I
        ' Now check to see if the columns aren't
        '     as wide as the grid itself.
        ' If not, determine the difference and e
        '     xpand each column proportionately
        ' to fill the grid
        intTempWidth = 0
       


        For I = 0 To .Cols - 1
            intTempWidth = intTempWidth + .ColWidth(I)
            ' Add up the width of all the columns
        Next I
       


        If intTempWidth < msFG.Width Then
            ' Compate the width of the columns to th
            '     e width of the grid control
            ' and if necessary expand the columns.
            intTempWidth = Fix((msFG.Width - intTempWidth) / .Cols)
            ' Determine the amount od width expansio
            '     n needed by each column


            For I = 0 To .Cols - 1
                .ColWidth(I) = .ColWidth(I) + intTempWidth
                ' add the necessary width to each column
                '    
               
            Next I
        End If
    End With
End Sub


hope thats what you want

cheers
Andy
0
 
LVL 3

Expert Comment

by:andysalih
ID: 6374730
just to explain what the code does above in more depth i forgot to include :-

 
     Automatically resize the columns in any flex grid to give a nice, professional appearance. Public sub automatically resizes MS Flex Grid columns to match the width of the text, no matter the size of the grid or the number of columns. Reads first n number of rows of data, and adjusts column size to match the widest cell of text. Will even expand columns proportionately if they aren't wide enough to fill out the entire width of the grid. Configurable constraints allow you to designate 1) Any flex grid to resize 2) Maximum column width 3) the maximum number of rows in depth to look for the widest cell of text


cheers
Andy
0
 
LVL 3

Expert Comment

by:andysalih
ID: 6374739
this is a little lengthy but see how you get on with it.

You must have MSflxgrd.ocx installed.



Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    ' Function to define the address of the
    '     Message-Handling routine


Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    ' Function to execute a function residin
    '     g at a specific memory address


Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    'Windows messages constants
    Public Const WM_LBUTTONUP = &H202
    Public Const WM_LBUTTONDOWN = &H201
    Public Const WM_ERASEBKGND = &H14
          
          
You must have MSflxgrd.ocx installed.
Create a Form frmMain and place a MSFlexGrid control with name MSFlexGrid1 and a Module. The code is given below.
'
'Side Effects:Don't ever finish the appl
'     ication with the Stop button of VB Envir
'     onment. This will cause VBE to terminate
'     .
VERSION 5.00
Object = "{5E9E78A0-531B-11CF-91F6-C2863C385E30}#1.0#0"; "Msflxgrd.ocx"
Begin VB.Form frmMain
BorderStyle =3 'Fixed Dialog
Caption ="Resize the Grid !!!"
ClientHeight=4110
ClientLeft =4650
ClientTop=3750
ClientWidth =6735
LinkTopic="Form1"
MaxButton=0'False
MinButton=0'False
ScaleHeight =4110
ScaleWidth =6735
ShowInTaskbar=0'False
Begin MSFlexGridLib.MSFlexGrid MSFlexGrid1
Height =3015
Left=120
TabIndex=0
Top =960
Width=6495
_ExtentX=11456
_ExtentY=5318
_Version=65541
Rows=4
Cols=4
AllowUserResizing=1
End
Begin VB.Label Label2
Caption ="Try to resize the columns of MSFlexGrid. All the columns will be resized proportionally."
BeginProperty Font
Name="MS Sans Serif"
Size=9.75
Charset =204
Weight =400
Underline=0'False
Italic =0'False
Strikethrough=0'False
EndProperty


ForeColor=&H8000000D&
    Height =615
    Left=1320
    TabIndex=1
    Top =120
    Width=3975
    End
    End
    Attribute VB_Name = "frmMain"
    Attribute VB_GlobalNameSpace = False
    Attribute VB_Creatable = False
    Attribute VB_PredeclaredId = True
    Attribute VB_Exposed = False
    Option Explicit
    ' This constant is used to refer to the
    '     Message Handling function in a given win


    '     dow
        Private Const GWL_WNDPROC = (-4)


Private Sub Form_Load()
    'Save the address of the existing Messag
    '     e Handler
    g_lngDefaultHandler = GetWindowLong(Me.MSFlexGrid1.hwnd, GWL_WNDPROC)
    'Define new message handler routine
    Call SetWindowLong(Me.MSFlexGrid1.hwnd, GWL_WNDPROC, AddressOf GridMessage)
End Sub


Private Sub Form_Unload(Cancel As Integer)
    'Return the old handler back
    Call SetWindowLong(Me.MSFlexGrid1.hwnd, GWL_WNDPROC, g_lngDefaultHandler)
End Sub


Public Sub ResizeGridProportional()
    Dim SumWidthAs Long
    Dim i As Integer


    With MSFlexGrid1


        For i = 1 To .Cols
            SumWidth = SumWidth + .ColWidth(i - 1)
        Next i


        For i = 1 To .Cols
            .ColWidth(i - 1) = SumWidth / .Cols
        Next i
    End With
End Sub





/* ******************** MODULE ***********************************/
Attribute VB_Name = "mHandlers"
'
Option Explicit
Public g_lngDefaultHandler As Long ' Original handler of the grid events
Private m_bLMousePressed As Boolean 'true if the left button is pressed
Private m_bLMouseClicked As Boolean 'true just after the click (i.e. just after the left button is released)
'API declarations ======================
'     ======================================
' Function to retrieve the address of th
'     e current Message-Handling routine


Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    ' Function to define the address of the
    '     Message-Handling routine


Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    ' Function to execute a function residin
    '     g at a specific memory address


Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    'Windows messages constants
    Public Const WM_LBUTTONUP = &H202
    Public Const WM_LBUTTONDOWN = &H201
    Public Const WM_ERASEBKGND = &H14
    '=======================================
    '     =======================================
    'this is our event handler


Public Function GridMessage(ByVal hwnd As Long, ByVal Msg As Long, ByVal wp As Long, ByVal lp As Long) As Long


    If m_bLMousePressed And Msg = WM_LBUTTONUP Then
        'button have been just released
        m_bLMousePressed = False
        m_bLMouseClicked = True
    End If


    If Not (m_bLMousePressed) And Msg = WM_LBUTTONDOWN Then
        'button have been just pressed
        m_bLMousePressed = True
        m_bLMouseClicked = False
    End If


    If m_bLMouseClicked And (Msg = WM_ERASEBKGND) Then
        'Only when resize happens this event may
        '     occur after releasing the button !
        'When user is making a simple click on g
        '     rid,
        'the WM_ERASEBKGND event occurs before W
        '     M_LBUTTONUP,
        'and therefore will not be handled there
        '    
        frmMain.ResizeGridProportional
        m_bLMouseClicked = False
    End If
    'call the default message handler
    GridMessage = CallWindowProc(g_lngDefaultHandler, hwnd, Msg, wp, lp)
End Function


cheers
Andy
0
 

Author Comment

by:sycale
ID: 6374841
andysalih , you can do it with very short way. But thanks for trying to help me, i used couple your properties.
0
 
LVL 3

Expert Comment

by:andysalih
ID: 6374895
im glad to have been of some help.


cheers
Andy

:o)


0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now