Page 1 of 1

How to add checkbox or switch button column to tableview

Posted: Friday 2nd December 2022 8:19am
by bored
I have a tableview and i will add one more column to it. This column need to have checkboxes or switchbutton or something else that can represent boolean value. And when checkbox's state changed i want to make some operation. I get values from database and will write to database when its state changed. How can i add this? The code part is like this:
WITH dbResult
tb.rows.count = .count
      i = 0
      WHILE i < .count
        tb[i, 0].Text = dbResult!name
        tb[i, 1].Text = dbResult!width
        tb[i, 2].Text = dbResult!height
        tb[i, 3].Text = dbResult!isValid
        i = i + 1
        .MoveNext
      WEND
END WITH


I use gambas 2.2 or something btw.

Re: How to add checkbox or switch button column to tableview

Posted: Friday 2nd December 2022 11:40am
by thatbruce
bored wrote: Friday 2nd December 2022 8:19am I use gambas 2.2 or something btw.
Very seriously, if that is true then if I were you I'd be looking to upgrade. I doubt anyone here could help. Version 2.2 was a very long time ago.

Re: How to add checkbox or switch button column to tableview

Posted: Friday 2nd December 2022 1:47pm
by cogier
Hi bored and welcome to the forum.

I have to agree with thatbruce. The Gambas version 2.23 was released in April 2011! We are now up to version 3.17.3.

What distro are you using?

Re: How to add checkbox or switch button column to tableview

Posted: Friday 2nd December 2022 4:52pm
by gambafeliz
There are tricks. For example use this:
SELECT CASE WHEN (Select sum(id) Block From Table1 Where id=Table2.id) > 0 THEN '☒' ELSE ' ' END AS  Block From ....


in a select from the database and receive a field from the record with the check or uncheck and when you click on the table you save the check uncheck in the database.

Re: How to add checkbox or switch button column to tableview

Posted: Friday 2nd December 2022 6:46pm
by BruceSteers
You could use the cells Picture property to be a checkbox picture , either selected or unselected.

See the code below.
First i create both selected and unselected checkbox pictures to use.
Then in column[1] i set each rows text and use the unselected checkbox picture for column[0].
the IsSelected() method checks if the picture in column[0] is the off image or not.
as the form closes it tells you the values of the checkboxes.

You can easily set values on load in the tableview checkboxes by using either the pOn or pOff pictures
You could easily use a different column number

Hope it helps...

' Gambas class file

Private pOff As New Picture(24, 24)
Private pOn As New Picture(24, 24)

Public Sub Form_Open()

  ' make our selected and unselected checkbox images
  Paint.Begin(pOff)
  Style.PaintCheck(0, 0, 24, 24, False)
  Paint.End

  Paint.Begin(pOn)
  Style.PaintCheck(0, 0, 24, 24, True)
  Paint.End

  TableView1.Columns.Count = 2

  For c As Integer = 0 To 9
    Inc TableView1.Rows.Count
    TableView1[TableView1.Rows.Max, 0].Picture = pOff
    TableView1[TableView1.Rows.Max, 1].Text = "Item " & c
  Next

  TableView1.Columns[0].Width = -1

End

Public Sub TableView1_Click()

  If TableView1.Column = 0 Then
    If IsSelected(TableView1.Current) Then
     ' Do your function if checking off
      TableView1.Current.Picture = pOff
    Else
     ' Do your function if checking on.
      TableView1.Current.Picture = pOn
    Endif
  Endif

End

Private Sub IsSelected(Cell As _GridView_Cell) As Boolean

  If Cell.Picture = pOff Then Return False
  Return True

End

Public Sub ButtonClose_Click()

  For c As Integer = 0 To TableView1.Rows.Max
    Print TableView1[c, 0].Text, IsSelected(TableView1[c, 0])
  Next

  Me.close

End


I guess something like this (put your condition in WhateverMakesItTrue)...

WITH dbResult
tb.rows.count = .count
      i = 0
      WHILE i < .count
        tb[i, 0].Picture = If(WhateverMakesItTrue, pOn, pOff)
        tb[i, 0].Text = dbResult!name
        tb[i, 1].Text = dbResult!width
        tb[i, 2].Text = dbResult!height
        tb[i, 3].Text = dbResult!isValid
        i = i + 1
        .MoveNext
      WEND
END WITH