wxGrid and validator Topic is solved
-
- Experienced Solver
- Posts: 96
- Joined: Fri Jun 17, 2005 1:53 pm
- Location: Germany
- Contact:
wxGrid and validator
Is there any method or way to create wxGrid with validator on each cell.
At the mement, wxGrid provides wxGridCellTextEditor, wxGriddCellNumberEditor,... for each cell.
How do i set the hex nuumber validor on each cell of the Grid?
At the mement, wxGrid provides wxGridCellTextEditor, wxGriddCellNumberEditor,... for each cell.
How do i set the hex nuumber validor on each cell of the Grid?
-
- wxWorld Domination!
- Posts: 1059
- Joined: Sun Dec 30, 2007 6:40 pm
- Location: Canada
Hi,
One choice is to derive a class from wxGridCellNumberEditor and override the IsAcceptedKey(wxKeyEvent& event) and StartingKey(wxKeyEvent& event) methods .
Here is a modification from the wxGridCellNumberEditor source code :
Hope that helps,
Jim
One choice is to derive a class from wxGridCellNumberEditor and override the IsAcceptedKey(wxKeyEvent& event) and StartingKey(wxKeyEvent& event) methods .
Here is a modification from the wxGridCellNumberEditor source code :
Code: Select all
bool myHexNumberEditor::IsAcceptedKey(wxKeyEvent& event)
{
if ( wxGridCellEditor::IsAcceptedKey(event) )
{
int keycode = event.GetKeyCode();
if ( (keycode < 128) &&
(wxIsxdigit(keycode) || keycode == '+' || keycode == '-'))
{
return true;
}
}
return false;
}
void myHexNumberEditor::StartingKey(wxKeyEvent& event)
{
int keycode = event.GetKeyCode();
if ((keycode < 128) && ( wxIsxdigit(keycode) || keycode == '+' || keycode == '-'))
{
wxGridCellTextEditor::StartingKey(event);
// skip Skip() below
return;
}
}
event.Skip();
}
Jim
OS: Vista SP1, wxWidgets 2.8.7.
-
- Experienced Solver
- Posts: 96
- Joined: Fri Jun 17, 2005 1:53 pm
- Location: Germany
- Contact:
-
- wxWorld Domination!
- Posts: 1059
- Joined: Sun Dec 30, 2007 6:40 pm
- Location: Canada
-
- Experienced Solver
- Posts: 96
- Joined: Fri Jun 17, 2005 1:53 pm
- Location: Germany
- Contact:
I have my own HexNumberEditor implemented as you said, but unfortunately it only works if the cell is currently selected and then key is pressed.
If i double click on the cell and starts writing again, editor doesnot call the overloaded functions. why??
If i double click on the cell and starts writing again, editor doesnot call the overloaded functions. why??
Code: Select all
class HexNumberEditor : public wxGridCellTextEditor
{
public:
bool IsAcceptedKey(wxKeyEvent& event);
void StartingKey(wxKeyEvent& event);
};
bool HexNumberEditor::IsAcceptedKey(wxKeyEvent& event)
{
if ( wxGridCellEditor::IsAcceptedKey(event) )
{
int keycode = event.GetKeyCode();
if ( (keycode < 128) && wxIsxdigit(keycode) )
{
return true;
}
}
return false;
}
void HexNumberEditor::StartingKey(wxKeyEvent& event)
{
int keycode = event.GetKeyCode();
if ((keycode < 128) && wxIsxdigit(keycode) )
{
wxGridCellTextEditor::StartingKey(event);
// skip Skip() below
return;
}
event.Skip();
}
HexNumberGridEditor* editor = new HexNumberGridEditor();
editor->SetParameters("6");
d_myGridCtrl->SetCellEditor(row, col, editor);
-
- Experienced Solver
- Posts: 96
- Joined: Fri Jun 17, 2005 1:53 pm
- Location: Germany
- Contact:
-
- wxWorld Domination!
- Posts: 1059
- Joined: Sun Dec 30, 2007 6:40 pm
- Location: Canada
-
- Experienced Solver
- Posts: 96
- Joined: Fri Jun 17, 2005 1:53 pm
- Location: Germany
- Contact:
-
- wxWorld Domination!
- Posts: 1059
- Joined: Sun Dec 30, 2007 6:40 pm
- Location: Canada
Not sure what you mean by the "is the same", what I suggested works fine for me. Perhaps you should post some code.MuhammadSohail wrote:My question is the same, the callback function only called if the cell is currently selected. If i double click on the cell and starts writing , this function doesnot invoked. Why??
Jim
OS: Vista SP1, wxWidgets 2.8.7.
-
- Experienced Solver
- Posts: 96
- Joined: Fri Jun 17, 2005 1:53 pm
- Location: Germany
- Contact:
The callback function only called if the cell is currently selected.
e.g.
---------------
---------------
If i double click on the cell and starts writing ,
---------------
0fg
---------------
Here 'g' is not allowed but the callback functions are not invoked to validate this character. Why??
My code is already attached in the previous post().
e.g.
---------------
---------------
If i double click on the cell and starts writing ,
---------------
0fg
---------------
Here 'g' is not allowed but the callback functions are not invoked to validate this character. Why??
My code is already attached in the previous post().
-
- wxWorld Domination!
- Posts: 1059
- Joined: Sun Dec 30, 2007 6:40 pm
- Location: Canada
Hi,
I guess I should have looked at this a bit more, I can replicate the behavior you're talking about.
There are a couple of solutions, both of which require that you install a wxValidator for the wxTextControl used for the cell.
You can get access to the wxTextControl using this->Text() inside of the HexNumberEditor object.
I.e.
You need to define a validator that only allows hex characters, and assign it to that object (see wxTextValidator for an example. http://docs.wxwidgets.org/stable/wx_wxt ... dator.html ).
The challenge is that the wxTextCtrl object does not exist until your edit control has been created, which happens the first time the cell is selected by the user.
So you either have to catch the EVT_GRID_CMD_EDITOR_CREATED event, which is raised after the wxTextCtrl is created, at which point you can assign the validator to it.
Or you can override the Create method for your HexNumberEditor
as follows:
Hope that helps,
Jim
I guess I should have looked at this a bit more, I can replicate the behavior you're talking about.
There are a couple of solutions, both of which require that you install a wxValidator for the wxTextControl used for the cell.
You can get access to the wxTextControl using this->Text() inside of the HexNumberEditor object.
I.e.
You need to define a validator that only allows hex characters, and assign it to that object (see wxTextValidator for an example. http://docs.wxwidgets.org/stable/wx_wxt ... dator.html ).
Code: Select all
this->Text()->SetValidator(new myHexValidator);
So you either have to catch the EVT_GRID_CMD_EDITOR_CREATED event, which is raised after the wxTextCtrl is created, at which point you can assign the validator to it.
Or you can override the Create method for your HexNumberEditor
as follows:
Code: Select all
void HexNumberEditor::Create(wxWindow* parent, wxWindowID id, wxEvtHandler* evtHandler)
{
wxGridCellTextEditor::Create(parent,id, evtHandler);
this->Text()->SetValidator(new myHexValidator);
}
Jim
OS: Vista SP1, wxWidgets 2.8.7.
-
- Experienced Solver
- Posts: 96
- Joined: Fri Jun 17, 2005 1:53 pm
- Location: Germany
- Contact:
Thanks a lot Jim Fairy, it works
HexNumberGridEditor class overides the Create() functiion and includes the following code
HexNumberGridEditor class overides the Create() functiion and includes the following code
Code: Select all
HexNumberGridEditor::Create(wxWindow* parent, wxWindowID id, wxEvtHandler* evtHandler)
{
wxArrayString includeList;
includeList.Add(wxT("0"));
includeList.Add(wxT("1"));
includeList.Add(wxT("2"));
includeList.Add(wxT("3"));
includeList.Add(wxT("4"));
includeList.Add(wxT("5"));
includeList.Add(wxT("6"));
includeList.Add(wxT("7"));
includeList.Add(wxT("8"));
includeList.Add(wxT("9"));
includeList.Add(wxT("a"));
includeList.Add(wxT("b"));
includeList.Add(wxT("c"));
includeList.Add(wxT("d"));
includeList.Add(wxT("e"));
includeList.Add(wxT("f"));
includeList.Add(wxT("A"));
includeList.Add(wxT("B"));
includeList.Add(wxT("C"));
includeList.Add(wxT("D"));
includeList.Add(wxT("E"));
includeList.Add(wxT("F"));
wxTextValidator textValidator(wxFILTER_INCLUDE_CHAR_LIST);
textValidator.SetIncludes(includeList);
wxGridCellTextEditor::Create(parent,id, evtHandler);
this->Text()->SetValidator( textValidator);
}