This question is an extension of another question:
TEdit Input Validation on C++ Builder XE8
I have an editable TStringGrid
. I only want the user to type numbers and a maximum of one decimal point or comma for each cell in the grid.
From the above link, I understand how to permit only certain keys, but not how to keep track of how many times a certain key-value already exists in the given cell.
From the above link, I have this:
void __fastcall TSetDataForm::ProbabilityGridKeyPress(TObject *Sender, System::WideChar &Key)
{
if( Key == VK_BACK )
return;
if( (Key < L'0') || (Key > L'9') )
{
ShowMessage("Please enter numerals only");
Key = 0;
}
}
How do I allow '.'
or ','
but only once?
I would suggest using
TryStrToFloat()
to validate the input, then there is no question whether the user is entering a valid decimal string or not. You would just need to handle the extra cases where:the user is typing a character somewhere other than the end of the edit field, with or without text selected.
the user is copy/pasting text into the editor.
For example: