If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
bloodlee
Experienced Solver
Posts: 77 Joined: Thu Nov 30, 2006 10:49 am
Location: Shenzhen, CHN
Post
by bloodlee » Tue May 13, 2008 8:39 am
Hi, all. I met another problem about wxGrid.
Now, when I left click the rows of wxGrid, I want to get how many rows has been selected. So I use:
Code: Select all
BEGIN_EVENT_TABLE(RecordGrid, wxGrid)
EVT_GRID_CELL_LEFT_CLICK(RecordGrid::OnLeftClick)
END_EVENT_TABLE()
Code: Select all
void RecordGrid::OnLeftClick(wxGridEvent& event)
{
wxArrayInt rows = GetSelectedRows();
.....
event.Skip();
}
And in the function of OnLeftClick(), I used GetSelectedRows() to get the selected rows. But I found the wxArrayInt is not right, and I think the reason is I can not get the right rows before wxGrid, the base class, handles this event.
I really need to get the selected rows when the selection changes. Do you have any suggestions? Thanks.
JimFairway
wxWorld Domination!
Posts: 1059 Joined: Sun Dec 30, 2007 6:40 pm
Location: Canada
Post
by JimFairway » Tue May 13, 2008 11:24 am
Hi,
There are events that wxGrid generates when the selected cells are changed.
Here's the code snippet from the sample that demonstrates how to get the info you need:
Code: Select all
EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell )
EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected )
...
void GridFrame::OnSelectCell( wxGridEvent& ev )
{
wxString logBuf;
if ( ev.Selecting() )
logBuf << _T("Selected ");
else
logBuf << _T("Deselected ");
logBuf << _T("cell at row ") << ev.GetRow()
<< _T(" col ") << ev.GetCol()
<< _T(" ( ControlDown: ")<< (ev.ControlDown() ? 'T':'F')
<< _T(", ShiftDown: ")<< (ev.ShiftDown() ? 'T':'F')
<< _T(", AltDown: ")<< (ev.AltDown() ? 'T':'F')
<< _T(", MetaDown: ")<< (ev.MetaDown() ? 'T':'F') << _T(" )");
//Indicate whether this column was moved
if ( ((wxGrid *)ev.GetEventObject())->GetColPos( ev.GetCol() ) != ev.GetCol() )
logBuf << _T(" *** Column moved, current position: ") << ((wxGrid *)ev.GetEventObject())->GetColPos( ev.GetCol() );
wxLogMessage( wxT("%s"), logBuf.c_str() );
// you must call Skip() if you want the default processing
// to occur in wxGrid
ev.Skip();
}
void GridFrame::OnRangeSelected( wxGridRangeSelectEvent& ev )
{
wxString logBuf;
if ( ev.Selecting() )
logBuf << _T("Selected ");
else
logBuf << _T("Deselected ");
logBuf << _T("cells from row ") << ev.GetTopRow()
<< _T(" col ") << ev.GetLeftCol()
<< _T(" to row ") << ev.GetBottomRow()
<< _T(" col ") << ev.GetRightCol()
<< _T(" ( ControlDown: ")<< (ev.ControlDown() ? 'T':'F')
<< _T(", ShiftDown: ")<< (ev.ShiftDown() ? 'T':'F')
<< _T(", AltDown: ")<< (ev.AltDown() ? 'T':'F')
<< _T(", MetaDown: ")<< (ev.MetaDown() ? 'T':'F') << _T(" )");
wxLogMessage( wxT("%s"), logBuf.c_str() );
ev.Skip();
}
You could merge the two event handlers if you want and use
ev.GetEventType() to identify which event was raised.
Hope that helps,
Jim
OS: Vista SP1, wxWidgets 2.8.7.
lester
Filthy Rich wx Solver
Posts: 211 Joined: Sat Sep 02, 2006 7:24 pm
Location: Ukraine
Post
by lester » Tue May 13, 2008 2:09 pm
Code: Select all
wxArrayInt res;
for( int i = 0 ; i < pGrid->GetNumberRows() ; ++i )
{
if( pGrid->IsInSelection( i, 0 ) )
res.Add( i );
}
bloodlee
Experienced Solver
Posts: 77 Joined: Thu Nov 30, 2006 10:49 am
Location: Shenzhen, CHN
Post
by bloodlee » Wed May 14, 2008 2:34 am
Thanks, Jim and lester.
I've tried your methods, but the problem still exists.
JimFairway
wxWorld Domination!
Posts: 1059 Joined: Sun Dec 30, 2007 6:40 pm
Location: Canada
Post
by JimFairway » Wed May 14, 2008 10:36 am
Hi,
Which problem still exists?
Do you have the sample code: wx/samples/grid?
Based on your initial problem description, it looks like it shows how to do exactly what you want without having to use a
EVT_GRID_CELL_LEFT_CLICK event.
Jim
OS: Vista SP1, wxWidgets 2.8.7.
bloodlee
Experienced Solver
Posts: 77 Joined: Thu Nov 30, 2006 10:49 am
Location: Shenzhen, CHN
Post
by bloodlee » Thu May 15, 2008 12:57 am
Hi,
I compiled the sample/grid and read the src. And I change the EVT_macro to EVT_GRID_SELECT_CELL and EVT_GRID_RANGE_SELECT.
But I can't get the right selected rows in the event handler, even I used the method which lester said.
JimFairway
wxWorld Domination!
Posts: 1059 Joined: Sun Dec 30, 2007 6:40 pm
Location: Canada
Post
by JimFairway » Thu May 15, 2008 1:19 am
Hi,
I'm presuming the sample worked. Can you post the code you're having trouble with?
Jim
OS: Vista SP1, wxWidgets 2.8.7.