Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TEdit entry validation 2

Status
Not open for further replies.

Dhagaxtuur

Technical User
Apr 15, 2003
47
0
0
CA
I need help validating entry of TEDIT->TEXT. How do I make sure user enters numbers with decimal points. Example: 29.95. If the user enters char, I want display error message or ignore it. Please help. Thanks
 
In the TDEIT->OnKeyPress event you can get the key pressed BEFORE it's entered. If you don't like the key just alter the 'Key' variable to 0 (zero) and it will "dissapear".

if(!strchr("0123456789.\r\n\x08",Key)) Key = 0;
Means: if key NOT in 0,1,2,3,4,5... set key to zero.
You might want to take in backspace keys and such to allow some edition.
\x08 = backspace
\r = return
\n = linefeed

Totte
Keep making it perfect and it will end up broken.
 
Thank you very much Totte. That was the solution. Great
 
How would you check for only two decimal places.

Thanks. Bernie.
 
it might require some fine tuning but I
got it to work OK.

Code:
int precision = 2;
int precision_count = 0;
bool stop = false;

void __fastcall Ttable::Edit2KeyPress(TObject *Sender, char &Key)
{
    if (stop)
        Key = 0;
    else if(!strchr("0123456789.\r\n\x08",Key))
        Key = 0;
    else if (Key == '.')
    {
        if (precision_count == 0)
            precision_count++;
        else
            Key = 0;
    }
    else if (precision_count > 0)
        precision_count++;  

    if (precision_count == precision + 1)
        stop = true; 
}

TC
 
Thanks Butthead,

This works well except once stop is true you cant arrow back and modify the entry.

Maybe if i clear the field when they backspace and reset the counters.

Thanks Bernie.
 
its a start.

it can be adapted to fit many specific instances.

you could also put

if(Key == '\b')

at the beginning to handle the backspace operation.
remember to handle the precision counter to decrement
this value when necessary.

the arrow key I think will be handled in the onKeydown
or up methods as you will test for the virtual key codes
there.

TC
 
Thanks TC (Top Cat)

Yes its a great start and i should be able to sort something out.

Thanks Again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top