We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

problem with edit box

boycoder
boycoder asked
on
Medium Priority
413 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;
Comment
Watch Question

MerijnBSr. Software Engineer
CERTIFIED EXPERT

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

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

Commented:
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
Software Engineer
CERTIFIED EXPERT
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
MerijnBSr. Software Engineer
CERTIFIED EXPERT

Commented:
Ah, anywhere in the text, I didn't read that from the question at first :p

Author

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

Author

Commented:
oops i posted too quick hang on let me try it above mate
MerijnBSr. Software Engineer
CERTIFIED EXPERT

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

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

Author

Commented:
Thanks very much mate, and thanks to all who have added info to this question, thanks for your time and effort.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.