HOME - - - / - - / - - / TUTORIALS INDEX - - - / - - / - - / - - / - - - Other material for programmers

Delphi or Lazarus: Getting Input From Your Users Using RadioButtons

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!

Click here if you want to know more about the source and format of these pages.

PLEASE NOTE: This is a late draft of a tutorial which I hope you'll like, and find useful. However, it has NOT has the "final polish" which it will eventually get, so don't moan about the little loose ends which remain for the moment, please! It IS essentially complete.




This is the less advanced of two tutorials on the subject of getting input from users.

The more advanced tutorial uses a user- created dialog box.


I sit down to write this tutorial on 17 December, 2004. It will be my 50th tutorial on Delphi for you. In the collected tutorials, you will find wide variation in quality and approach. In this tutorial, I am attempting to pull together the experience of writing those other tutorials. I may well repeat things that appear scattered through the others. This is a "Level 2" tutorial, so I hope you will understand the presence of more "hand holding" that would be in tutorials for the more advanced levels.

On 20 Sep 2011, I went through this and added hasty notes so that it is "right" for people using Lazarus, too. Where no indication is made, whatever has been said applies to either RAD environment.

The tutorial will show you how to set up a some RadioButtons in a RadioGroup to get input from users. In the example developed, users choose one of three colors.


When people turn to computers to get help with something, they can rarely just start the appropriate program running and leave it at that. Usually, users need to give the program instructions, indicate their needs, make choices, etc. For example: I frequently send mail from the UK to the US. I have a little program that tells me how much postage is needed. Even that simple little program needs to know something: How heavy is the parcel I'm sending?

Windows provides programmers and users with almost too many ways to deal with the problem of letting the program know the users needs, the problem of obtaining input. (If only things were as easy as they are made to seem in the film Short Circuit!) (And if only there were many Stephanies decorating the planet. But I digress!)

Novice programmers need to attack the problem from both ends: They need to think about programs that have been written by others, and think about the ways input is collected by those programs.

The other "end" novice programmers can attack from is their current stock of skills. They must be patient! They will (eventually) be able to "run", but even while they are still "walking", most things can be done, even if a little crudeness sometimes creeps into a program until necessary skills are learned or discovered.

This tutorial will show you a really simple way to get input which will suit SOME needs. It will also take you gently by the hand, and try to tell you almost everything you need to know. It won't tell you everything about Delphi, but it will try to tell you everything you need to know to create the program the tutorial is about.

An important rule for programmers: Keep a very clear idea in your head of exactly what users of your program should encounter. It is easy to get distracted by the minutiae of the program's internals, and then start writing a program which isn't the one you had in mind when you started, and before you know it, parts of your program don't "fit together".

Having said that, I should also admit that sometimes you need to re-consider your goals as the project progresses. This is fine... just be sure that you do think carefully about the overall picture, from the user's perspective, any time you decide you need to re-define your goals.

The sort of input which can be obtained by the techniques explained in this tutorial is limited to things like....

yes / no
north / east / south /west
red / green / blue
... in other words, the user will be selecting one alternative from a limited list. The programmer will define what the choices are, and the user will not be able to extend that list. In the colors example, yes, there are other colors... but only the colors put in the list by the programmer will be available to the user.

Well! (Almost) enough theory... let's move on to creating a program.


This tutorial assumes you have Delphi (or Lazarus) set up on your computer. I am using Delphi 2, on a Win98 machine. (Lazarus 0.9.30 on a Windows XP SP3 machine.) I am assuming that you have a working Delphi or Lazarus installation, but I'm not assuming any extra features.

Some of the things I tell you to do below are simply the only way you can achieve the result. Other things are more open to choice. Sometimes I'll discuss options, other times I'll just give you one of perhaps several things which work.

You don't need to be online for any of what follows. You probably accessed this tutorial via the internet (http://sheepdogguides.com/tut.htm, or search with Google for "sheepdog delphi tutorial"). Just use your browser's File | Save, and you can access the document while offline.

