LAZARUS/ DELPHI COURSE - - / - - / - - Other material for programmers

Lazarus and Delphi Course: First lesson focussing on strings

Page URL: Str1.htm

This page is information rich, and a has search button at the bottom of the page.

Please don't dismiss it because it isn't full of graphics, scripts, cookies, etc!

You will probably find the text easier to read if you make your browser window much narrower than usual. You may also want to change your browser's zoom level, to enlarge the text. Opera (at least) lets you change zoom level easily. The text will adapt nicely to the settings you decide give the best results for your needs!

The lines of sample Lazarus/ Delphi code in these pages will not "wrap". I.e., if a line is too long to show in the width you have set your browser too, parts of the line will be "off the page". Those lines will still copy/paste properly, at least in Opera. Please feel free to send feedback on the choices I've made! (Will you forgive me for not forcing upon you a column of links on the left and a column of ads on the right?)

This is just one exercise in a series of Lazarus / Delphi exercises. You will probably be best served by doing them in sequence... each assumes some prior knowledge. Material © TK Boyd, sheepdogsoftware.co.uk, 4/05-6/20.

You may find typos and rough edges in this. None-the-less, the basic information should be accurate. If something seems wrong, or if you find I've assumed knowledge without explaining it in a previous lesson, please let me know. Please forgive matters of typos, etc. for now. I am not inherently sloppy! The blemishes will be dealt with later.

In this lesson,.

Lazarus/ Delphi:
--- Properties Rev (u/d (c) message too. Search on "terial") 9 Jun 20. isited
--- Heirarchies Rev (u/d (c) message too. Search on "terial") 9 Jun 20. isited

Lazarus/ Delphi components:
--- The Panel container
--- ComboBoxes
--- Labels Rev (u/d (c) message too. Search on "terial") 9 Jun 20. isited
--- EditBoxes Rev (u/d (c) message too. Search on "terial") 9 Jun 20. isited
--- Buttons Rev (u/d (c) message too. Search on "terial") 9 Jun 20. isited

Lazarus/ Delphi data type:
--- String

This lesson will be pretty easy, if you have been reading through the lessons in the order that they are presented in the table of contents for the course, which I hope you are, because I have worked hard over the sequence in which the material is presented. This lesson is mostly to lay the foundations for further lessons in the "strings" series. It does take you a little further with containers, and shows you the ComboBox, an additional way to get input from users.

When I travel, I frequently obtain insurance from Travel Guard. They send me a sheet of sticky labels in various sizes with, effectively, my name and address on them. The idea is that I put labels on my bags (of course), but also on my camera, in my wallet, etc. To protect me, and to make the label more effective, instead of showing my home address, the label has a reference number. If someone honest finds my camera, say, in a restaurant in Rome, they call the TravelGuard telephone number (on the label), tell them the reference number on the label, TravelGuard knows what hotel I'm at, pass on the location of my camera, and I turn up to collect it. Have you never thought it a little reckless to put your home address on your bags while travelling? You are announcing "Here is a house which will be empty." On the other, less suspicious, hand: a taxi driver once returned the wallet I'd left in his cab to me at my hotel in Barcelona, complete with credit cards and cash. there are good people out there.

Anyway... in this lesson, you will set up a simple application in which the user types, once, a name, phone number, and travel details, and the computer will generate from that input a set of various labels- large ones for bags, tiny ones to stick on the back of a camera, etc.

Start a new application. Rename the form str1f1. Save the unit as str1.htmu1. Save the project as str1.htm.

Before we're through, our application will produce a screen looking a little like....
Name: Fred Brown
Staying at: Waldorf Phone: 860-555-5555
Flights: 1 Apr 05 USAir 582 / 5 Apr 05 USAir 976
Fred Brown. Staying at Waldorf
Phone: 860-555-5555
Flights: 1 Apr 05 USAir 582 / 5 Apr 05 USAir 976

Fred Brown. Staying at Waldorf
Phone: 860-555-5555
Flights: 1 Apr 05 USAir 582 / 5 Apr 05 USAir 976

Fred Brown. Staying at Waldorf
Phone: 860-555-5555
Flights: 1 Apr 05 USAir 582 / 5 Apr 05 USAir 976

