Saturday, October 3, 2009

Delphi beginner tutorial

Delphi is a wonderful language and has the most powerful IDE(Integrated Development Environment -- the place where you design your application and type code) on the market!
This tutorial is strictly for beginners in Delphi, it should give you a good understanding about how you should tell Delphi what you want.
Let's say that your in a foreign country and you don't speak their language, here is where you need a translator(in our case Delphi -- translates human readable code into machine code), now the translator could be someone that speaks your language and the foreign language but what if doesn't know too many words? then you have to figure out how to tell him what you want and also understand him in the same time, you dizzy yet?
If you have some computer programming background this will be very easy if not then you have to know that a programming language has some keywords which are reserved this means that you cannot name a variable or function using a keyword i.e. begin.
Here's a simple Delphi console application which shows some text
Note
To create a console application go to main menu File -> New -> Console Application or if Console Application is not available from New Menu then File -> New -> Other... -> Console Application
program Project1;

begin
  WriteLn('Hello World from Delphi console application!');
  ReadLn;
end;
program tells the compiler that the next word is the name of you program
begin marks the beginning of a code block(i.e. a := 2009; b := 2010;)
end marks the end of a code block
WriteLn is a procedure which prints text
ReadLn reads one line from the console, user types text and when he hits return(Enter key) bam!
A string in Delphi is between ' and ' in other programming languages like C#, C++, C, Java, etc. a string is between " and ".
As I always say that an example speaks for itself fallow this steps to create your first GUI(Graphical User Interface) application:
- start Delphi(any version)
- from main menu go to File -> New -> Application(or VCL Forms Application)
if you haven't customized your IDE yet then in your left side of the screen there should be Object Inspector and under main menu(or right side of the screen depending on you customization or Delphi version) Tool Palette, Tool Palette holds you VCL(Visual Component Library) components but not only, there are also non-visual components(ex. TActionList).
- from Standard double-click a TButton component it will be automatically added on your form, move it where you wish and then double-click it to write some code, now your in the code editor part of the IDE, you should see something like this
procedure TForm1.Button1Click(Sender: TObject);
begin

end;
when you create a new VCL application Delphi is sub-classing a TForm object in order to create your application's main form and other which you add whenever you need a new form, that's why there's a TForm1 in the code, DO NOT change it manually! go to object inspector and find the Name property and change it from Form1 to frmMain for example, if you scroll up in the editor you should see something like this(I haven't changed the name of the form from Form1 to frmMain)
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin

end;

end.
let's move forward, Button1Click is an object method which is triggered when you click the button, if you select the button on the form and go to Object Inspector and change the name from Button1 to myCoolButton for example then in stead of TForm1.Button1Click you will have TForm1.myCoolButtonClick, Delphi IDE automatically modifies your source code so you don't waste time
Note
The F12 key which swaps between code editor(where you write your code) and form designer(where you drop components and modify their size, etc.), so don't waste time moving windows around and resize them just remember the F12 key.
Let's tell our translator(Delphi) to show a message to the world, we can do that by typing ShowMessage('Hello world from Delphi!'); between begin and end; of Button1Click method, so
procedure TForm1.Button1Click(Sender: TObject);
begin

end;
should look like this after adding the ShowMessage method
procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage('Hello world from Delphi!');
end;
After adding ShowMessage(...) don't forget to add semicolon ";" it tells Delphi compiler that that's the end of that expression, if you forget you will see a lot of "Missing operator or semicolon" in the message box at the bottom of the IDE.
Whenever you wish to test your application press the F9 key and Delphi generates and executable application in the directory where you saved your project, if you press F9 key you will see that a form with one button, press the button and you get this message Hello world from Delphi!.
Let's try to create another application a bit more complicated, go to File -> New -> (VCL Forms Application or Application), from Tool Palette select add a TListBox component and TButton, move and/or resize as you please, double-click Button1 and between begin and end write Listbox1.Items.Add('New ListBox1 item');
procedure TForm1.Button1Click(Sender: TObject);
begin
  ListBox1.Items.Add('New ListBox1 item');
end;
select Button1 from form designer, go to Object Inspector find Caption property and change it from "Button1" to "Add Listbox Item", press F9 key and now start adding items to listbox by clicking the button, nice and simple huh?
Close the application, go to Tool Palette and add a TEdit and another TButton component on the form, move it around, resize, etc., select the new button and go to Object Inspector and change caption to "Add TEdit item", if you don't like that TEdit component default text is Edit1 then select it go to Object Inspector find the Text property and change it to whatever you like.
Double-Click the new button and add ListBox1.Items.Add(Edit1.Text);
procedure TForm1.Button2Click(Sender: TObject);
begin
  ListBox1.Items.Add(Edit1.Text);
end;
press F9 modify the text in TEdit component and press the "Add TEdit item" button and you should see a new item in the Listbox with the text from TEdit component, close application, add a new button to the form, change caption to "Clear Listbox" double-click it and write this code Listbox1.Clear;
procedure TForm1.Button3Click(Sender: TObject);
begin
  ListBox1.Clear;
end;
when you press this button all items from listbox will be deleted.
I don't know how you application looks like but here's mine

I belive this should be enough to get you started on your first application as a Delphi programmer, other information like loops, conditions, classes, etc. are easier to understand if you browse this blog or Google for Delphi examples/source.
If you need more information don't hesitate to ask for more examples and/or tutorials.

4 comments:

  1. Thanks a million! Beginners are beginners.
    First tutorial that I found on the net that actuallystarted from ground zero!
    I had BASIC and Fortran years ago.
    More recently some VB, ASM, TSM (microcontrollers), Access, Ladder Logic (Programmable Logic Controllers)
    and possibly some more that I have escapes me at the moment.
    I have a very good tutorial on C++ and that should help.
    Hope you do a follow up!

    Tony

    ReplyDelete
  2. I'm glad this helped you, thank you for comment.
    Hang around there's more to come.

    ReplyDelete
  3. where is the best place to learn delphi starting from scratch. This helped but is there a place I can go for a complete directory of functions like the showmessage and writeln types of stuff along with any additional stuff for beginers.

    Thank you for your help with the post

    ReplyDelete
  4. Hi Jayson,
    I'm really NOT sure if there is any Delphi blog/website which would give deeper insight for beginners, http://delphi.about.com has some good examples with comments.
    If you want my advice on learning Delphi, note this:
    - learn what bit, byte, integer, double and pointer are for a start: 8 bits = byte(octet); 4 bytes = a integer(32 bit integer); 8 bytes = a double, a 32 bit pointer occupies 4 bytes;
    - learn syntax, you should NOT make simple mistakes as NOT knowing where to put a semicolon or that you need to double a quote inside a string ex: 'that''s my string'
    When you learned this basic stuff which should take you less than 1 week come back and you will understand more when you read code.
    Just so you know, programming is NOT a easy job, therefore if you wish to start learning a programming language you should be willing to sacrifice some of your time, just so you know you will never stop learning, in 10 years from now you cannot say: I know all there is to know, you will always learn something new.

    ReplyDelete

Blogroll(General programming and Delphi feeds)