Posted 11 years ago on 2/26/2008 and updated 10/24/2009
Take Away:
This primer is intended for those just getting started in Delphi and focuses on displaying things.
KB100883
Displaying Things in Delphi
One of the easiest ways to learn a language is to jump right in and make it do things. You can take this tutierial right on-line. Just task switch beween your browser and Delphi. In this tutorial, you will create a form with several buttons on it. Each button will demonstrate a technique for displaying information to the user. Lets get started by doing the following to do steps.
To do:
Start Delphi
Note We strongly recommend that you type in and run each exercise. Just reading about something isn't nearly as good as doing it.
1. Using ShowMessage
ShowMessage displays a simple dialog box with the text you provide it. It is one of the most used ways of displaying information.
To do:
Create a new application by selecting File | New Application.
Place a button on the form and change its Caption property to ShowMessage.
Double click on the button to bring up the editor.
Type line 3 from the code listing (on the right) between the begin and end.
Run and test the program by selecting the Run VCR style button (looks like a play button).
Notes
The user has no way to modify the text.
Notice the semi-colon at the end of the line you typed in.
If you have trouble compiling, try the copy/paste method to typing.
Code Listing
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage('Hello World!');
end;
2. Using MessageBox - Part I
MessageBox is similar to ShowMessage but gives you more control over how it displays. This one is a favorite of developers because it is a Windows API function wrapped in a Delphi method. This is important because many Windows development languages support the MessageBox function.
To do:
Add a button to our form (this time change its Caption to MessageBox1).
Double click on the button to bring up the editor.
Type line 3 from the code listing between the begin and end.
Run and test the program.
Notes:
The begin and end mark this methods code. Notice that the end has a ; after it.
There is a main begin and end that mark the applications code. Notice the final end has a . after it. (You can see this by switching back to Delphi.)
Code Listing
procedure TForm1.Button2Click(Sender: TObject); begin Application.MessageBox('Hello World!', 'My First App', 0); end;
3. Using MessageBox - Part II
MessageBox also allows you to display multiple buttons and know which the user pressed. This is very usefull for limited yes/no type user interaction.
To do:
Add a button to our form (set its Caption to MessageBox2).
Double click on the button to bring up the editor.
Type lines 2 and 3 from the code listing above begin.
Type Lines 5 and 6 between the begin and end.
Run and test the program.
Notes:
Notice we declared sAns to be an integer. Then assigned the return value from MessageBox to it.
Because Pascal is a highly typed language, we must use methods like IntToStr to convert a value to the correct type for the given method (as in line 6).
Code Listing
procedure TForm1.Button3Click(Sender: TObject);
var
iAns : Integer;
begin
iAns := Application.MessageBox('Is this fun?', 'Question', MB_YESNO);
ShowMessage(IntToStr(iAns));
end;
4. Using InputBox
InputBox allows you to set a value, display it to the user, and have the user change it. Although InputBox generally isnt flashy enough for finished applications, it is great for debugging and smaller in-house applications.
To do:
Add a button to our form (set its Caption to InputBox).
Double click on the button to bring up the editor.
Add lines 2, 3, 5, and 6 from the code listing to it.
Run and test the program.
Notes:
Notice we used to the + symbol to concatenate two strings (a literal and a variable).
Because themethod Button4Click belongs to the class TForm1, the first line must include both to uniquely identify the method. Youll learn a lot more about object oriented programming as you learn Delphi. For now, this simple definition will work.
Code Listing
procedure TForm1.Button4Click(Sender: TObject);
var
sAns : String;
begin
sAns := InputBox('Question', 'What is your name?', 'Tiger Woods');
ShowMessage('Hello ' + sAns);
end;
5. Using the StatusBar Component
The StatusBar component allows you to write text to the status bar. This is one of the most common ways to display information to a user without stopping their work.
To do:
Add a StatusBar component from the Win95 tab to the form.
Change the SimplePanel property of the StatusBar to True.
Add a button to our form (set its Caption to StatusBar).
Add lines 2 and 3 from the code listing to the Click event of the button.
Run and test the program.
Notes:
Notice we used two apostrophies in a row to represent one apostrophe in a literal string.
Notice the use of dot notation in line 3 to set a property of an object.
PrestwoodBoards.com was developed and is maintainted by me. Do you have a question or suggestion? Do you see a problem? Contact me now. My goal is to build an ad-free and spam-free source of I.T. information with many contributers (ok to promote your website/company in your bio). Yes, my company Prestwood IT Solutions is mentioned in my bio which shows with every post, but you can contribute and promote your pet project too!