Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

problem with edit box

Posted on 2011-04-27
10
Medium Priority
?
366 Views
Last Modified: 2012-05-11
Hi, i want to be able to show message "error" if when button1 is pressed...  the memo1.text has any of the following..

S, U, s, u, $, &

Ive tried and failed in my test.


  if  memo1.text=('s' ,'u')
  then begin
  showmessage('error');
  end;
0
Comment
Question by:boycoder
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 19

Expert Comment

by:MerijnB
ID: 35475512
try this:
if (Edit1.Text = 's') or (Edit1.Text = 'u') or (Edit1.Text = 'S') or (Edit1.Text = 'U') or (Edit1.Text = '$') or (Edit1.Text = '&') then
  then begin
  showmessage('error');
  end;

Open in new window

0
 
LVL 12

Expert Comment

by:Hypo
ID: 35475539
your main problem is that you are using := for comparison, when := is actually the assign operator... When you compare things in delphi you should use only the equals sign =

however, you cant either compare a string to several characters like that.... since they are of different types... (you are trying to compare a string with a set of chars, and there are no default comparison operator/function to do that...

there are however different ways to check if a string contains a char... one way is to use the Pos-function, but in your case I think it would be simplier to use the LastDelimiter function, which returns the last index any of the delimiter chars resides in the string... (if none of the delimiters exist it should return zero, unless I dont remember correct)...

try this code below instead...

/Hypo
if LastDelimiter('SsUu$&', Edit1.text) <> 0 then 
  showmessage('error');

Open in new window

0
 
LVL 12

Expert Comment

by:Hypo
ID: 35475578
Sorry, You can forget the first part of my post about the assign operator... for some reason your code turned up with := instead of = on my screen when I wrote my reply... but now I see that you didn't use :=

anyway, the rest of my post is still valid though...

/Hypo
0
Technology Partners: 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 32

Accepted Solution

by:
Ephraim Wangoya earned 2000 total points
ID: 35475588
try
procedure TForm1.Button1Click(Sender: TObject);
var
  I: Integer;
  S: string;
begin
  S := Memo1.Text;
  for I := 1 to Length(S) do
  begin
    if S[I] in ['S', 'U', 's', 'u', '$', '&'] then
    begin
       ShowMessage('Error');
       Break;
    end;
  end;
end;

Open in new window

0
 
LVL 19

Expert Comment

by:MerijnB
ID: 35475621
Ah, anywhere in the text, I didn't read that from the question at first :p
0
 

Author Comment

by:boycoder
ID: 35475623
Thanks both of you, however i should have said that in the memo are many other things, like this


GDFGF-RYRTU-4557R-FKGKH-DSJD4
HDFJK-W34344-DCFRF-FREFREF-FRERFF
u

i want it so that if those bad letters/numbers i posted in the first post are found,show the error, as the code i tried didnt work. Cheers guys
0
 

Author Comment

by:boycoder
ID: 35475637
oops i posted too quick hang on let me try it above mate
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 35475652
ewangoya's code will work, a nicer solution would be to add this to the OnKeyPress event of the TEdit, it will prevent the user from adding the keys in the first place:
procedure TForm3.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
 if (Key in ['S', 'U', 's', 'u', '$', '&']) then
  key := #0;
end;

Open in new window

0
 
LVL 12

Expert Comment

by:Hypo
ID: 35475673
Ahh, I used Edit1.Text instead of Memo1.Text... I misread what components you were using...

This is what I should have suggested...
if LastDelimiter('SsUu$&', Memo1.text) <> 0 then
  showmessage('error');

Open in new window

0
 

Author Closing Comment

by:boycoder
ID: 35475707
Thanks very much mate, and thanks to all who have added info to this question, thanks for your time and effort.
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

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

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…
This is an update to some code that someone else posted on Experts Exchange. It is an alternate approach, I think a little easier to use, & makes sure that things like the Task Bar will update.
Integration Management Part 2
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

564 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