| View previous topic :: View next topic |
| Author |
Message |
peted@iinet.net.au Guest
|
Posted: Tue Jun 22, 2004 1:24 pm Post subject: restricting input on Tedit control |
|
|
is there a way to restrict input to an edit box to numbers only, that
is the result should be a integer value only, say from 1 to 99999.
any help appreciated
thanks
|
|
| Back to top |
|
 |
Oren Halvani Guest
|
Posted: Tue Jun 22, 2004 1:24 pm Post subject: Re: restricting input on Tedit control |
|
|
OnKeyPress Event of you Edit:
void __fastcall TForm1::KeyPress(char &Key)
{
if(!('0' <= Key && Key <= '9') && !(Key == VK_BACK))
{
Key = 0;
}
}
Oren
|
|
| Back to top |
|
 |
Steve Aletto Guest
|
Posted: Tue Jun 22, 2004 1:43 pm Post subject: Re: restricting input on Tedit control |
|
|
| Quote: | is there a way to restrict input to an edit box to numbers only,
that
is the result should be a integer value only, say from 1 to 99999.
|
SetWindowLong(Edit1->Handle, GWL_STYLE, GetWindowLong(Edit1->Handle,
GWL_STYLE) | ES_NUMBER);
HTH,
Steve.
|
|
| Back to top |
|
 |
JD Guest
|
Posted: Tue Jun 22, 2004 2:26 pm Post subject: Re: restricting input on Tedit control |
|
|
"Steve Aletto" <steve_alettoANTI (AT) SPAMhotmail (DOT) com> wrote:
| Quote: | is there a way to restrict input to an edit box to numbers only,
that
is the result should be a integer value only, say from 1 to 99999.
SetWindowLong(Edit1->Handle, GWL_STYLE, GetWindowLong(Edit1->Handle,
GWL_STYLE) | ES_NUMBER);
|
That won't work. The OP want a range from 1 - 99999 (inclusive).
Not only will your solution allow a value of zero, it also
doesn't disallow a value greater than 99,999.
~ JD
|
|
| Back to top |
|
 |
JD Guest
|
Posted: Tue Jun 22, 2004 2:28 pm Post subject: Re: restricting input on Tedit control |
|
|
"Oren Halvani" <NoSpam (AT) fdtd (DOT) com> wrote:
| Quote: | OnKeyPress Event of you Edit:
void __fastcall TForm1::KeyPress(char &Key)
{
if(!('0' <= Key && Key <= '9') && !(Key == VK_BACK))
{
Key = 0;
}
}
|
That won't work. It doesn't account for a (text string) value
of zero and it doesn't check for a maximum of 99,999 as
perscribed by the OP.
~ JD
|
|
| Back to top |
|
 |
Steve Aletto Guest
|
Posted: Tue Jun 22, 2004 2:49 pm Post subject: Re: restricting input on Tedit control |
|
|
"JD" <nospam (AT) nospam (DOT) com> ha scritto nel messaggio
news:40d84194$1 (AT) newsgroups (DOT) borland.com...
| Quote: |
"Steve Aletto" <steve_alettoANTI (AT) SPAMhotmail (DOT) com> wrote:
is there a way to restrict input to an edit box to numbers only,
that
is the result should be a integer value only, say from 1 to
99999.
SetWindowLong(Edit1->Handle, GWL_STYLE,
GetWindowLong(Edit1->Handle,
GWL_STYLE) | ES_NUMBER);
That won't work. The OP want a range from 1 - 99999 (inclusive).
Not only will your solution allow a value of zero, it also
doesn't disallow a value greater than 99,999.
|
Don't criticize and propose your solution if you have one...
The question is quite approximate (e.g. there's no "result" for an
edit box), about the range, "say from 1 to 99999" could mean 0 to
1000000 is still OK.
Anyway:
In the constructor:
Edit1->MaxLength = 5;
SetWindowLong(Edit1->Handle, GWL_STYLE, GetWindowLong(Edit1->Handle,
GWL_STYLE) | ES_NUMBER);
In the OnExit event of Edit1:
if (Edit1->Text.ToIntDef(0) == 0) Edit1->SetFocus();
Happy now? It's just a little help, I'm note coding for him... Thanks.
Steve.
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Tue Jun 22, 2004 5:14 pm Post subject: Re: restricting input on Tedit control |
|
|
"Oren Halvani" <NoSpam (AT) fdtd (DOT) com> wrote
| Quote: | if(!('0' <= Key && Key <= '9') && !(Key == VK_BACK))
|
You have a small typo in that code:
if(!(('0' >= Key && Key <= '9') || (Key == VK_BACK)))
Alternatively:
if( ('0' < Key || Key > '9') && (Key != VK_BACK) )
Gambit
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Tue Jun 22, 2004 5:15 pm Post subject: Re: restricting input on Tedit control |
|
|
<peted (AT) iinet (DOT) net.au> wrote
| Quote: | is there a way to restrict input to an edit box to numbers
only, that is the result should be a integer value only, say
from 1 to 99999.
|
Use a TCSpinEdit instead, or alternatively attach a TUpDown to the TEdit and
then set its Min and Max values.
Gambit
|
|
| Back to top |
|
 |
