Overview  tips and tricks :                                         Last update : 05-01-2000Home

A.Basic tips

A1. Dynamically creating forms
A2. Dynamically creating components and controls
A3. Making a round form
A4. Using a splashform

B.
Common tips advanced

B1. Procedure for enabling or disabling all controls on a panel
B2. Procedure for opening all TTables in a datamodule
B3. Using the Enter as Tab key
B4. Launching applications with ShellExecute
B5. Launching applications with WinExec

 

A1. Dynamically creating forms

Underneath code shows the common way to create dynamic a form.
(Don't forget to move the form from the list Auto-create-forms to the list
available forms in Delphi (menu option Project-Option- tab forms.)

  var MyForm : TForm;
  ......// More code
  MyForm:=TForm.Create(Self); // Create MyForm
  MyForm.Showmodal;
  MyForm.Free;// ....and free it!

arrows.gif (215 bytes)Top


A2. Dynamically creating components and controls

Components and controls can also be created dynamically.
You have to specify also the parent of the control.
(The control must know where he (or she) must draw itself)

  var MyButton : TButton;
  ......// More code
  MyButton := TButton.Create(Self); // Create MyButton
  MynButton.parent := Self; // self is the Form-object from which the call is coming
// Or MijnButton.Parent := Panel1;

arrows.gif (215 bytes)Top


 

A3. Making a round form

With the window api function SetWindowRgn can you make special forms :

procedure TForm1.FormCreate(Sender: TObject);
var Region : hRgn;
begin
  Region := CreateEllipticRgn(0, 0, Width , Height);
  SetWindowRgn(Handle, Region, True);
end;

arrows.gif (215 bytes)Top


A4. Making a splash form

A splashform is a form which is shown when an application starts.
The splashform is somehow to keep the user busy while the application is starting.
Place the next code in the project file of the application between the begin and end to add a splashform to your application.

program SplashApp;

uses
  Forms,
  Mainunit in 'MAINUNIT.PAS' {MainForm},
  USplash in 'USplash.pas' {SplashForm},
 
{$R *.RES}

begin
  //Create the splashform and show it!
  SplashForm:=TSplashForm.Create(Application);
  SplashForm.Show; SplashForm.UpDate;

  Application.CreateForm(TMainForm, MainForm);//Start the application

  SplashForm.Hide; SplashForm.Free;//Free the splashform
  Application.Run;//Run the application
end.

B1. Procedure for enabling or disabling all controls on a panel

To enable or disable all controls on a panel  the property enabled has to be set to true or false.
Disabled controls (enabled = false)can not respond to user actions and they are often light grey.
Manually setting all enabled properties to true or false in code is not very effective. This code does this
automatic, using the property Controls en Controlcount of the panel

Controls ( type TWinControl) is declared as follows :

property Controls[Index: Integer]: TControl;

De property Controls is an array type which holds all the  Child controls of the panel.
through the index all child controls can be edited.
ControlCount holds the number of  child controls.

procedure TEenDM.EnablePanel (aPanel : TPanel);
var Teller : Integer;

begin
  For Teller := 0 to aPanel.ControlCount - 1 do
  begin
     aPanel.Controls[Teller].Enabled := True;
  end;
end;

procedure TEenDM.DisablePanel (aPanel : TPanel);
var Teller : Integer;

begin
  For Teller := 0 to aPanel.ControlCount - 1 do
  begin
     aPanel.Controls[Teller].Enabled := False;
  end;
end;

arrows.gif (215 bytes)Top


B2. Procedure for opening all TTables in a datamodule

To open all TTables in a datamodule at the start of the application can be very bussy work
The property Active of all the TTables must be set to True.
Just as shown in tip C2 this can be easily automated in one procedure.
The difference is that TTables are components and not controls.
The working is practical the same as tip C2, only we have to be sure that we are dealing with a TTable.
This can be done with the key is.

procedure TEenDM.OpenEenDatabase;
var i ,n : Integer;
begin
  n := 0;
  try
  for i := 0 to ComponentCount-1 do
    begin
      n := i;
      if (Components[i] is TTable) then
      with TTable(Components[i]) do
        Active := True;
    end;
  except
    messagedlg('Fout in het openen van de tabel '+
    (Components[n] as TTable).name ,mtInformation, [mbOk] , 0);
  end;
end;

arrows.gif (215 bytes)Top


B3. Using the Enter key as Tab key

In DOS it was normal to navigate between edits with the <ENTER> key.
In Windows is this standard the <TAB>key.
A <ENTER> triggers often a default button which closes the dialog or form.
This code gives the <ENTER> key the same habbit as the <TAB>key.
This happens through the windows message WM_NEXTDLGCTL (see also the  Delphi help)
Perform acts as if the control received the windows message. The standard Windows message is bypassed..
Key:= #0 is doing as if no key was pressed at all.
The property KeyPreview of the form must be set to true !

procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    Perform( WM_NEXTDLGCTL, 0, 0 );
    Key := #0;
  end;
end;

arrows.gif (215 bytes)Top


B4. Launch applications with ShellExecute

With the windows api function ShellExecute function you can launch  other applications from you application.
The parameters of Shellexecute are shown below :

HINSTANCE ShellExecute(

HWND hwnd,                    // handle to parent window
LPCTSTR lpOperation,     // pointer to string that specifies operation to perform
LPCTSTR lpFile,                // pointer to filename or folder name string
LPCTSTR lpParameters,  // pointer to string that specifies executable-file parameters
LPCTSTR lpDirectory,      // pointer to string that specifies default directory
INT nShowCmd                 // whether file is shown when opened
);

(See the Delphi help for more detail information
)
When the function returns a value smaller then 32 then launching has not succeeded.
The next code starts an application :

procedure TForm1.Button1Click;
Var X:String;

begin
  x:='C:\Program Files\Borland\Delphi5\bin\Delphi.exe';
  if ShellExecute(0, nil, PChar(x), nil, nil, SW_SHOWDEFAULT) <= 32 then 
    ShowMessage('Can't launch the application');
end;

 

Besides executables you can also launch associated documents, url's and mail-to's.
TLaunchLabel, from our components pages also uses ShellExecute.

arrows.gif (215 bytes)Top


B5. Launch application with WinExec

An alternative way to launch applications is the function   WinExec  :

procedure TForm1.Button1Click;
Var X:String;

begin
  x:='C:\Program Files\Borland\Delphi5\bin\Delphi.exe';
  if WinExec(PChar(x),SW_SHOWNORMAL) <= 32 then 
    ShowMessage('Can't launch the application');  
end;

arrows.gif (215 bytes)Top