Thursday, July 5, 2012

Grid Trick# 5 - Column Moved or Header Simply Clicked?

A question inside foxite is raised just now and it took my fancy on trying to find out how to differentiate if user simply clicks on the header of a column or has moved it.  Until I realize I was looking at the wrong things because really it comes only with two simple rules:

  1. If you wanted to do something when you click the header, then use header's click event
  2. If you wanted to do something when you reposition column, then use column's moved event
Here is a simple sample for that:



Local oForm
oForm=Newobject("Form1")
oForm.Show
Read Events
Return

Define Class Form1 As Form
    Height = 390
    Width = 500
    AutoCenter = .T.
    Caption = 'Column Moved or Header Simply Clicked?'

    Add Object grid1 As Grid With ;
        GridLines = 0, ;
        Height = 328, ;
        Left = 10, ;
        Top = 50, ;
        Width = 480,;
        GridLines = 3,;
        DeleteMark = .F.,;
        GridLineColor = Rgb(192,192,192),;
        FontName = 'Tahoma',;
        FontSize = 8, Anchor = 15, AllowCellSelection = .F.


    Procedure Load
        Set Talk Off
        Set Safety Off
        Close Databases All
        Select  company,contact,Title From (Home(2)+"data\customer") Where Recno() < 50 Into Cursor junk
        Use In Select('customer')
    Endproc

    Procedure grid1.Init
        With This
            .RecordSourceType = 6
            .RecordSource = 'junk'
            .Column1.Header1.Caption = 'Column1'
            .Column2.Header1.Caption = 'Column2'
            .Column3.Header1.Caption = 'Column3'
            .SetAll('Width',150,'Column')
            For lnloop = 1 To .ColumnCount
                Bindevent(.Columns(m.lnloop),'Moved',Thisform,'_Moved')
            Next
        Endwith
    Endproc

    Procedure _Moved
        Messagebox('Moved!')
    Endproc

    Procedure Destroy
        Clear Events
    Endproc

Enddefine


No comments:

Post a Comment