The user will fill in edit boxes in the area above the "===". The rest will be generated by the computer. The text will be in different sizes. Before we go any further, I should mention that this will be a theoretical exercise. It will "work", but we won't add the extra bits which would be needed to print out the labels in ink on paper. Maybe that should go in a future lesson?

Put an edit box and two labels on the form. Rename the edit box eName. Don't bother renaming the labels. Make the edit box's OnChange handler...
procedure Tstr1f1.eNameChange(Sender: TObject);
... and run the program. Yes... I know... not very impressive... yet. I do try to build things in small steps for you! And when you are building something of your own end, you should also build it up a bit at a time.

Just before we move on, let me talk about "at design time" and "at run time". All sorts of things happen at one or either or both of them. Some things can only happen at one or the other. Some usually, or more easily, happen at one or the other.

In the application we've put together so far, Delphi has provided "eName" for the edit box's test property. If this application is going to be used mainly by Mr Fred Smith, then it would make sense to change the caption to 'Mr Fred Smith', using the Object Inspector. When the application starts, the edit box will start with Mr Fred Smith in it. (You will also have to put 'Mr Fred Smith' in the labels.... until you learn a way to be more clever.) These settings are being made "at design time", i.e. before the application is run. If the application will be used by many people, and not by one of them usually, it would make sense to set the captions to nothing at design time, so that users do not have to delete anything when they change the captions while the application is running.

Moving on....

Delete the two labels from the form, and the two lines of code from the edit box's OnChange handler. When you re-save the form, the skelton of the OnChange handler will disappear, along with the other bits that Delphi looks after for you. Put a second edit box on the form. Name it ePhoneAway.

Put two labels on the form. Name the first laTxtName, the second laTxtPhoneAway. Make the captions "Name:" and "Phone while travelling:" The captions of these labels will remain unchanged; they are just to label the edit boxes. I tend to use laTxt... as the name for such labels.

Resize and move the edit boxes and labels around to achieve a sensible layout. So far, so boring. Now we turn to something a little new...

You met your first container back in the second lesson about built around converter applications. It was the RadioGroup component.

