Solved

Check Tedit (or anything else) twice or more

Posted on 2004-10-01
12
210 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

751 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