How to add checkbox or switch button column to tableview

New to Gambas? Post your questions here. No question is too silly or too simple.
Post Reply
bored
Posts: 1
Joined: Friday 2nd December 2022 8:04am

How to add checkbox or switch button column to tableview

Post 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.
User avatar
thatbruce
Posts: 161
Joined: Saturday 4th September 2021 11:29pm

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

Post 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.
Have you ever noticed that software is never advertised using the adjective "spreadable".
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

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

Post 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?
User avatar
gambafeliz
Posts: 139
Joined: Friday 2nd September 2022 7:50pm
Location: I live in the same city as Picasso

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

Post 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.
For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you. :)
User avatar
BruceSteers
Posts: 1523
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

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

Post 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

If at first you don't succeed , try doing something differently.
BruceS
Post Reply