Page 1 of 1

check box in grid column

Posted: Wed Jul 29, 2020 11:22 pm
by pvn
I have a grid, and trying to have one column have a check box for all rows with

Code: Select all

wxGridCellAttr* attr;
  wxColour clr(51, 172, 255);
  attr = new wxGridCellAttr();
  attr->SetReadOnly(false);
  attr->SetBackgroundColour(clr);
  if (1) attr->SetEditor(new wxGridCellBoolEditor());
  if (1) attr->SetRenderer(new wxGridCellBoolRenderer());
  grid->SetColAttr(0, attr);
the check box displays , but when clicked does not change to checked

running the grid demo, it does
what am I missing here ?

Re: check box in grid column

Posted: Thu Jul 30, 2020 1:20 am
by ONEEYEMAN
Hi,
What platform? What wx version?
Can you see it in the grid sample?

Thank you.

Re: check box in grid column

Posted: Thu Jul 30, 2020 1:22 am
by pvn
windows 10, wxwidgets-3.1.3, 64bit build
yes, grid demo shows the check box fine (checked, not checked on click)

Re: check box in grid column

Posted: Thu Jul 30, 2020 1:38 am
by pvn
I was having

Code: Select all

grid->EnableEditing(false);
setting to true fixes it

Code: Select all

grid->EnableEditing(true);
related to this, I want to have the behavior of

1) also having text in the same cell
2) when I click one check box, all the other check boxes that have the *same* string also get selected

would this be possible, or is it too much to ask to wxwidgets ?
having text on the same cell could be skipped, an option would be to have the text in another column;
but the ability to have all the other check boxes selected (that have the same text ) remains

Re: check box in grid column

Posted: Thu Jul 30, 2020 1:40 am
by ONEEYEMAN
Hi,
So, what do you do differently from the sample?

Thank you.

Re: check box in grid column

Posted: Thu Jul 30, 2020 2:07 am
by ONEEYEMAN
Hi,
pvn wrote: Thu Jul 30, 2020 1:38 am I was having

Code: Select all

grid->EnableEditing(false);
setting to true fixes it

Code: Select all

grid->EnableEditing(true);
related to this, I want to have the behavior of

1) also having text in the same cell
You can always create your own renderer.
pvn wrote: Thu Jul 30, 2020 1:38 am 2) when I click one check box, all the other check boxes that have the *same* string also get selected
You can catch the appropriate event and then go thru all cells and check/uncheck the check box.
You just need to test how efficient it will be, depending on the number of cells.
pvn wrote: Thu Jul 30, 2020 1:38 am would this be possible, or is it too much to ask to wxwidgets ?
having text on the same cell could be skipped, an option would be to have the text in another column;
but the ability to have all the other check boxes selected (that have the same text ) remains
Thank you.

Re: check box in grid column

Posted: Thu Jul 30, 2020 2:19 am
by pvn
You can catch the appropriate event and then go thru all cells and check/uncheck the check box.
I do catch a click event for the column

Code: Select all

void PanelCurrent::OnCellLeftClick(wxGridEvent& ev)
{
  if (ev.GetCol() == underlying(cols_t::ActionTaken))
  {
but how to get access of the underlying control (the check box) so that I can explicitally make it checked ?

Re: check box in grid column

Posted: Thu Jul 30, 2020 4:19 am
by ONEEYEMAN
Hi,
Are they going to be in 1 row/1 column or it will be intermixed with other ones?
Anyway:

event.GetCol() - should give you the column where the click occur
event.GetRow() - should give ou the row
grid->GetCellEditor() - should give you the editor assigned to the cell

Cast it as appropriately and check the pointer validity.

But as I said - it all needs to be tested so that you don't lose the performance.

Maybe it is better to use wxDVC or wxListCtrl in report/virtual mode?

Thank you.

Re: check box in grid column

Posted: Thu Jul 30, 2020 4:08 pm
by pvn
the usage is like in the image: I want to add a check box to all the blue rows in the column;
I will give it a try to a custom renderer like in the grid demo
thanks

Re: check box in grid column

Posted: Thu Jul 30, 2020 5:10 pm
by doublemax
You need a wxGridCellBoolRenderer like in the sample.
In order to change the state of the checkbox, set the value of the cell: "0" for unchecked, "1" for checked

Re: check box in grid column

Posted: Thu Jul 30, 2020 6:42 pm
by pvn
ok, got it

Code: Select all

wxGrid* grid = new wxGrid(this, wxID_ANY);
  grid->CreateGrid(10, 3);
  grid->SetCellRenderer(3, 0, new wxGridCellBoolRenderer);
  grid->SetCellEditor(3, 0, new wxGridCellBoolEditor);
  grid->SetCellValue(3, 0, "1");

Re: check box in grid column

Posted: Mon Aug 03, 2020 9:58 am
by Nunki
Hi pvn,

The first time I used checkboxes in a wxGrid I experienced I had to click twice to check or uncheck the checkbox. The first click was to set the cell in focus, the second to actually select the checkbox control in the cell.
However Julian Smart sent me the code to actually make the checkbox work with a single click. Maybe this proves to be useful for you too.

Code: Select all

// Handling a single check on the first column-cells of the grid where checkboxes have been 
// displayed by using the boolean editor/renderer.  This to avoid a dubbel-click to select 
// or deselect the checkbox.  Courtesy to Julian Smart.
void tcGDoc::OnGridCheck(wxGridEvent& event)
{
 bool bSelected;
 short nAkls, nCkls, nArtAkls;
 int nCol, nRow, nGRow, nPos;
 long nAantal;
 wxString sTxt, sAkls, sCkls, sVpath, sSelVpath;
 wxGrid *hGrid;

 nCol = event.GetCol();
 nRow = event.GetRow();
 if (nCol == 0)
   {
    hGrid = XRCCTRL(*this, "DOCD_ARTCOMPONENTS", wxGrid);
    sTxt = hGrid->GetCellValue(nRow, 0);
    if ((sTxt.IsEmpty()) || (sTxt.Cmp("0") == 0))
      {
       hGrid->SetCellValue(nRow, 0, "1");
       bSelected = true;
      }
    else
      {
       hGrid->SetCellValue(nRow, 0, "");
       bSelected = false;
      }
    hGrid->Refresh();

    return;
   }

 event.Skip();
}
with of course the entry

Code: Select all

EVT_GRID_CELL_LEFT_CLICK( tcGDoc::OnGridCheck )
in the event table.

with regards,
Nunki

Re: check box in grid column

Posted: Mon Aug 03, 2020 10:45 am
by PB
BTW, I have noticed there have been recent changes in wxGridCellBoolEditor
https://github.com/wxWidgets/wxWidgets/ ... 86f902d7be
Normally wxGridCellEditor shows some UI control allowing the user to edit
the cell, but starting with wxWidgets 3.1.4 it's also possible to define
"activatable" cell editors, that change the value of the cell directly when
it's activated (typically by pressing Space key or clicking on it).