Delphi Programming for Beginners - Lesson 9

Other useful components

TDateTimePicker

The purpose of this component is to choose the date or the time (or both). In the pictures below you may see it in several modes:

Delphi Course Delphi Course

The view mode is determined by following properties: DataFormat, Format, DateMode and Kind and ShowCheckbox – you should try this right in the IDE.The most complex property is Format. You can set time and date directly via properties Date and Time.

TPaintBox and drawing with TCanvas

In Delphi, drawing is implemented via TCanvas class which represents a canvas and at the same time it contains methods for drawings (for example printing the text, drawing lines, circles or squares).

Where can we find TCanvas? A lot components contains drawing on TCanvas internally, we can as well draw via canvas on bitmap (and use it after that) and other options.

Component TPaintBox is special because itself, it doesn’t have any look, but it allows us to draw easily on TCanvas – thus we can easily do a graphical output. Unlike the component TImage, which displays a bitmap (which we can draw on via its TCanvas), we have to do drawing on our own via event OnPaint.

Let’s do a small, but really interesting project. Put component TPaintBox on the form, set its align on alClient and add following piece of code to its OnPaint event:


procedure TForm1.pb1Paint(Sender: TObject);
 var
  iX, iY: Integer;
  iAmp: Integer;
begin
 iAmp := pb1.Height div 2;
 with pb1.Canvas do
 begin
  Font.Size := 30; //velikost písma
  Font.Color := ClRed; // barva písma
  TextOut(0, iAmp, 'Delphi'); // nápis
  for iX := 0 to (Width - 1) div 2 do
  begin
   iY := Round(sin ((ix + fpos)/ (2*PI)) * iAmp);
   Pen.Color := RGB(0, 0, fPos + ix);
   MoveTo(iX*2, Height);
   LineTo(ix * 2, iAmp + iY);
   Pen.Color := RGB(0, 0, 0); // cerna
   MoveTo(iX*2 + 1, Height);
   LineTo(ix * 2 + 1, iAmp + iY);
  end;
 end;
end;

Pen.Color is the color of a line, MoveTo moves the position of drawing and LineTo draws a line. Variable fpos:byte is a variable of the form and in OnCreate is set to 0. Add TTimer on the form, set enabled to True and interval to 20 (the speed of an animation). The code in the body of Timer looks like this:


procedure TForm1.tmr1Timer(Sender: TObject);
begin
 inc(fpos, 2);
 pb1.Invalidate; //
end;
Delphi Course

And that’s all. Our program will animate interestingly drawn sinusoid with a text in the background. We can as well draw pictures (for example from ImageList) etc. Code of this program is at the end of this article.

Dialogues for opening and saving files

These 2 non-visual components, TSaveDialog and TOpenDialog, are a very important part of every program and their purpose is to choose which file users want to open or save.

Both of components are very similar and via properties Options and OptionsEx you can change how they look (for example whether a file must exist, whether a dialog is expandable etc.).

Delphi Course

You can either put both components on the form and specify their properties there or create them during the run of the program.

Delphi Course

Delphi Course

Important properties are FileName, Filter (string with filters for files), FilterIndex (ordinal number of preselected filter), InitialDir (initial directory) and Title (title of the window).

If we have a dialog on the form, we can simply write>


if dlgOpen1.Execute then
  ShowMessage('Selected ' + dlgOpen1.FileName);

Dialog will be shown via method Execute and returns boolean value based on whether a selection was done successfully. Selected file is saved in property FileName (including the path). Before calling, we can fill Filename with an initial file.

If we don’t want to put components on the form, we can create dialog dynamically (component will be created once is needed which is convenient for rarely used parts of the code), for example like this:


with TOpenDialog.Create(nil) do
begin
 try
  Options := [ofFileMustExist, ofEnableSizing];
  if Execute then
  ShowMessage('Selected ' + FileName);
 finally
  Free;
 end;
end;

Homework

Homework: Create a simple text editor capable of loading and saving files.

Sample project regarding homework can be downloaded from here.

About the Author

Ing. Radek Červinka
Embarcadero MVP
Web: Delphi.cz
Twitter: @delphi.cz

Note from Torry.net

Today's article is last one from Delphi for the Beginners series. You may look forward to next couple of articles focused on special tasks like compound interest calculation, arithmetic and geometric progression or matrixes.


More interesting links and pages


All binary option robots reviewed


Valforex.com Reviews


Breaking News & Headlines


FX Tribune - Breaking Daily News


USA News


BestVPNRating.com


Do my homework 123


My essay geek


My essay writing


Car hire prices comparison


College paper world


Essay writing help


What is control panel?