This page is "browser friendly". Make your browser window as wide as you want it. The text will flow nicely for you. It is easier to read in a narrow window. With most browsers, pressing plus, minus or zero while the control key (ctrl) is held down will change the texts size. (Enlarge, reduce, restore to default, respectively.) (This is more fully explained, and there's another tip, at my Power Browsing page.)
If you have valiantly slogged through the first tutorial in my Lazarus programming series, you deserve a pat on the back, and a break with a bit more fun in it.
That tutorial set the scene regarding some of the working practices I recommend, and, for people with the determination, lets them get started, even if they have never programmed in any language. But it wasn't much fun!
I will explain less in this tutorial. Go with the flow, and see if the ideas don't seep into you by osmosis.
This tutorial won't result in a clever application, but it will result in something which, in a future tutorial, will do something clever.
(That "future tutorial" isn't written yet, but the code that it will explain is. You can download a .zip with the code and a compiled .exe file in it. It is an application which allows users to enter temperatures in Celsius or Fahrenheit, and as soon as they do, they see the temperature in the other units.)
All of what is explained here should, by the way, work the same way for any Delphi programmers reading this tutorial.
Here's what the finished application will look like, after a user has done some typing in one or the other or both of the edit boxes on the form. Eventually, whatever you type in one will be copied to the other... but all letters in one will be made uppercase, whatever you type, and all letters in the other will be made lowercase.
Start a new application; put a "Quit" button on it. Put everything in a folder called LDN003, call the form LDN003f1, call the application LDN003, call the code ldn003u1.
("LDN003": Lazarus Demo (New), number 3. ("LD003" would be a Lazarus demo comparable to my Delphi Demo DD003. (Sadly, I forgot my plan until it was too late, and LD001 and LD002 are not comparable to DD001 and 002.)) Aren't you glad you asked?)
Put two edit boxes on the form, side by side. Name them eUpper and eLower... for "uppercase" and "lowercase". "Upper"/ "lower" won't have anything to do with where the edit boxes are on the form.
Double-click somewhere on the form to cause Lazarus to give you a shell "FormCreate" handler, and between the begin and the end, put...
... and run the application.
After fixing the inevitable typos, you should have a form that has nothing in the eUpper edit box, and "eLower" in the other. And a "Quit" button that works. You should be able to enter text into the edit boxes, but nothing exciting happens when you do that.
Double-click on the eLower edit box to cause Lazarus to give you a shell event handler, and between the begin and the end, put...
... and run the application.
I hope you didn't notice that when you double-clicked on the edit box, instead of getting a shell for the FormCreate event, you got a shell for the eLowerChange event? Apologies if you did, and worried.
Notice it now!
When you double-click on an object (the form, a button, an edit box), Lazarus creates a shell for some "event"... but which event it creates the shell for depends on the sort of object you double-clicked on. Happily, it is pretty good at choosing the event you would have chosen, and you can override the default when necessary, which won't be for a while, if you are a novice GUI programmer.
So. We've "followed the cookbook". What happens? If you've followed the cookbook correctly when you run the application, it looks just like it did before... at first.
If you start by changing things in the eUpper edit box, it still behaves as it did before.
But change the contents of the eLower edit box, and, Ta Da!... the eUpper edit box "echoes" what you type in the eLower edit box.
Not very "useful"... but pretty magic! Bear with me... we are moving towards something useful. Eventually, when you type a temperature in Celsius into one of the text boxes, you will see the Fahrenheit equivalent in the other, and if you change what's in the Fahrenheit box, the Celsius number will change. Before a GUI interface with and event driven operating system... which is what you are using... creating an application like this would have been a nightmare. (You are using a GUI interface with and event driven operating system regardless of which Lazarus environment you are in, Linux, Windows or Mac.)
Now double-click on the eUpper edit box to cause Lazarus to give you a shell event handler, and between the begin and the end, put...
... and run the application.
There's an immediate difference. Both edit boxes are empty by the time your eye can register what is in them.
The program started, at least internally, with nothing in eUpper because of the eUpper.text:='' in the FormCreate handler. But as soon as what was in eUpper changed, the eUpperChange event handler was executed, resulting in eLower.text:=eUpper.text, so eLower became empty, too. All in the blink of an eye.
Type something in either edit box.
Now whatever you type in one echoes in the other, regardless of which edit box you type in.
We're going to go further with this application in a future tutorial, but just before we end this one, let's make it a little more interesting!
Lazarus (and Delphi) has two functions which we are going to use to make this application do something which I hope you will find entertaining. They are...
If temporarily, you put the following into your application's FormCreate handler....
... then when the application starts, a message box will appear on the screen with "123ABCDEF" in it. Notice that the "abc" in your code has become "ABC"
Change the temporary line to...
... and then when the application starts, the message box will show "123abcdef" in it. Notice that the "DEF" has become "def".
A function is said to return a value. The uppercase and lowercase functions return strings.. which is just as well, because the showmessage() procedure must be given a string.
If you promise to go back, read through that, and promise to think while we do the next bit, I will refrain from more "explaining"... for the moment.
Where previously, you had....
procedure TLDN003f1.eLowerChange(Sender: TObject); begin eUpper.text:=eLower.text; end; procedure TLDN003f1.eUpperChange(Sender: TObject); begin eLower.text:=eUpper.text; end;
... add uppercase and lowercase and some brackets, to make...
procedure TLDN003f1.eLowerChange(Sender: TObject); begin eUpper.text:=uppercase(eLower.text); end; procedure TLDN003f1.eUpperChange(Sender: TObject); begin eLower.text:=lowercase(eUpper.text); end;
... and play with the application again.
Apart from an annoying "thing" (which could be cured) which makes the insertion point jump around within the edit box you are typing in (sometimes), the application should now maintain two copies of whatever you type. One in uppercase; the other in lowercase.
That's (almost) it for today!
In Delphi help files, anyway... I'm not familiar with Lazarus documentation yet... you will see people say things like "In the OnFormCreate event handler", and you will see "In the FormCreate event handler".
To be honest: I can't quite get my head around when it is exactly right to say "OnFormCreate" and when it is right to say "FormCreate". The Object Inspector's "Events" tab refers to the events available as OnActivate, OnClick, OnCreate, etc. But in the code, we see the same things without the "On".
Happily, for my purposes these many years, knowing when I need the "On", and when I don't hasn't been a problem. I apologize for not giving you these tutorials with absolute technical perfection, but if you can live with my sloppiness, you are welcome to return. I'm now going to write another tutorial, based on this one, but it will go further and create the Celsius/Fahrenheit convertor. Other people can go on worrying about the "on", if they wish. (But I do get exercised about correct use of apostrophes. Be warned. I'm not a complete slob.)
If you visit 1&1's site from here, it helps me. They host my website, and I wouldn't put this link up for them if I wasn't happy with their service. They offer things for the beginner and the corporation.
Page tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org
....... P a g e . . . E n d s .....