Board index » cppbuilder » Validating text entered into a string grid

Validating text entered into a string grid


2005-03-31 08:33:47 AM
cppbuilder11
I'd like to make sure that only a valid floating point (or int) number gets
entered into a string grid cell. The Edit Mask I've always found to be a
joke for editing most things I'm interested in (like floats and ints of
unknown magnitude). All of the different handlers seem a bit overwhelming
and I'm confused as to which one to use. I tried a couple and there didn't
seem to be a way to reject a bad character.
Jon
 
 

Re:Validating text entered into a string grid

Hi Jon,
You can post-validate what the user entered by using a combination of
the OnKeyPress, OnSelectCell, and OnExit events; here's a rough example...
bool IsValidEntry(TStringGrid& Grid)
{
try
{
AnsiString const entry =
Grid.Cells[Grid.Col][Grid.Row];
if (entry.Length()>0)
{
entry.ToDouble();
}
}
catch (EConvertError& e)
{
Application->ShowException(&e);
return false;
}
return true;
}
// OnSelectCell event handler
void __fastcall TForm1::StringGrid1SelectCell(
TObject *Sender, int ACol, int ARow,
bool &CanSelect)
{
if (StringGrid1->EditorMode)
{
CanSelect = IsValidEntry(*StringGrid1);
}
}
// OnKeyPress event handler
void __fastcall TForm1::
StringGrid1KeyPress(TObject *Sender, char &Key)
{
if (Key == '\r' && StringGrid1->EditorMode &&
!IsValidEntry(*StringGrid1))
{
Key = 0;
}
}
// OnExit event handler
void __fastcall TForm1::
StringGrid1Exit(TObject *Sender)
{
if (!IsValidEntry(*StringGrid1))
{
ActiveControl = StringGrid1;
PostMessage(StringGrid1->Handle,
WM_KEYDOWN, VK_F2, 0);
}
}
You can also pre-validate by filtering keystrokes and pasting events,
but this will likely become annoying to user (especially wiithout proper
feedback of the error condition). If you want to take this latter
approach, I suggest creating a TInplaceEdit descendant and overriding
the KeyPress() method and handling the WM_PASTE message (let me know if
you need an example).
Good luck,
Jon Berndt wrote:
Quote
I'd like to make sure that only a valid floating point (or int) number gets
entered into a string grid cell. The Edit Mask I've always found to be a
joke for editing most things I'm interested in (like floats and ints of
unknown magnitude). All of the different handlers seem a bit overwhelming
and I'm confused as to which one to use. I tried a couple and there didn't
seem to be a way to reject a bad character.
 

Re:Validating text entered into a string grid

"Damon Chandler (TeamB)"
Quote
Hi Jon,
You can post-validate what the user entered by using a combination of
the OnKeyPress, OnSelectCell, and OnExit events; here's a rough example...

bool IsValidEntry(TStringGrid& Grid)
{
...
snipped
Thanks, Damon:
I ended up find a way that was slightly different, but used some of the same
ideas you presented. The idea was that I wanted to use what was in my grid
of floating point numbers (really a set of X and Y coordinates) to do a live
update of a teeChart graph. I darn well wanted to make sure I was only
passing valid numbers to the chart component. So, I had this event function
void __fastcall TFormGain::SchedGainGridSetEditText(TObject *Sender,
int ACol, int ARow, const AnsiString Value)
{
if (Value.IsEmpty() ||
Value == "-" ||
Value == "-." ||
Value == ".") return; // ignore these combinations
try {
Value.ToDouble();
} catch (...) {
ShowMessage("This is NOT a valid number");
SchedGainGrid->Cells[ACol][ARow] = "0.0";
}
DrawGraph();
}
And my DrawGraph() function now works cleanly:
void TFormGain::DrawGraph(void)
{
Chart1->Series[0]->Clear();
for (int i=1; i<SchedGainGrid->RowCount; i++) {
Chart1->Series[0]->AddXY(
SchedGainGrid->Cells[0][i].ToDouble(),
SchedGainGrid->Cells[1][i].ToDouble(),
"", clBlack);
}
}
One way to make this whole setup better would be to save the last valid
number entered and restore to that value if a bad value was entered.
Thanks,
Jon
 

{smallsort}