Solved

Check Tedit (or anything else) twice or more

Posted on 2004-10-01
12
206 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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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
 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

809 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