wildzero
asked on
Check Tedit (or anything else) twice or more
Hey there :)
Ok, lets say I have a Tedit component named, Edit1
Now if I want to check the value of this to see if it = something you just called
If edit1.text = 'this' then do this
Now what if I want to check it against two things, do I have to do
If (edit1.text = 'this') or If (edit1.text = 'that') then do this
Or is there someway of doing
If edit1.text in ['this','that'] that would work alot better.
Just looking for a 'cleaner' way to code.
Cheers
Nick
Ok, lets say I have a Tedit component named, Edit1
Now if I want to check the value of this to see if it = something you just called
If edit1.text = 'this' then do this
Now what if I want to check it against two things, do I have to do
If (edit1.text = 'this') or If (edit1.text = 'that') then do this
Or is there someway of doing
If edit1.text in ['this','that'] that would work alot better.
Just looking for a 'cleaner' way to code.
Cheers
Nick
ASKER
Yea, but wouldn't that use mode code
as you have to call
the variable, then create the string list, assign the values and then free the stringgrid.... interesting answer though.
as you have to call
the variable, then create the string list, assign the values and then free the stringgrid.... interesting answer though.
procedure TForm1.Button1Click(Sender : TObject);
type
TCharMode = (cmNone, cmAlpha, cmNum);
var
C: Char;
CharMode: TCharMode;
begin
C := '7';
if (C in ['0'..'9']) then
CharMode := cmNum
else
CharMode := cmAlpha;
if CharMode in [cmAlpha, cmNum] then
ShowMessage(' in [] workks only with Ordinal types');
end;
type
TCharMode = (cmNone, cmAlpha, cmNum);
var
C: Char;
CharMode: TCharMode;
begin
C := '7';
if (C in ['0'..'9']) then
CharMode := cmNum
else
CharMode := cmAlpha;
if CharMode in [cmAlpha, cmNum] then
ShowMessage(' in [] workks only with Ordinal types');
end;
ASKER
Yea I wouldn't be working with just Char, would be with full words (more then 1 char)
Sorry. It would not work with strings (full words) ....
You could create a Function Accepting Edit1.Text as the first parameter, and your options as the rest (variable number of params). Then you could program the function to perform a check using a loop through the params. Is there a maximum to your number of possibilities to check? In any option, you'll have to either pass your test-strings as parameters or put them into an array/stringlist/whatever to have them checked.
>If (edit1.text = 'this') or If (edit1.text = 'that') then
should be
If (edit1.text = 'this') or (edit1.text = 'that') then
and this is the easiest (but static) method
meikl ;-)
should be
If (edit1.text = 'this') or (edit1.text = 'that') then
and this is the easiest (but static) method
meikl ;-)
const
a : array [1..3] of string = ('s1', 's2', 's3');
found := false;
for i:=low(a) to high(a) do
if Edit1.Text = s[i] then begin
found := true;
break;
end
a : array [1..3] of string = ('s1', 's2', 's3');
found := false;
for i:=low(a) to high(a) do
if Edit1.Text = s[i] then begin
found := true;
break;
end
I sometimes use an approach like...
if pos('|'+edit1.text+'|','|t his|that|' )>0 then...
I use the vertical bars ("splats") to ensure that edit1.text isn't a partial match within "this" or "that". This approach also makes it easy to do a case insensitive comparison...
if pos('|'+uppercase(edit1.te xt)+'|',up percase('| this|that| '))>0 then...
Also, I don't bother with this approach until there are several choices.
Chuck
if pos('|'+edit1.text+'|','|t
I use the vertical bars ("splats") to ensure that edit1.text isn't a partial match within "this" or "that". This approach also makes it easy to do a case insensitive comparison...
if pos('|'+uppercase(edit1.te
Also, I don't bother with this approach until there are several choices.
Chuck
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Perfect!
if StringList.Find(Edit1.Text
...
end;