Recent

Author Topic: If, Then, Else Statements?  (Read 11850 times)

A

  • Guest
If, Then, Else Statements?
« on: December 27, 2017, 04:47:30 pm »
Hello :)

It's my second day of trying to learn Object Pascal and I'm trying to practice some If, Then, Else statements. However, the code doesn't work (and it doesn't show any errors either). I have reread the code multiple times but I can't figure out what's wrong with it:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit2Change(Sender: TObject);
  2. begin
  3.   If Edit2.Text = ''
  4.   Then Button1.Enabled := False
  5.   Else Button1.Enabled := True
  6.     End;

What this code is supposed to accomplish is enabling a button only if there is text in the textbox, however, the button remains disabled even when the button has anything other than '' (nothing) inside it.

Can anyone tell me what's wrong with the code (without explicitly giving me the code to fix it).

Thanks for your help, again :D

jamie

  • Hero Member
  • *****
  • Posts: 7423
Re: If, Then, Else Statements?
« Reply #1 on: December 27, 2017, 04:54:14 pm »
That should of gave you a compiler error..

 You didn't place the final ";" at the end of the statement..
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit2Change(Sender: TObject);
  2.  
  3.  
  4. 2.begin
  5.  
  6.  
  7. 3.  If Edit2.Text = ''
  8.  
  9.  
  10. 4.  Then Button1.Enabled := False
  11.  
  12.  
  13. 5.  Else Button1.Enabled := True    // Place a ";" here..
  14.  
  15.  
  16. 6.    End;
  17.  

Not sure if that is your real problem, the statement looks logical..
Try this instead.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit2Change(Sender: TObject);
  2.  
  3.  
  4. 2.begin
  5.  
  6. 3.  Button1.Enabled := Not  Edit2.Text = '' ;
  7.  
  8. 6.    End;
  9.  
The only true wisdom is knowing you know nothing

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: If, Then, Else Statements?
« Reply #2 on: December 27, 2017, 04:59:30 pm »
Missing ( )
Code: Pascal  [Select][+][-]
  1.   Button1.Enabled := Not  (Edit2.Text = '') ;
  2.  
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

madref

  • Hero Member
  • *****
  • Posts: 1114
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: If, Then, Else Statements?
« Reply #3 on: December 27, 2017, 05:06:10 pm »
Shouldn't it be an onChange-event?
Code: [Select]
procedure TForm1.Edit2OnChange(Sender: Object);
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Main Platform:
--------------
Mac OS X Sequoaia 15.6.1
Lazarus 4.99 (rev main_4_99-2644-gfd63613782) FPC 3.3.1 x86_64-darwin-cocoa

Windows 10 Pro
Lazarus 3.99 (rev cbfd80ce39)

wp

  • Hero Member
  • *****
  • Posts: 13275
Re: If, Then, Else Statements?
« Reply #4 on: December 27, 2017, 05:18:03 pm »
Did you assign the code to the event property OnChange, i.e. click on the '...' buttons in the object inspector next to "OnChange"?

A

  • Guest
Re: If, Then, Else Statements?
« Reply #5 on: December 27, 2017, 05:22:28 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit2Change(Sender: TObject);
  2. begin
  3.   Button1.Enabled := Not  (Edit2.Text = '') ;
  4.     End;                  

This doesn't throw up any errors, but it doesn't work either. I've tried this code with the button enabled and disabled in the designer, however, it continues to not work.

I'm interested in why the code that I wrote didn't work. The semi-colon at the end of the Else statement makes no difference either. As jamie says, it looks logical.

wp - The OnChange entry in the object inspector for the Edit2 is "Edit2Change" - that's right isn't it?
Thanks for everyone's input.

Handoko

  • Hero Member
  • *****
  • Posts: 5508
  • My goal: build my own game engine using Lazarus
Re: If, Then, Else Statements?
« Reply #6 on: December 27, 2017, 05:25:05 pm »
Hello A,

Your code works perfectly on my test. I copied/pasted yours and run it, it worked as what we think. The problem must be on the other thing that you didn't show us.

Maybe you didn't set the event correctly. You can try:
01. Select the Edit2
02. On the Object Inspector > click "Events" tab
03. Select OnChange
04. Click the 3 dots on the right of the OnChange

jamie

  • Hero Member
  • *****
  • Posts: 7423
Re: If, Then, Else Statements?
« Reply #7 on: December 27, 2017, 05:28:11 pm »
During an EDIT.OnChange, not all Widgets will repaint the rest of the surface.

Maybe this is needed after setting the Enabled property

 Button1.Repaint;
The only true wisdom is knowing you know nothing

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: If, Then, Else Statements?
« Reply #8 on: December 27, 2017, 05:30:29 pm »
the code is correct. I have two comments.
1) your code will run only at run time it will never be executed when you change things from the object inspector
2) add a break point inside your method at the first line or anywhere to make sure that the code is executed. IF the break point is reached and execution terminates then you know the code executes if not you do not have the correct event linked.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

A

  • Guest
Re: If, Then, Else Statements?
« Reply #9 on: December 27, 2017, 05:30:56 pm »
That's embarassing...

I got muddled with which button was which. I was supposed to be referring to button2... *sigh...*

I'll get into the habit of naming my form components so that I don't make the same mistake again. Silly me. :P

Lol, thanks for everyone's help anyways! I at least learnt another way to do it: // Button1.Enabled := Not  (Edit2.Text = '') ;

 :D :D :D :D :D :D

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: If, Then, Else Statements?
« Reply #10 on: December 27, 2017, 05:32:27 pm »
During an EDIT.OnChange, not all Widgets will repaint the rest of the surface.

Maybe this is needed after setting the Enabled property

 Button1.Repaint;
no, setting a controls property should invalidate the control forcing a repaint on the next iteration, you simple have to exit the handler and let the application loop do its job.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4653
  • I like bugs.
Re: If, Then, Else Statements?
« Reply #11 on: December 27, 2017, 05:36:43 pm »
@jamie, all your advice was wrong. If you don't know the answer, it is better to not write anything.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

wp

  • Hero Member
  • *****
  • Posts: 13275
Re: If, Then, Else Statements?
« Reply #12 on: December 27, 2017, 05:43:25 pm »
I'll get into the habit of naming my form components so that I don't make the same mistake again.
An absolutely wise decision! Properly named variables are the key to good programming.

Button1.Enabled := Not  (Edit2.Text = '') ;
Or simpler (in my eyes)
Code: Pascal  [Select][+][-]
  1. Button1.Enabled := (Edit2.Text <> '')

A

  • Guest
Re: If, Then, Else Statements?
« Reply #13 on: December 27, 2017, 05:48:08 pm »
Thanks wp, good to know of other ways to achieve this.

I've named everything:

Code: Pascal  [Select][+][-]
  1. If NameEdit.Text = ''
  2.   Then OKButton.Enabled := False
  3.   Else OKButton.Enabled := True;
  4.     End;

It's much easier to understand now. Are there any naming conventions that I should use, or is the above fine? I can't find any naming guidelines online (e.g. should I use "btn" over "button" or...?)

Thanks.

Bart

  • Hero Member
  • *****
  • Posts: 5649
    • Bart en Mariska's Webstek
Re: If, Then, Else Statements?
« Reply #14 on: December 27, 2017, 06:04:14 pm »
Your naming convetion seems just OK to me.
Your indentation style is a bit odd IMHO though.

Bart

 

TinyPortal © 2005-2018