Problem with checkbox inside a grid
-
- Experienced Solver
- Posts: 74
- Joined: Thu Sep 25, 2014 7:40 pm
Problem with checkbox inside a grid
I am running wxWidgets 3.0.2 on Windows7-64.
One of the columns in my wxGrid is a checkbox that I have implemented using wxGridCellBoolRenderer and wxGridCellBoolEditor.
I then catch the OnGridSelectCell event to look for input. This works fine if the user clicks on the area of the cell which is just to the right of the checkbox (i.e. inside the checkbox cell, but not ON the checkbox itself). However, if the user clicks on the actual checkbox, no event gets generated (even though the checkbox value DOES toggle). The same is true for the OnGridCellLeftClick and OnGridCellChange events - no event gets generated if the checkbox itself is clicked (but events DO get generated if the outer-area is clicked).
By the way, the same behavior can be seen in the "grid" sample. If you click the "pink" area of cell A-4, the selection is displayed in the status area, but if you click the actual checkbox, it seems to be ignored.
Is this the expected behavior?
Is there some other event that I could trap to know when the user has clicked the actual checkbox?
Thanks.
One of the columns in my wxGrid is a checkbox that I have implemented using wxGridCellBoolRenderer and wxGridCellBoolEditor.
I then catch the OnGridSelectCell event to look for input. This works fine if the user clicks on the area of the cell which is just to the right of the checkbox (i.e. inside the checkbox cell, but not ON the checkbox itself). However, if the user clicks on the actual checkbox, no event gets generated (even though the checkbox value DOES toggle). The same is true for the OnGridCellLeftClick and OnGridCellChange events - no event gets generated if the checkbox itself is clicked (but events DO get generated if the outer-area is clicked).
By the way, the same behavior can be seen in the "grid" sample. If you click the "pink" area of cell A-4, the selection is displayed in the status area, but if you click the actual checkbox, it seems to be ignored.
Is this the expected behavior?
Is there some other event that I could trap to know when the user has clicked the actual checkbox?
Thanks.
-
- Earned some good credits
- Posts: 113
- Joined: Sat Sep 24, 2011 9:32 pm
- Location: Poland
Re: Problem with checkbox inside a grid
I am not quite sure if I understand correct, but I can advise to use "EditorHidden" event function (in my project it works but without check box, I have wxGridCellChoiceEditor)
Regards, Jarek
Code: Select all
void YourFrameObjectFrm::gridInfoEditorHidden(wxGridEvent& event)
{
unsigned short int Row = event.GetRow();
unsigned short int Col = event.GetCol();
//...
}
-
- Experienced Solver
- Posts: 74
- Joined: Thu Sep 25, 2014 7:40 pm
Re: Problem with checkbox inside a grid
Thanks for the suggestion, but that event doesn't trigger either.
To be more clear, I can click the checkbox (inside the grid cell) on and off without getting any event to trigger, however, if I click the area inside the cell (but outside the actual checkbox), various events will trigger as expected.
I want to be notified when the user clicks anywhere in the cell (inside the checkbox or not).
To be more clear, I can click the checkbox (inside the grid cell) on and off without getting any event to trigger, however, if I click the area inside the cell (but outside the actual checkbox), various events will trigger as expected.
I want to be notified when the user clicks anywhere in the cell (inside the checkbox or not).
-
- Earned some good credits
- Posts: 113
- Joined: Sat Sep 24, 2011 9:32 pm
- Location: Poland
Re: Problem with checkbox inside a grid
First of all: I think this is a bug that should be reported at http://trac.wxwidgets.org/
Second, i think you should receive a wxEVT_CHECKBOX event. Haven't tried it though.
Second, i think you should receive a wxEVT_CHECKBOX event. Haven't tried it though.
Use the source, Luke!
Re: Problem with checkbox inside a grid
If everthing else fails, you can catch the click from the editor control, seems to work but I don't know if this is the correct way:
Code: Select all
#include <wx/wx.h>
#include <wx/grid.h>
class MyFrame : public wxFrame
{
public:
MyFrame()
: wxFrame(NULL, wxID_ANY, ("Test"), wxDefaultPosition, wxSize(500,500))
{
m_grid = new wxGrid(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0);
m_grid->CreateGrid(10, 2);
m_grid->SetColFormatBool(boolColIdx);
m_grid->Bind(wxEVT_GRID_EDITOR_CREATED, &MyFrame::OnEditorCreated, this);
m_grid->Bind(wxEVT_GRID_CELL_LEFT_CLICK, &MyFrame::OnBoolCellLClick, this);
wxTextCtrl* textCtrl = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_READONLY | wxTE_RICH2);
wxLog::SetActiveTarget(new wxLogTextCtrl(textCtrl));
wxBoxSizer* sizer;
sizer = new wxBoxSizer(wxVERTICAL);
sizer->Add(m_grid, 4, wxALL | wxEXPAND, 5);
sizer->Add(textCtrl, 1, wxALL | wxEXPAND, 5);
SetSizer(sizer);
}
private:
enum
{
stringColIdx = 0,
boolColIdx = 1,
};
wxGrid* m_grid;
void OnEditorCreated(wxGridEditorCreatedEvent& e)
{
if ( e.GetCol() == boolColIdx )
{
wxLogMessage("Bool editor created.");
wxControl* control = e.GetControl();
if ( control )
control->Bind(wxEVT_LEFT_DOWN, &MyFrame::OnBoolEditorClicked, this);
}
}
void OnBoolCellLClick(wxGridEvent& e)
{
if ( e.GetCol() == boolColIdx )
wxLogMessage("Bool cell clicked.");
e.Skip();
}
void OnBoolEditorClicked(wxMouseEvent& e)
{
wxLogMessage("Bool editor clicked.");
e.Skip();
}
};
class MyApp : public wxApp
{
public:
virtual bool OnInit()
{
(new MyFrame)->Show();
return true;
}
};
wxIMPLEMENT_APP(MyApp);
-
- Experienced Solver
- Posts: 74
- Joined: Thu Sep 25, 2014 7:40 pm
Re: Problem with checkbox inside a grid
No, that doesn't work. Clicking the checkbox on/off doesn't cause the event to trigger.If anything else fails, catch the click from the editor control
That's an interesting idea, but how would I know which row/column in the grid was being clicked?I think you should receive a wxEVT_CHECKBOX event.
Moreover, how would I even bind to that event from a wxGrid control?
Remember that it's not really a checkbox control, it's a wxGridCellBoolEditor/wxGridCellBoolRenderer.
Re: Problem with checkbox inside a grid
Odd, it does work for me, i.e. the events I'm catching there are fired (see the GIF below). have you tried the exact code I posted?mbeardsley wrote:No, that doesn't work. Clicking the checkbox on/off doesn't cause the event to trigger.If anything else fails, catch the click from the editor control
Last edited by PB on Fri Jun 03, 2016 9:16 pm, edited 1 time in total.
Re: Problem with checkbox inside a grid
Current cursor position?That's an interesting idea, but how would I know which row/column in the grid was being clicked?
It's a "normal" wxCommandEvent which propagate upwards in the window hierarchy. Catch it the same way you would for a wxCheckbox.Moreover, how would I even bind to that event from a wxGrid control?
The editor contains a "real" wxCheckbox.Remember that it's not really a checkbox control, it's a wxGridCellBoolEditor/wxGridCellBoolRenderer.
Use the source, Luke!
-
- Experienced Solver
- Posts: 74
- Joined: Thu Sep 25, 2014 7:40 pm
Re: Problem with checkbox inside a grid
I tried various ways to work around the problem, (mostly trying to interface to the underlying checkbox) all of which seemed to have their own issues.
However, I found a much simpler way of doing what I want.
Rather than using a wxGridCellBoolEditor AND a wxGridCellBoolRenderer, I am now just using the wxGridCellBoolRenderer and the "normal" cell editor. This way, when any part of the cell is clicked, I get a cell selection event, but the cell is still rendered as a checkbox.
In my event handler, I simply toggle the cell value between "1" and "", and it seems to work fine.
Thanks for all of the replies.
However, I found a much simpler way of doing what I want.
Rather than using a wxGridCellBoolEditor AND a wxGridCellBoolRenderer, I am now just using the wxGridCellBoolRenderer and the "normal" cell editor. This way, when any part of the cell is clicked, I get a cell selection event, but the cell is still rendered as a checkbox.
In my event handler, I simply toggle the cell value between "1" and "", and it seems to work fine.
Thanks for all of the replies.