Link to home
Start Free TrialLog in
Avatar of sycale
sycale

asked on

Flex Greed

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.
ASKER CERTIFIED SOLUTION
Avatar of andysalih
andysalih

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of andysalih
andysalih

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
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
Avatar of sycale

ASKER

andysalih , you can do it with very short way. But thanks for trying to help me, i used couple your properties.
im glad to have been of some help.


cheers
Andy

:o)