Monday, March 2, 2015

Fake Checkbox on Grid

As most of my posts, this one also comes from a need of a member inside Foxite forum.  His desire is to have a uniquely looking checkbox and so my advice is to utilize two images representing checkboxes, one that is ticked and the other not ticked.  Then to manipulate viewing of the image via  DynamicCurrentControl.



In my mind, even without testing yet, I believe it will work good.  But I was notified later that that trick requires two clicks and so I tested and it did needs two clicks as follows:

1st click - to set focus and activate the cell
2nd click onwards - actual toggling of the underlying logical field

And I agree that if we can get around the issue of the first click, then that would be better.  However, I cannot find a property or event  to activate a cell using click and then perform click on the image as well in one go.  But believing  there is always a way, I tried other ideas.  And as I found a way, then here it is again, maybe you'll need something like this:



loTest = Createobject("Sample")
loTest.Show(1)

Define Class Sample As Form
      Caption = 'DynamicCurrentControl'

      Add Object grid1 As Grid With ColumnCount = 2


      Procedure Load
      Create Cursor junk (x C(1), Y l)
      For lnloop = 1 To 26
            Insert Into junk Values (Chr(m.lnloop+64),.F.)
      Next
      Go Top
      Endproc

      Procedure grid1.Init
      Local lcImage, lcFile
      With This
            .RecordSourceType= 1
            .RecordSource='junk'
            .Column1.ControlSource = 'junk.x'

            With .Column2
                  .ControlSource='junk.y'
                  .AddObject("ImgChk","MyImage")
                  .AddObject("ImgNoChk","MyImage")
                  .Width = 24
                  With .imgChk
TEXT TO lcImage NOSHOW
/9j/4AAQSkZJRgABAAEAYABgAAD//gAfTEVBRCBUZWNobm9sb2dpZXMgSW5jLiBWMS4wMQD/2wCEAAgFBgcGBQgHBgcJCAgJDBQNDAsLDBgREg4UHRkeHhwZHBsgJC4nICIrIhscKDYoKy8xMzQzHyY4PDgyPC4yMzEBCAkJDAoMFw0NFzEhHCExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMf/EAaIAAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKCwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoLEAACAQMDAgQDBQUEBAAAAX0BAgMABBEFEiExQQYTUWEHInEUMoGRoQgjQrHBFVLR8CQzYnKCCQoWFxgZGiUmJygpKjQ1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4eLj5OXm5+jp6vHy8/T19vf4+foRAAIBAgQEAwQHBQQEAAECdwABAgMRBAUhMQYSQVEHYXETIjKBCBRCkaGxwQkjM1LwFWJy0QoWJDThJfEXGBkaJicoKSo1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoKDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uLj5OXm5+jp6vLz9PX29/j5+v/AABEIABIAFQMBEQACEQEDEQH/2gAMAwEAAhEDEQA/APVvEmtatD4it9H0eTTbXNm93Nc36O6gB0QKArLgkt1J/CgDG8WeJfE3hG0gudUv9DuJLiUQ21lbWM3n3TkgBEzKcHnqRge/AIB0HgG81DU9BXU9UvY7ia8dnEUKBYrYAlfLU4y2CDliTk9MDFAGP4o03TtZ8ef2VrKo1neaDNG6swXP7+M5B9QcEHscUAcz8NPAd/p3iy41Txjqw1IaODaaM00wf9318zGeODtHoc+goA7X4UEHwDppUggmYgjoR5z80Abmr6LpWsJGur6ZZ36xElBdQJKEJ643A4oAzP8AhCPCf/Qr6J/4L4v/AImgDft4YraCOC3iSGGNQqRooVVA6AAdBQB//9k=
ENDTEXT
                        lcFile = Addbs(Getenv("TEMP"))+"check"
                        Strtofile(Strconv(m.lcImage,14),m.lcFile)
                        .Picture = m.lcFile
                        .Visible = .T.
                  Endwith

                  With .imgNoChk
TEXT TO lcImage NOSHOW
/9j/4AAQSkZJRgABAQEAYABgAAD//gAfTEVBRCBUZWNobm9sb2dpZXMgSW5jLiBWMS4wMQD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcGBwcICQsJCAgKCA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N5EuY/Jlm1ho5IfuTL/bF7hh9RRQB7Z8Wv2ZPht+0rZafB8Rvh74H8fwaXdXDWcfiTQrXVUtCWYExidHCEgAHbjOBXB/8Osv2Yv8Ao3L4D/8AhAaT/wDGKKKAPbPAvhnTfBnhbTdJ0fT7HSdK06zigtbOzgSC3to1BCokaAKqgdAAAKKKKAP/2Q==
ENDTEXT
                        lcFile = Addbs(Getenv("TEMP"))+"Nocheck"
                        Strtofile(Strconv(m.lcImage,14),m.lcFile)
                        .Picture = m.lcFile
                        .Visible = .T.
                  Endwith

                  .DynamicCurrentControl='IIF(junk.y=.F.,[ImgNoChk],[ImgChk])'
                  .Sparse = .F.

            Endwith
            .RowHeight = 22
      Endwith
      Endproc

      Procedure grid1.AfterRowColChange
      Lparameters nColIndex
      If This.ActiveColumn = 2
            Replace Y With !Y In junk
      Endif

Enddefine

Define Class MyImage As Image
      Procedure Click
      Replace Y With !Y In junk
      Endproc
Enddefine


The final trick on the single click need, if you have not noticed, lies on usage of the AfterRowColChange event of the grid. Hope this helps!  Cheers!

No comments:

Post a Comment