Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 219
  • Last Modified:

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
0
wildzero
Asked:
wildzero
  • 3
  • 3
  • 2
  • +4
1 Solution
 
pritaeasSoftware EngineerCommented:
You could use a TStringList to store the values you want to check against and then use

if StringList.Find(Edit1.Text) then begin
  ...
end;
0
 
wildzeroAuthor Commented:
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.
0
 
esoftbgCommented:
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;
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
wildzeroAuthor Commented:
Yea I wouldn't be working with just Char, would be with full words (more then 1 char)
0
 
esoftbgCommented:
Sorry. It would not work with strings (full words) ....
0
 
pritaeasSoftware EngineerCommented:
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.
0
 
pritaeasSoftware EngineerCommented:
0
 
kretzschmarCommented:
>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 ;-)
0
 
vadim_tiCommented:
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
0
 
cqhallCommented:
I sometimes use an approach like...

if pos('|'+edit1.text+'|','|this|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.text)+'|',uppercase('|this|that|'))>0 then...

Also, I don't bother with this approach until there are several choices.

Chuck
0
 
Bart_ThomasCommented:
That's easy. Borland, finally, added some functions for this
purpose: StrUtils.pas

  if AnsiMatchText (Edit1.Text, ['this','that'] then // case insensitive
  begin
    ...
  end;

or use AnsiMatchStr for case sensitive string-comparing.

0
 
wildzeroAuthor Commented:
Perfect!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
  • 3
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now