📱

Tournez votre téléphone

Tournez votre appareil pour une meilleure expérience.

Application simple

Article publié le Mardi 06 janvier 2026 , lu 22 fois

Illustration : Application simple

Présentation

Application simple avec création, lecture, mise à jour et suppression (CRUD), recherche et sauvegarde (CSV ou SQLite).

1. Prérequis

2. Fonctionnalités prévues

3. Créer le projet

  1. Fichier → Nouveau → Application VCL (VCL Forms Application).
  2. Sauvegardez le projet : File → Save All → dossier Projets\ContactsVCL.

4. Conception de l'interface

Sur Form1, placez les composants suivants :

Nommez clairement les composants (ex. edtNom, btnAjouter, lvContacts) pour faciliter le code.

5. Structure des données (record)

Nous utilisons un record pour représenter un contact et une liste TList<TContact> (génériques) pour stocker en mémoire :

type
TContact = record
	Prenom: string;
	Nom: string;
	Email: string;
	Tel: string;
	Adresse: string;
end;

  TContacts = TList;

var
  Contacts: TContacts; // initialiser dans FormCreate
			
Pascal

6. Code : initialisation et utilitaires

uses
System.Generics.Collections, System.SysUtils, Vcl.ComCtrls;

procedure TForm1.FormCreate(Sender: TObject);
begin
	Contacts := TContacts.Create;
	// config ListView en mode report
	lvContacts.ViewStyle := vsReport;
	lvContacts.Columns.Clear;
	lvContacts.Columns.Add.Caption := 'Prénom';
	lvContacts.Columns.Add.Caption := 'Nom';
	lvContacts.Columns.Add.Caption := 'Email';
	lvContacts.Columns.Add.Caption := 'Téléphone';
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
	Contacts.Free;
end;

procedure TForm1.RefreshListView;
	var
	i: Integer; lvItem: TListItem; c: TContact;
	
	begin
		lvContacts.Items.BeginUpdate;
		try
		lvContacts.Items.Clear;
		for i := 0 to Contacts.Count - 1 do
		begin
			c := Contacts[i];
			lvItem := lvContacts.Items.Add;
			lvItem.Caption := c.Prenom;
			lvItem.SubItems.Add(c.Nom);
			lvItem.SubItems.Add(c.Email);
			lvItem.SubItems.Add(c.Tel);
			end;
			finally
			lvContacts.Items.EndUpdate;
		end;
	end;
			
Pascal

7. Ajouter un contact

procedure TForm1.btnAjouterClick(Sender: TObject);
var
  c: TContact;
begin
	// Validation simple
	if Trim(edtNom.Text) = '' then
	begin
	ShowMessage('Nom requis');
	Exit;
end;

	c.Prenom := Trim(edtPrenom.Text);
	c.Nom    := Trim(edtNom.Text);
	c.Email  := Trim(edtEmail.Text);
	c.Tel    := Trim(edtTel.Text);
	c.Adresse:= Trim(edtAdresse.Text);

	Contacts.Add(c);
	RefreshListView;

	// Réinitialiser les champs
	edtPrenom.Text := '';
	edtNom.Text := '';
	edtEmail.Text := '';
	edtTel.Text := '';
	edtAdresse.Text := '';
	edtNom.SetFocus;
end;
			
Pascal

8. Sélection / Modification / Suppression

Lorsqu'un élément est sélectionné dans le ListView, chargez ses données dans les champs pour modification :

procedure TForm1.lvContactsSelectItem(Sender: TObject; Item: TListItem; Selected: Boolean);
var
	idx: Integer; c: TContact;
begin
	if not Selected then Exit;
	idx := Item.Index;
	c := Contacts[idx];
	edtPrenom.Text := c.Prenom;
	edtNom.Text := c.Nom;
	edtEmail.Text := c.Email;
	edtTel.Text := c.Tel;
	edtAdresse.Text := c.Adresse;
end;

procedure TForm1.btnModifierClick(Sender: TObject);
var
	idx: Integer; c: TContact;
	
begin
	if not Assigned(lvContacts.Selected) then
	begin
		ShowMessage('Sélectionnez un contact');
		Exit;
	end;
	idx := lvContacts.Selected.Index;
	c.Prenom := Trim(edtPrenom.Text);
	c.Nom    := Trim(edtNom.Text);
	c.Email  := Trim(edtEmail.Text);
	c.Tel    := Trim(edtTel.Text);
	c.Adresse:= Trim(edtAdresse.Text);
	Contacts[idx] := c;
	RefreshListView;
end;

procedure TForm1.btnSupprimerClick(Sender: TObject);
var
	idx: Integer;
	