I said I'd cover basics: You can have Delphi AND the internet browser of your choice (I liked Opera when I started this tutorial. At 9/11, I'd been using Firefox for years) running at the same time. I find it easiest to work with multiple programs by having everything in the "restored" state, i.e. not minimized, not maximized. When they are like this, you can have one app, say Delphi, with it's upper left corner in the screen's upper left, and the other app, say your internet browser, with this tutorial in it, with it's lower right corner in the screen's lower right. Neither should be stretched to fill the screen, of course. Like this, whichever app is on top, there's still a bit of the other peeking out. Click there, and you can see the other.

Still on the subject of multi-tasking: Of course, there's also the option of using the buttons on the task bar to switch between active applications. If you knew all this, thank you for your patience. IF YOU DIDN'T: Please make an effort to try working with Delphi and your browser "at the same time". Once you get the hang of it, it is very useful. At the moment, I am generating the text of this page in a text editor called Textpad, saving my work to disc from time to time, and then looking at what it looks like in my browser before going back to working on the text.


So! You're sitting there, computer ready, Delphi (Or Lazarus... which I will NOT continue to add, except when I need to say "But in Lazarus...") ready.

There may be things left over from earlier Delphi jobs. Just do...

File | Close All (Delphi)
File | Close All Editor Files (Lazarus)

.... If nothing was open, no harm done.

