Solved

Check Tedit (or anything else) twice or more

Posted on 2004-10-01
12
204 Views
Last Modified: 2010-04-05
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
Comment
Question by:wildzero
  • 3
  • 3
  • 2
  • +4
12 Comments
 
LVL 6

Expert Comment

by:pritaeas
ID: 12198571
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
 
LVL 10

Author Comment

by:wildzero
ID: 12198580
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
 
LVL 12

Expert Comment

by:esoftbg
ID: 12198585
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
 
LVL 10

Author Comment

by:wildzero
ID: 12198609
Yea I wouldn't be working with just Char, would be with full words (more then 1 char)
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12198639
Sorry. It would not work with strings (full words) ....
0
 
LVL 6

Expert Comment

by:pritaeas
ID: 12198651
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 6

Expert Comment

by:pritaeas
ID: 12198655
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 12198676
>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
 
LVL 6

Expert Comment

by:vadim_ti
ID: 12198687
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
 
LVL 2

Expert Comment

by:cqhall
ID: 12200499
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
 
LVL 1

Accepted Solution

by:
Bart_Thomas earned 125 total points
ID: 12202273
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
 
LVL 10

Author Comment

by:wildzero
ID: 12224922
Perfect!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now