Hans Galema Guest
|
Posted: Tue Jun 22, 2004 6:37 pm Post subject: Re: restricting input on Tedit control |
|
|
Remy Lebeau (TeamB) wrote:
| Quote: | if(!('0' <= Key && Key <= '9') && !(Key == VK_BACK))
You have a small typo in that code:
|
That is perfect code.
| Quote: | if(!(('0' >= Key && Key <= '9') || (Key == VK_BACK)))
|
Well that's a typo. That code does allow nothing.
| Quote: | Alternatively:
if( ('0' < Key || Key > '9') && (Key != VK_BACK) )
|
The same.
Hans.
|
|
| Back to top |
|
 |
William Charles Nickerson Guest
|
Posted: Tue Jun 22, 2004 7:07 pm Post subject: Re: restricting input on Tedit control |
|
|
"Hans Galema" <dontusethis (AT) dontusethis (DOT) nl> wrote
| Quote: | Remy Lebeau (TeamB) wrote:
if(!('0' <= Key && Key <= '9') && !(Key == VK_BACK))
You have a small typo in that code:
That is perfect code.
|
Yes, it is perfect, but unusually constructed (in my experience). What I
normally see is:
(Key >= '0' && Key <= '9')
which means the same. At a quick glance the above code would appear to be
wrong since both conditions are <= which would be an error if the variable
was always on the lhs.
I can see where that construct is actually quite elegant though, as it does
make the range stand out quite clearly (and I seem to recall that some
languages allow something akin to ('0' <= Key <= '9')).
Aloha
Bill
|
|
| Back to top |
|
 |
Hans Galema Guest
|
Posted: Tue Jun 22, 2004 7:10 pm Post subject: Re: restricting input on Tedit control |
|
|
William Charles Nickerson wrote:
| Quote: | I can see where that construct is actually quite elegant though, as it does
make the range stand out quite clearly (and I seem to recall that some
languages allow something akin to ('0' <= Key <= '9')).
|
That would be my language!
Hans.
|
|
| Back to top |
|
 |
JD Guest
|
Posted: Tue Jun 22, 2004 8:08 pm Post subject: Re: restricting input on Tedit control |
|
|
"Steve Aletto" <steve_alettoANTI (AT) SPAMhotmail (DOT) com> wrote:
Eat me. The entire purpose of these groups is for 'free exchange'.
Wouldn't you want to know if something isn't going to work for
you BEFORE you try it?
| Quote: | and propose your solution if you have one...
|
I thought that I had but it appears to not be there.
| Quote: | The question is quite approximate [...] "say from 1 to 99999"
could mean 0 to 1000000 is still OK.
|
Ok, but my point was that you still didn't include that as
part of your solution.
| Quote: | Edit1->MaxLength = 5;
SetWindowLong(Edit1->Handle, GWL_STYLE, GetWindowLong(Edit1->Handle,
GWL_STYLE) | ES_NUMBER);
In the OnExit event of Edit1:
if (Edit1->Text.ToIntDef(0) == 0) Edit1->SetFocus();
|
So now the user can't enter data in the order that he/she preferes and if they uses leading zeros (and they will
sooner or later), then they can't get to the max with out editing.
| Quote: | Happy now? It's just a little help, I'm note coding for him... Thanks.
|
Half a solution is not a solution.
~ JD
|
|
| Back to top |
|
 |
|