BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Validating text entered into a string grid

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage)
View previous topic :: View next topic  
Author Message
Jon Berndt
Guest





PostPosted: Thu Mar 31, 2005 12:33 am    Post subject: Validating text entered into a string grid Reply with 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.

Jon


Back to top
Damon Chandler (TeamB)
Guest





PostPosted: Thu Mar 31, 2005 6:35 am    Post subject: Re: Validating text entered into a string grid Reply with 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)
{
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,
--
Damon (TeamB)
C++Builder Developer's Journal
http://bcbjournal.com
BCB Commonly Asked Questions
http://bcbjournal.com/bcbcaq


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.

Back to top
Jon Berndt
Guest





PostPosted: Thu Mar 31, 2005 11:30 am    Post subject: Re: Validating text entered into a string grid Reply with quote



"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



Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.