Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Check Tedit (or anything else) twice or more

Posted on 2004-10-01
12
Medium Priority
?
216 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
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.

 
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 500 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

730 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