ApplicationsProgramming SolutionsDelphi help - Simple code

14 Apr 2010, 09:44

Hi all,

im learning delphi, I need help with a simple piece of code
I have 3 off Tmemo fields

Tmemo1
Tmemo2
Tmemo3

What is the code for...
if tmemo1 = 'Input number of 10'
if tmemo2 = 'Input number of 20'

Tmemo3 = tmemo2+tmemo1
Tmemo3 = 30


My current code makes tmemo3 = 2010

Any help would be helpfull??

Rating 0 Comments 8
tobyb121
0
tobyb121 14 Apr 2010, 18:00 #
Try:
tmemo1=StrToInt('10')
tmemo2=StrToInt('20')

You need to convert the strings in the tmemo field to integers, in order to add them together as numbers, otherwise they are joined as two strings.
necko240
0
necko240 16 Apr 2010, 09:16 #
Thanks Toby, That helps me out:
This is what i was chasing...

Edit3.Text := IntToStr(StrToInt(Edit1.Text) + StrToInt(Edit2.Text));
necko240
0
necko240 17 Apr 2010, 13:56 #
hi need assistance on another issue,
This is my code so far,

if edit11.Text >= IntToStr(StrToInt(Edit2.Text)+1)then
edit11.Text := IntToStr(0)
else if edit10.Text >= IntToStr(StrToInt(Edit2.Text)+1)then
edit10.Text := IntToStr(0)
else if edit9.Text >= IntToStr(StrToInt(Edit2.Text)+1) then
edit9.Text := IntToStr(0)
else if edit8.Text >= IntToStr(StrToInt(Edit2.Text)+1)then
edit8.Text := IntToStr(0)
else if edit7.Text >= IntToStr(StrToInt(Edit2.Text)+1)then
edit7.Text := IntToStr(0)
else if edit6.Text >= IntToStr(StrToInt(Edit2.Text)+1)then
edit6.Text := IntToStr(0)


it works but will execute the first if statement which is true then ends, there may be 3 true answers and 3 false answers, what code will i need to execute all the if statements?
any help would be apriciated
SerialKiller
0
SerialKiller 17 Apr 2010, 20:50 #
you can try to use

BEGIN/END in ur statement blocks

or you can try

Case of statement
necko240
0
necko240 17 Apr 2010, 23:48 #
Thanks serialkiller, i have tried the following but when this is executed, It still only executes one if statement when it is true, I need it to run through all if staements regardless of true or false answers?

any suggestions on the code?

if edit11.Text > IntToStr(StrToInt(Edit2.Text)+1)then
begin
edit11.Text := IntToStr(0)
end
else if edit10.Text > IntToStr(StrToInt(Edit2.Text)+1)then
begin
edit10.Text := IntToStr(0)
end
else if edit9.Text > IntToStr(StrToInt(Edit2.Text)+1) then
begin
edit9.Text := IntToStr(0)
end
else if edit8.Text > IntToStr(StrToInt(Edit2.Text)+1)then
begin
edit8.Text := IntToStr(0)
end
else if edit7.Text > IntToStr(StrToInt(Edit2.Text)+1)then
begin
edit7.Text := IntToStr(0)
end
else if edit6.Text > IntToStr(StrToInt(Edit2.Text)+1)then
begin
edit6.Text := IntToStr(0)
end
else
tobyb121
0
tobyb121 18 Apr 2010, 16:22 #
The begin end is not required here, the way this is done is just to have a string of separate "if" statements, i.e.

if edit11.Text >= IntToStr(StrToInt(Edit2.Text)+1)then
edit11.Text := IntToStr(0)

if edit10.Text >= IntToStr(StrToInt(Edit2.Text)+1)then
edit10.Text := IntToStr(0)
if edit....etc.


Also I would have thought that you would need to do:

if StrToInt(edit10.Text) >= (StrToInt(Edit2.Text)+1) then

as comparing strings with < and > often leads to odd outcomes, depending on the language, some will compare the length, some will compare the binary value of the string and some will event put them in alphabetical order :m

tobyb
necko240
0
necko240 18 Apr 2010, 23:05 #
Thsnks toby this worked a treat, thannks again,

if StrToInt(edit10.Text) >= (StrToInt(Edit2.Text)+1) then
necko240
0
necko240 18 Apr 2010, 23:05 #
Thsnks toby this worked a treat, thanks again,

if StrToInt(edit10.Text) >= (StrToInt(Edit2.Text)+1) then
Reply

You have to login or register to post comments.

necko240
necko240
0 ♠ 0 ♣
Tweet:


Bookmark and Share