begin
	if not Assigned(lvContacts.Selected) then
	begin
		ShowMessage('Sélectionnez un contact');
		Exit;
	end;
	idx := lvContacts.Selected.Index;
	if MessageDlg('Supprimer ce contact ?', mtConfirmation, [mbYes, mbNo], 0) = mrYes then
	begin
		Contacts.Delete(idx);
		RefreshListView;
	end;
end;
			
Pascal

9. Recherche

Recherche simple par nom ou email — filtre en mémoire :

procedure TForm1.btnRechercheClick(Sender: TObject);
var
	i: Integer; q: string; c: TContact; lvItem: TListItem;
  
begin
	q := LowerCase(Trim(edtRecherche.Text));
	lvContacts.Items.BeginUpdate;
	try
		lvContacts.Items.Clear;
		for i := 0 to Contacts.Count - 1 do
		begin
		  c := Contacts[i];
		  if (q = '') or (Pos(q, LowerCase(c.Nom)) > 0) or (Pos(q, LowerCase(c.Email)) > 0) then
			begin
				lvItem := lvContacts.Items.Add;
				lvItem.Caption := c.Prenom;
				lvItem.SubItems.Add(c.Nom);
				lvItem.SubItems.Add(c.Email);
				lvItem.SubItems.Add(c.Tel);
			end;
		end;
		finally
		lvContacts.Items.EndUpdate;
	end;
end;
			
Pascal

10. Sauvegarde / Chargement (CSV simple)

Format CSV basique : chaque contact sur une ligne, champs échappés par ; ou autre séparateur.

procedure TForm1.btnSauverClick(Sender: TObject);
var
	sl: TStringList; i: Integer; c: TContact; line: string;

begin
	sl := TStringList.Create;
	try
	for i := 0 to Contacts.Count - 1 do
		begin
			c := Contacts[i];
			// échapper ; si besoin
			line := Format('"%s";"%s";"%s";"%s";"%s"', [c.Prenom, c.Nom, c.Email, c.Tel, c.Adresse]);
			sl.Add(line);
		end;
		sl.SaveToFile('contacts.csv');
		ShowMessage('Sauvegardé dans contacts.csv');
		finally
		sl.Free;
	end;
end;

procedure TForm1.btnChargerClick(Sender: TObject);
var
	sl: TStringList; i: Integer; parts: TArray; c: TContact; s: string;
	
begin
  if not FileExists('contacts.csv') then
	begin
		ShowMessage('Fichier contacts.csv introuvable');
		Exit;
	end;
	sl := TStringList.Create;
	try
		sl.LoadFromFile('contacts.csv');
		Contacts.Clear;
		for i := 0 to sl.Count - 1 do
		begin
		  s := sl[i];
		  // Supposons le format "a";"b";"c";... -> retirer les " puis Split(';')
		  s := s.Replace('"','');
		  parts := s.Split([';']);
		  if Length(parts) >= 4 then
			begin
				c.Prenom := parts[0];
				c.Nom    := parts[1];
				c.Email  := parts[2];
				c.Tel    := parts[3];
				if Length(parts) >= 5 then c.Adresse := parts[4]
				else c.Adresse := '';
				Contacts.Add(c);
			end;
    end;
    RefreshListView;
	finally
		sl.Free;
	end;
end;
			
Pascal

11. Option avancée : utiliser SQLite via FireDAC

Si vous préférez une base embarquée, utilisez FireDAC + SQLite (composants FireDAC disponibles dans Delphi). Exemple rapide :

-- Créez un DataModule, placez TFDMemtable / TFDConnection / TFDQuery
-- Exemple pseudo-configuration :
FDConnection1.Params.DriverID := 'SQLite';
FDConnection1.Params.Database := 'contacts.db';
FDConnection1.Connected := True;

// SQL pour création de table (si pas d'existence)
FDConnection1.ExecSQL('CREATE TABLE IF NOT EXISTS contacts (id INTEGER PRIMARY KEY AUTOINCREMENT, prenom TEXT, nom TEXT, email TEXT, tel TEXT, adresse TEXT)');

// Pour charger
FDQuery1.SQL.Text := 'SELECT id, prenom, nom, email, tel, adresse FROM contacts';
FDQuery1.Open;

// Pour insérer
FDQuery1.SQL.Text := 'INSERT INTO contacts (prenom, nom, email, tel, adresse) VALUES (:p1,:p2,:p3,:p4,:p5)';
FDQuery1.Params[0].AsString := c.Prenom; // etc
FDQuery1.ExecSQL;
Pascal

L'approche FireDAC est préférable pour des volumes importants, recherche SQL, tris, et intégrité.

12. Bonnes pratiques & améliorations

13. Debug & test

14. Exemple de packaging

Sous Windows, générez l'exécutable (Release), vérifiez les DLL nécessaires si vous utilisez SQLite (sqlite3.dll), et fournissez le dossier de données (contacts.csv ou contacts.db).

15. Captures d'écran — pas à pas