Panels are another useful container. Place a panel on your form. Name it pa1. Make pa1.caption nothing. Make the panel about 2 inches high, four inches wide. (You can make it smaller again later, once we've put various things on it.)

Put two labels on the panel; arrange them in two rows. Name them laPa1Name and laPa1Phone.

A word: If you place a label (or other component) on a container, that container becomes the parent of that component. The location has more implications than simple layout. If you place a label on the form which is the panel's parent and container, you can't (easily) move it onto the form. You can drag it to the same part of the screen as the paenl, but it will be under or over the panel, not "on", i.e. contained by, it. (The best thing to do in such cases is to delete the component you tried to put on the panel and try again.

Click somewhere on the panel, but not on either of its labels. That should select the panel (and its contents). You should see eight small black squares, at the corners and in the middle of the edges of the panel.

Do ctrl-C (hold down the ctrl key, keep it down, press and release the C key, THEN release the ctrl key). This will copy the panel (and its contents) to the Windows clipboard.

Click somewhere on the form, not on the panel, not on any of the other components. (The display at the top of the Object Inspector should say "str1f1: Tstr1f1")

Do ctrl-V. This will paste a copy of the panel onto the form.

N.B.: It will look like there are now two laPa1Name labels, two laPa1Phone labels in yor project now. There aren't. You'll have to use the Object Inspector to change.........
the panel's name to pa2
the panel's caption to nothing
the first label's name to laPa2Name
the second label's name to laPa2Phone

It all sounds oh so tedious, but if this was your own project, you'd zip though this setting up of the user's interface without hardly noticing what you were doing.

Now make the form's eName OnChange handler be...
procedure Tstr1f1.eNameChange(Sender: TObject);
Save project. Run application. Still boring? Well, yes, perhaps. Hang in there.

Make the form's ePhoneAway OnChange handler be the following. With a little creative use of copy/paste, you can do it with minimal typing.
procedure Tstr1f1.ePhoneAwayChange(Sender: TObject);
Save project. Run application. How if you change either the name, or the phone, two copies of your details also change.

...making them...
Likewise, change....
...making them...
Still pretty boring. the quick route to the second panel wasn't too bad. I'm assuming you see how you would add another edit box and labels on the panels to implement the "Flights" data line that was mentioned in the application's plan. If you are in any doubt, make sure that you can.

Click on the first panel, not on eithere of the components on it. Press F11 to go over to the Object Inspector. You will see a property called "+Font". The "+" tells us that this entry in the properties list is merely a heading for a number of related properties. Double click on the +Font, and it will change to -Font, and beneath it will be Color, Height, Name, and some others. Change the font size to 14, and the name to the name of some nice font.

Save and run your application. Now that's more like it! With one little change, we took care of a bunch of stuff on the first label. you could customise the second label in the same way.

You can even use the panel's font property to control the appearance of some of the text on it, but not all of the text. Click on laPa1Phone, and click on it's font entry in the Object Inspector. Change the font size to 10. Notice that Delphi also changes the ParentFont property to false. When true, the component's font is determined from the parent's font.

The things you can d owth Delphi! My!

You could (I trust... try it, if you are in any doubt) add further panels to the form, so that more than two copies of your travel labels would be created. Maybe I should add that I regularly travel between two places, and routinely use just such a program to prepare labels for my suitcases.

I said this was a lesson about strings. The data we put into the edit box is string type data. We "added" (concatenate is the proper term) "Name:" and "Phone:" to the data entered by the user. We did things to the way the strings were displayed.

The next strings lesson will focus more on what you can do with strings, and spend less time on edit boxes, labels, and the like.

Just before we go, we've got a good chance to introduce the use of ComboBoxes. Imagine this label printing application was being written for the busy secretary of a small firm which frequently sent one of three executives on trips. (Okay, the example is artificial. Forgive me?) The executives are Mr. JP Morgan, Ms. Chase, and Mr Wells Fargo. ComboBoxes can be a big help in guiding users towards entering sensible data. If they have to typo "Ms Chase", it may come out "Mrs Chose", "sM hCase", etc. Make it easy for your users to get things right!

Add a ComboBox to the form. Rename it cbNameSelect. Change the Items property to Mr. JP Morgan, Ms. Chase, and Mr Wells Fargo, each of them on their own line. (If you double click the cell to the left of "Items" a little text editor will open. Just type the names into that. Click the "Okay" button to close the editor; your list will be saved.

Change cbNameSelect.text to Mr. JP Morgan. Change laPa1Name.caption to 'Name: Mr. JP Morgan', and laPa2Name.caption to 'Name: Mr JP Morgan'

Create the following OnChange handler for the ComboBox:
procedure Tstr1f1.cbNameSelectChange(Sender: TObject);
Save project. Run application. You should now be able to alter the name in the labels using the ComboBox. It would probably be best to remove the old eName edit box, and it's OnChange handler. If you want to make labels for good old Fred, you can always type his name into the ComboBox when you need his labels.... well, you can as things stand. If you change the style of the ComboBox to csDropDownList, then you cannot type things in by hand, you must choose (only) from the names provided in the drop down list. this is a nice example of the way that little changes in a components many properties can mean that "one" component has many flavors.

Search just this site without using forms,
Or... again to search just this site, use...

Powered by FreeFind

Site search Web search

The search engine merely looks for the words you type, so....
  *!  Spell them properly   !*
  Don't bother with "How do I get rich?" That will merely return pages with "how", "do", "I", "get" and "rich".

I have other sites...
   SheepdogSoftware site.
   My site at Arunet.

Ad from page's editor: Yes.. I do enjoy compiling these things for you... I hope they are helpful. However.. they don't pay my bills!!! If you find this stuff useful, (and you run an MS-DOS or Windows PC) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated.
Click here to visit editor's Sheepdog Software freeware, shareware page.
Link to Tutorials main page Link to Lazarus/ Delphi Course index

To email this page's editor, Tom Boyd.... Editor's email address. Suggestions welcomed! Please cite "str1.htm".

Click for W3.org HTML validity test Page has been tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org. Mostly passes.

AND passes... Click to check CSS validity

One final suggestion: Be sure you know all you need to about spyware.

. . . . . P a g e . . . E n d s . . . . .