Thursday, November 10, 2016

Tick/Untick Checkboxes on Grid with AllowCellSelection = .F. - Part II

Last time I have shown how to employ ticking/unticking of checkboxes on Grid with AllowCellSelection = .F. via utilizing MouseDown event of Grid.  So what makes this different than the first trick?  The first trick relies on Relative Column of Grid.  And this poses a problem when the columns are reordered via dragging into new position as Relative Column  will then change.

So it is not that foolproof and only will work when you don't rearrange the column orders.  This one though fixes that as this is no longer based on the relative column's position but will be basing on the ControlSource of that column.



Here is the sample code:



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

Define Class Form1 As Form
      Height = 460
      Width = 400
      AutoCenter = .T.
      Caption = 'Ticking Checkbox on Grid with AllowSelection = .F. - PART II'
      ShowTips = .T.

      Add Object grid1 As Grid With ;
            GridLines = 0, ;
            Height = 380, ;
            Left = 10, ;
            Top = 70, ;
            Width = 390,;
            GridLines = 3,;
            DeleteMark = .F.,;
            ScrollBars = 2,;
            ColumnCount = 4,;
            AllowCellSelection = .F.


      Add Object label2 As Label With ;
            top = 10,;
            left = 15,;
            Height = 46,;
            caption = 'This is a simple trick on allowing the checkbox object to be ticked even though '+;
            "the grid's AllowCellSelection is set to .F.  The trick is on MouseDown",;
            WordWrap = .T.,;
            Width = 370,;
            Forecolor = RGB(255,0,0)

      Procedure Load
      Close Databases All
      Create Cursor junk (Products C(10), PickedUp L, Sold L, Returned L)
      Insert Into junk (Products) Values ('Apple')
      Insert Into junk (Products) Values ('Orange')
      Insert Into junk (Products) Values ('Mango')
      Insert Into junk (Products) Values ('Guava')
      Insert Into junk (Products) Values ('Banana')
      Go Top
      Endproc

      Procedure grid1.Init
      Local lnloop, lcColumn
      With This
            .RecordSourceType = 1
            .RecordSource = 'junk'
            .Column1.Header1.Caption = 'PRODUCTS'

            For lnloop = 2 To 4
               lcColumn = '.Column'+TRANSFORM(m.lnloop)
               WITH EVALUATE(m.lcColumn)
                        .AddObject("check1","checkbox")
                        .CurrentControl = "check1"
                        .Sparse = .F.
                        .check1.Caption = ''
                        .check1.Visible = .T.
                        .check1.BackStyle=0
                        .Alignment = 2
                        .Width = 93
                        .Header1.Caption = FIELD(m.lnloop)
                        .Header1.Alignment = 2
                  Endwith
            NEXT
      Endwith
      Endproc

      Procedure grid1.MouseDown
      Lparameters nButton, nShift, nXCoord, nYCoord

      Local lnWhere, lnRelRow, lnRelCol, loColumnsControl, lcSource, loColumn
      This.GridHitTest(m.nXCoord,m.nYCoord,@lnWhere,@lnRelRow,@lnRelCol)

      lcSource = ''
      For Each loColumn In This.Columns
            If loColumn.ColumnOrder = m.lnRelCol
                  lcSource = Upper(Getwordnum(loColumn.ControlSource,2,'.'))
            Endif
      Next
      Try
            Do Case
                  Case m.lcSource = "PICKEDUP"
                        Replace PickedUp With !PickedUp
                        WAIT WINDOW 'You toggled Pickedup Column' NOWAIT
                        This.Refresh
                  Case m.lcSource = "SOLD"
                        Replace Sold With !Sold
                        WAIT WINDOW 'You toggled Sold Column' NOWAIT
                        This.Refresh
                  Case m.lcSource = "RETURNED"
                        Replace Returned With !Returned
                        WAIT WINDOW 'You toggled Returned Column' NOWAIT
                        This.Refresh
            Endcase
      Catch
      Endtry
      Endproc

      Procedure Destroy
      Clear Events
      Endproc

ENDDEFINE


I have added a WAIT WINDOW here so you can visually see that it does change the proper underlying field.  Hope this comes useful to you!  Cheers!

No comments:

Post a Comment