For Delphi, do File | New Application. For Lazarus, do File | New..., and from the dialog select "Application", which is in the "Project" section. A form should appear on the screen. (Form (in this context): A rectangle covered in a grid of dots, with a Windows bar across the top with the Delphi logo and "Form1" at the left, and three small square buttons at the right, the usual ones: One for "minimize", one which will be for "maximize" at the moment, but will change to the one for "restore" if clicked, and one for "close". (Don't click the one for "close" at the moment... (If you do, just press F12 to get it back.)


Here beginnneth a lengthy section on "Working With Delphi (or Lazarus) To Create An Application". Even if you're sure you know the basics, please work through this section, as a few things will need to be in place for later parts of this tutorial. However, if you are in a big hurry, know quite a bit already, you can click here.

Even if you can't see all of them at this moment, there are (at least!) four windows which are typically on a Delphi programmer's screen, and we will tour them now.

Typically long and not tall, and across the top of the screen is the main Delphi interface. Below the title bar of that window is the Delphi menu....
File | Edit | Search | View.... etc

Below the menu there are some buttons on the left, and a tabbed control on the right.

Here's where half of you will love me, half will hate me. For reasons that half of us understand, I don't like putting graphics in web pages. This does entail a cost, though, which we're about to endure.

"The buttons on the left", on my machine, are in two rows, and consist of three groups of four buttons. I use only two of them regularly, and we'll come back to what they are, and what they do, in a minute. All of the buttons are just a quick way to accomplish things which you could also do via the menu.

Turning to "the tabbed control on the left": I'm speaking of the "component palette". The tabs on my machine for Delphi are "Standard", "Additional", "Win 95", "Data Access", etc. (For Lazarus are "Standard", "Additional", "Common Controls", "Dialogs", etc.) On each tab, there are a number of buttons with icons. ("BitBtn"s, in Delphi-speak... which in many cases derives from MS-speak. Don't forget as you work through this, that Delphi has an extensive "Help" file, and if you know the right term, e.g. BitBtn, you can often learn useful things from the help file.) At the left, on every tab, there's an arrow icon that looks like a standard mouse pointer. We'll come back to this in a moment, calling it "the component palette's pointer button".If you have a Delphi window active, hover the mouse pointer over any of the other buttons of the component palette. A tool tip will pop up to tell you what that icon stands for. So, for example, on my Delphi component palette's "Standard" tab, the buttons are for: Main Menu, Pop Up Menu, Label (icon is letter "A"), "Edit"(box), Memo, Button (Icon is button with "OK") (I keep saying "on my..." because menus, component palettes, button sets, etc can be customized, and they change between Delphi versions. I hope that you will find your environment close enough to mine that you can figure things out.) The Lazarus palette is similar.

Click (i.e., press and release the left hand mouse button while the mouse pointer is over...) the Label (or "TLabel") icon of the component palette. The "button" of the Label icon should "go" down, and "stay" down. Move the pointer to the middle of Form1's window, and click again.

You should see two things: First, a label saying "Label1" should appear on the form. (For the moment the selection marks around it will make it a little hard to read.) Second, and less obvious, the button on the component palette for labels has "popped up", and the pointer icon is re-selected. If you want to place another label on the form, you have to click on the Label icon in the palette again first. (Yes- Delphi and Lazarus behave the same in this respect.)

Reading about it is tedious, but you'll soon get the hang of using it; it is pretty intuitive.

Now click on the "Button" (or "TButton") icon of the component palette, and then click somewhere on Form1. I.e., click somewhere in the window which is displaying Form1: the rectangle of dots with "Form1" at the top. This is where we can see what your application is going to look like. As your programming skills grow, you will find that an application may have more than one window, and how any of them appear can be changed as needed... but for now, things are as simple as I hope they seem.

Try moving the label or the button. Try changing the button's size and shape. (Don't bother with the label's size and shape, for now.) All done "the usual Windows way", and if you have no luck, it doesn't matter!

Double click on the button you put on the form. Things will happen. You should see....
procedure TForm1.Button1Click(Sender: TObject);
begin

end;

end.
You are seeing it in the third of our four important Delphi / Lazarus programming windows: The Code Editor. (The first two were the window with the form, the one you put the button on, and the main Delphi interface, the one with the menu, buttons, and component palette.)

Programming in Delphi is a little weird, depending on your background. Delphi itself types a lot of the "stuff" needed in your program. What we're doing with the button is an example. We're about to tell the program what we want to happen when the button is clicked when the program is running. All that "procedure TForm1.Button1Click(Sender..." stuff is standard and necessary, and as such, we don't have to hassle with it... hurrah!

The cursor will already be in the right place. Type....
application.terminate;
... on the line just after the "begin" that Delphi put in for you. In "application.terminate;", there are no spaces, and there is one dot, one semicolon. (Don't be alarmed when Lazarus makes the punctuation marks red.)

Now use the main Delphi interface to invoke Run | Run.

(You can use F9 in the future, if you happen to remember it. Look at what you see after clicking the first "Run". See the note that tells you that F9 gives you "Run | Run"?

Your "program" will be compiled, and, absent any typos, you will see the same form as before... minus the dots, to show you the program is running, not being designed. Click on the button. In Lazarus, you will be asked if it is okay that the execution stopped. Click "OK". In both, the program will stop running, and take you back to being in Delphi (or Lazarus), all set to make further changes to the program. (Move the Source Editor window (that's what Lazarus calls the window with "application.terminate", etc, in it) around, if need be, so that you can see the form, as well as the Source Editor.

Congratulations! You did it! Take a break!


Right: Back to work.

In every realm of computing, "Save your work, often" is an important mantra. I haven't inflicted saving on you before this, because, despite all the reading you've had to do, you wouldn't have to do much to get back to where we are now even if you had to re-start from scratch a few times.

Saving, itself, isn't very hard... but good use of your hard disk means that some things need to be considered.

First, I'm going to suggest that you use a separate folder (what we once called "directories") for each Delphi project you embark upon. A good place for those folders on my machine would be a folder called "Delphi Projects- TKB" Change the initials (TKB) to your initials for your folder. If you just call it "My Delphi Projects", it is easy to become unclear as to who created the folder.... you? Borland? Microsoft? (And you also end up with an awful lot of entries in the "My..." part of the list!)

I'd recommend putting the Delphi (or Lazarus) Projects folder in the My Documents folder.

One more thing, before we save your work. I'll write about Delphi first, Lazarus in a moment...

A Delphi project consists of at least three files. There will be one with each of the following extensions: .dpr, .pas, .dfm. (These are discussed in more detail in my tutorial "The Whole Picture".) In addition, eventually, there will be a file with the .exe extension. This, and only this, is what you send on to customers. It is all they need to use your program. Without the other files, which are only useful to Delphi programmers, they can't (easily) change things within the program, e.g. your copyright message.

A Lazarus project consists of at least four files. There will be one with each of the following extensions: .lpi, .lpr, .pas, .lfm. (The first is unique to Lazarus. The others are the equivalents of the Delphi .dpr, .pas, and .dfm discussed in more detail in my tutorial "The Whole Picture".) In addition, eventually, there will be a file with the .exe extension. This, and only this, is what you send on to customers. It is all they need to use your program. Without the other files, which are only useful to Delphi programmers, they can't (easily) change things within the program, e.g. your copyright message. Lazarus will also create a "backup" and a "lib" folder within your project's folder.

While what I'm about to describe isn't the only way to do things, do them this way for the moment. You can re-write the rules later, when you have a better idea of the consequences.

Doing the first save in DELPHI

Use the menu to invoke "File | Save All". (Not "Save project as..", not the simple "File | Save", or any of the other things. Use "File | Save All")

You'll get a save dialog with "Save Unit1 As" in the title bar.

If you haven't already set up a "Delphi Projects-(your initials)" folder in your MyDocuments, use the tools to navigate to "My Documents"

Use the "New folder" button to create a new folder (!), call it, "Delphi Projects-(your initials)"... or just navigate to it, if it already existed.

Double click on the folder's icon. That should move you into the folder. Don't be alarmed that it is empty... it should be at this stage.

Click the "New folder" button again, and create a folder called DD47, to stand for "Delphi Demo 47", which is my name for the program we are creating during this tutorial. Use "DD47", not a name of your choosing (this time!), because parts of the name get into things we're going to do, and you will be saved typing and confusion if you use DD47

Double click on the DD47 folder icon to move into the project's folder. It will also still be empty at this point.

The "Save Unit1 As" dialog box should now have "DD47" in the "Save In:" edit box. We've got the "where" of the saving sorted out. Now we have the "what" to do...

In the edit box for File Name, change the proposed "Unit1.pas" to "DD47u1.pas", click the "Save" button.

As soon as the save is done, you'll get another Save dialog, but this time it is to "Save Project1 As...". You will (or should already be!) in the right place, i.e. "My Documents\Delphi Projects-(your initials)\DD47. You need to change the name for the file to DD47.dpr. One consequence of this choice is that the default name for the .exe file will be DD47. (You can rename it, it will still run, but you might as well have it start with a good name.

Doing the first save in LAZARUS

Use the menu to invoke "File | Save All". (Not "Save project as..", not the simple "File | Save", or any of the other things. Use "File | Save All")

You'll get a save dialog with "Save project project1" in the title bar.

If you haven't already set up a "Delphi Projects-(your initials)" folder in your MyDocuments, use the tools to navigate to "My Documents"

Use the "New folder" button to create a new folder (!), call it, "Lazarus Projects-(your initials)"... or just navigate to it, if it already existed.

Double click on the folder's icon. That should move you into the folder. Don't be alarmed that it is empty... it should be at this stage.

Click the "New folder" button again, and create a folder called DD47, to stand for "Delphi Demo 47", which is my name for the program we are creating during this tutorial. (Yes, call it DELPHI Demo 47, even if you are doing this with Lazarus... changing all the references to DD47 is more than I can face!) Use "DD47", not a name of your choosing (this time!), because parts of the name get into things we're going to do, and you will be saved typing and confusion if you use DD47

Double click on the DD47 folder icon to move into the project's folder. It will also still be empty at this point.

The "Save project project1" dialog box should now have "DD47" in the "Save In:" edit box. We've got the "where" of the saving sorted out. Now we have the "what" to do...

In the edit box for File Name, change the proposed "project1.lpi" to "DD47.lpi", click the "Save" button. One consequence of this choice is that the default name for the .exe file will be DD47. (You can rename it, it will still run, but you might as well have it start with a good name... and yes, DD47 IS a good name!

As soon as the save is done, you'll get another Save dialog, but this time it is to "Save Unit1 (*.pas)". You will (or should already be!) in the right place, i.e. "My Documents\Lazarus Projects-(your initials)\DD47. You need to change the name for the file to dd47u1.pas. If, here, you don't use all lowercase, the system will suggest that you do. I agree.... And it will rename it for you, so you don't have to retype your entry, anyway.

The .lpr file is created and saved without any explicit effort by you.

Back to one tutorial for two languages....


We'll say more about saving in a moment. (If you use Windows Explorer or similar to look in the DD47 folder, don't worry about the extra things which will be there.)

I said there were four important Windows, and we've only covered three so far. The remaining window is the Object Inspector. It nicely has its name in its title bar. If for some reason you can't see it, as long as you have one of the Delphi or Lazarus windows selected, if you press F11, the Object Inspector should appear, and focus will be switched to it.

The Object Inspector tells us much more than we wanted to know about many, many things, but it is also useful. With it, you can get things the way you want them.

Make sure that the component palette's pointer button is currently selected. Then click on the button you put on the form. The box at the top of the Object Inspector should now be saying "Button1: TButton". This says that the button's name is "Button1", and that it is an object of type (important word, but we can leave it for now) "TButton".

The main part of the Object Inspector is made up of (Delphi:two) / (Lazarus: four) tabbed pages, most of them consisting of two columns. Make sure that the "Properties" tab is selected. In the left hand column, you should see...

Action
Align
Anchors
Autosize
... etc
Click on the edit box to the right of "Caption", where it says "Button1". Change "Button1" to Quit.

Now click somewhere in the form other than on the label or the button. At the top of the Object Inspector, you should now see Form1: TForm. Go down to the "Name" property (currently "Form1". (You may need to re-size the Object Inspector, and/ or use the scroll bar).... and enter "DD47f1", which re-names the form.

Do "File | Save All" again. You won't see any messages, but Delphi and Lazarus will save all the important files.

Do "Run | Run". Now that your application is a little bit bigger, don't expect the process of creating and running the .exe to happen "instantly". When the dots go away across the face of the form, you are looking at the running application. Your button should now say "Quit", which helps the user know what it is for! Use the button to go back to the "design" phase of the program development cycle.

Now... I said there were two buttons on the Delphi/Lazarus interface window which I find particularly useful. The first is the green, right pointing triangle. This is the equivalent of doing "Run | Run" with the menu. If it is grayed out, it means the program is already running. (Don't try it just now.

With the RAD (Rapid Application Development) environment still in "design mode", use the Object Inspector to change the name of the button on the form to "buQuit". Don't re-save yet.

The following is the left hand end of a Delphi 4 Delphi interface....

-

The third one, if you count as I do, is black, looks like a floppy disk. (Remember them?). Hover over it, and the pop-up says "Save". I don't want to talk about this one. I want to talk about the next one, the image of several floppies on top of each other. The pop-up says "Save All"... i.e. not just the file for the form you are working on (some projects have multiple forms), but also the .lpi file, the .lpr file, etc. Everything you could possibly want to save. Click this frequently as you work! If the button is grayed, it means that you haven't made any changes since the last save. (I asked you to change the button's name a moment ago so that the "Save All" button wouldn't be in the grayed state!) (In an earlier Delphi, before I overcame my aversion to screenshots(!), these were the icons: Two have a picture of a folder, and a small gray rectangle, wider than it is high. On my machine, they are the first two from the left, in the top row of buttons. On each, there is also an arrow. The rectangle is your hard disc. The icon with the arrow from the folder to the hard disc marks the "Save All" button.)

The Lazarus "Save All" button is similar...

-

... It is blue, instead of black, in the first row of icons, below the "h" in the menu item "Search", which is just above it.
Now, finally, we are going to address the nominal specific topic this tutorial, which is getting input from the user. We're not even going to do very much with the input... that's for another day! But we'll get it. As follows....

On the "Standard" tab of the component palette, click on RadioGroup "TRadioGroup, in Lazarus... but not "RadioButton" or "TRadioButton" which is also on the Standard Tab. Don't worry too much (yet) about getting the right component... you can delete mistakes and try again, if need be. We'll see if you got the right one in a moment. Try for the one near the right hand end. It is (Delphi and Lazarus) a gray rectangle with a blue "sp lodge" on its upper left edge, and some blue lines, each preceded by a dot inside the rectangle. If you hover the mouse pointer, it will say RadioGroup (TRadioGroup) when you're over the right one.

Now click on the form. The object inspector should confirm that you did get a Radio Group, by saying that the object is "RadioGroup1: TRadioGroup". If it doesn't, delete the object you added (press delete key), and try again!

If necessary, move the label and button you created earlier to make room for the RadioGroup. You can also adjust the size of the form.

Be sure that the RadioGroup is selected. Its name will be in the box at the top of the Object Inspector.

In the Object Inspector, you'll see a row called "Items", with "(TStrings) in the box in the right-hand column.

-

Double click on TStrings (or click on the "TStrings", and then click on the ellipsis (little square with three dots)). This will bring up the String List Editor (Delphi) / Stings Editor Dialog (Lazarus).

Type in Red, Blue, Green, in that order. Press enter after Red and after Blue, so that each words appears on a separate line. There should be a status message on the editor window saying that you have 3 lines. Click the OK button. Do a "Save All", before you forget!

If you now run the program, you will see some radio buttons. If you click any of them, it will become "checked", and any that was checked previously will become unchecked.

A "minor" untidiness, which we could probably live with, should be tidied away. At present, when you start the program, NONE of the radio buttons is pressed. We'll fix that. It may seem like a little bit of black magic for the moment. Don't worry... just use the trick.

Quit the application, so that you are back in the design mode.

Get the form showing. If it isn't already, be sure that one of the Delphi or Lazarus windows is currently the window with focus (i.e. "active") and press F12 once or twice. The form should appear. Double click somewhere on it NOT on the label, the button, the radiogroup. You should be switched to the code window (as you were when we told the Quit button what it's job was), and you should be seeing...
procedure TDD47f1.FormCreate(Sender: TObject);
begin

end;
DON'T click "Save All" just now... Delphi will "see" the empty "FormCreate" skeleton, and remove it. On the line just after the "begin", add...
radiogroup1.itemindex:=0;

Be sure that line ends with one semicolon. For Delphi, you will have to type it. For Lazarus, the RAD environment will have provided one for you.

This will cause the first (item "0") of the radio buttons to be checked when the program starts up. Click "Save All" now, and try running the application again. This time, the first radiobutton ("Red") should be ticked when the application fires up.

If you did NOT get "procedure TDD47f1.FormCreate...." when you tried for it, just leave whatever you get, go back to the form (pressing F12 is the easy way) and try again. In Delphi, spare, empty things will be removed for you. In Lazarus, you will have to do your own tidying. That's harder than it seems... for now, just leave extra things which have been added by Lazarus. They won't do as much harm as you trying to remove things, and not getting it right!

Well... so far so good. We have a way to let users tell the program their wishes. How do we get the program to take notice of those inputs? That's really a story for another tutorial, but as you have done all the work you've done, you deserve something, so here's a simple use of the input....

Go back to the form. Double click somewhere within the RadioGroup. Turn what you get into...
procedure TDD47f1.RadioGroup1Click(Sender: TObject);
begin
if radiogroup1.itemindex=0 then label1.caption:='You pressed RED';
if radiogroup1.itemindex=1 then label1.caption:='You pressed BLUE';
if radiogroup1.itemindex=2 then label1.caption:='You pressed GREEN';
end;
(Note that the first radio button is "number zero". Computer programmers often count the first thing in a list as "zero". It can be very helpful. Delphi isn't entirely consistent... it sometimes counts things from "1".) (I hope you are making good use of "copy" and "paste".)

Now try running the program, and clicking the different radio buttons. The text in the label on your form should change when you change the "checked" radio button.
Of course, our program doesn't do much... but there will be many times when you need to make settings from a limited list of choices, and the RadioGroup component gives you one way to do that. See my Level 3 tutorial on getting input for another option, which is a level 3 tutorial as it moves more quickly, gives you less hand holding, and explores more subtle ideas.




Click here if you're feeling kind! (Promotes my site via "Top100Borland")
   Search this site or the web        powered by FreeFind
 
  Site search Web search
Site Map    What's New    Search
Ad from page's editor: Yes.. I do enjoy compiling these things for you... I hope they are helpful. However.. this doesn'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 (tm) freeware, shareware page.

Link to Tutorials main page
Here is how you can contact this page's author, Tom Boyd.


Valid HTML 4.01 Transitional Page WILL BE tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org


If this page causes a script to run, why? Because of things like Google panels, and the code for the search button. Why do I mention scripts? Be sure you know all you need to about spyware.

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