Nous allons découvrir ici une autre manière d'afficher les données, il s’agit des dialogues qui utilise un système de layouts extrêmement efficace.

Première Partie (présentation)

Deuxième Partie
mise en place de la fenêtre de la table des contacts

Troisième Partie
mise en place de la fenêtre fiche d'un contact

Quatrième Partie
mise en place de la base de donnée, ajout d'un contact

Cinquième Partie(Fin)
Affichage des contacts dans la table,édition d'un contact,suppression d'un contact


Le code complet (ce code peut différé du code des vidéos)
Le fichier Main.pbi
Code : Tout sélectionner
EnableExplicit
UseSQLiteDatabase()
#Title="Tuto Dialog"
#D_contact_table=0
#D_contact_sheet=1
Global gDbName$="Contact.sqlite"
Global gXmlContactTable$="Contact_table.xml"
Global gEvent,gExit.b=#False
Global IdXml
Global IdContactTable,IdContactSheet
Global gModeEdit.b=#False
Procedure LoadForm()
IdXml=LoadXML(#PB_Any,gXmlContactTable$)
If IdXml=0 Or XMLStatus(IdXml)<>#PB_XML_Success
MessageRequester("XML error on line ", Str(XMLErrorLine(IdXml)) + ": " + XMLError(IdXml))
ProcedureReturn #False
EndIf
ProcedureReturn #True
EndProcedure
Procedure TestEdit()
Protected idTable=DialogGadget(#D_contact_table,"table")
If GetGadgetState(idTable)=-1
DisableGadget(DialogGadget(0,"edit"),#True)
Else
DisableGadget(DialogGadget(0,"edit"),#False)
EndIf
EndProcedure
Procedure MakeDb()
Protected Query$
;Création du fichier de la base de données sqlite
If FileSize(gDbName$)=-1
CreateFile(0,gDbName$)
CloseFile(0)
EndIf
;Ouvre la base de donées
OpenDatabase(0,gDbName$,"","",#PB_Database_SQLite)
;Génére la requète
Query$="CREATE TABLE If Not EXISTS CONTACT ("
Query$+"Id INTEGER PRIMARY KEY AUTOINCREMENT,"
Query$+"name VARCHAR(50),"
Query$+"adress VARCHAR(80),"
Query$+"city VARCHAR(50),"
Query$+"zip_code VARCHAR(10),"
Query$+"contry VARCHAR(50),"
Query$+"phone VARCHAR(20 ),"
Query$+"mail VARCHAR(80)"
Query$+")"
;Envoie de la requète
If DatabaseUpdate(0,Query$)=0
MessageRequester("Erreur Sql",DatabaseError())
EndIf
CloseDatabase(0)
EndProcedure
Procedure ReadItem()
Protected line$
OpenDatabase(0,gDbName$,"","",#PB_Database_SQLite)
Protected Query$,N
Query$="SELECT * FROM CONTACT ORDER BY 'name'"
ClearGadgetItems(DialogGadget(#D_contact_table,"table"))
If DatabaseQuery(0,Query$ )=0
MessageRequester("Erreur Sql",DatabaseError())
ProcedureReturn #False
EndIf
While NextDatabaseRow(0)
line$=GetDatabaseString(0,1)+Chr(10)+GetDatabaseString(0,3)+Chr(10)+GetDatabaseString(0,5)
AddGadgetItem(DialogGadget(#D_contact_table,"table"),-1,line$)
SetGadgetItemData(DialogGadget(#D_contact_table,"table"),N,GetDatabaseLong(0,0))
N+1
Wend
CloseDatabase(0)
EndProcedure
Procedure OpenContactTable()
CreateDialog(#D_contact_table)
If OpenXMLDialog(#D_contact_table,IdXml,"Contact_table")=0
MessageRequester("Dialog creation error: " ,DialogError(#D_contact_table))
ProcedureReturn #False
EndIf
;Ajout des colonnes
Protected IdTable=DialogGadget(#D_contact_table,"table")
AddGadgetColumn(IdTable,0,"Nom",200)
AddGadgetColumn(IdTable,1,"Localité",200)
AddGadgetColumn(IdTable,2,"Code postal",200)
AddGadgetColumn(IdTable,3,"Pays",200)
;Ajout des items à la table
ReadItem()
TestEdit()
ProcedureReturn #True
EndProcedure
Procedure OpenContactSheet()
CreateDialog(#D_contact_sheet)
If OpenXMLDialog(#D_contact_sheet,IdXml,"Contact_Sheet")=0
MessageRequester("Dialog creation error: " ,DialogError(#D_contact_sheet))
ProcedureReturn #False
EndIf
EndProcedure
Procedure AddContact()
;Ouvre la base de donées
OpenDatabase(0,gDbName$,"","",#PB_Database_SQLite)
Protected Query$
Query$="INSERT INTO CONTACT (name,adress,city,zip_code,contry,phone,mail)"
Query$+" VALUES ("
Query$+Chr(34)+GetGadgetText(DialogGadget(#D_contact_sheet,"name"))+Chr(34)+","
Query$+Chr(34)+GetGadgetText(DialogGadget(#D_contact_sheet,"adress"))+Chr(34)+","
Query$+Chr(34)+GetGadgetText(DialogGadget(#D_contact_sheet,"city"))+Chr(34)+","
Query$+Chr(34)+GetGadgetText(DialogGadget(#D_contact_sheet,"zip_code"))+Chr(34)+","
Query$+Chr(34)+GetGadgetText(DialogGadget(#D_contact_sheet,"contry"))+Chr(34)+","
Query$+Chr(34)+GetGadgetText(DialogGadget(#D_contact_sheet,"phone"))+Chr(34)+","
Query$+Chr(34)+GetGadgetText(DialogGadget(#D_contact_sheet,"mail"))+Chr(34)
Query$+")"
If DatabaseUpdate(0,Query$)=0
MessageRequester("Erreur Sql",DatabaseError())
CloseDatabase(0)
ProcedureReturn #False
EndIf
CloseDatabase(0)
ProcedureReturn #True
EndProcedure
Procedure UpdateContact()
;Ouvre la base de donées
OpenDatabase(0,gDbName$,"","",#PB_Database_SQLite)
Protected Query$,line,id
line=GetGadgetState(DialogGadget(#D_contact_table,"table"))
id=GetGadgetItemData(DialogGadget(#D_contact_table,"table"),line)
Query$="UPDATE CONTACT SET "
Query$+"name="+Chr(34)+GetGadgetText(DialogGadget(#D_contact_sheet,"name"))+Chr(34)+","
Query$+"adress="+Chr(34)+GetGadgetText(DialogGadget(#D_contact_sheet,"adress"))+Chr(34)+","
Query$+"city="+Chr(34)+GetGadgetText(DialogGadget(#D_contact_sheet,"city"))+Chr(34)+","
Query$+"zip_code="+Chr(34)+GetGadgetText(DialogGadget(#D_contact_sheet,"zip_code"))+Chr(34)+","
Query$+"contry="+Chr(34)+GetGadgetText(DialogGadget(#D_contact_sheet,"contry"))+Chr(34)+","
Query$+"phone="+Chr(34)+GetGadgetText(DialogGadget(#D_contact_sheet,"phone"))+Chr(34)+","
Query$+"mail="+Chr(34)+GetGadgetText(DialogGadget(#D_contact_sheet,"mail"))+Chr(34)
Query$+" WHERE id="+id
If DatabaseUpdate(0,Query$)=0
MessageRequester("Erreur Sql",DatabaseError())
CloseDatabase(0)
ProcedureReturn #False
EndIf
CloseDatabase(0)
ProcedureReturn #True
EndProcedure
Procedure ReadSheet()
OpenDatabase(0,gDbName$,"","",#PB_Database_SQLite)
Protected Query$,id,line
line=GetGadgetState(DialogGadget(#D_contact_table,"table"))
id=GetGadgetItemData(DialogGadget(#D_contact_table,"table"),line)
Query$="SELECT * FROM CONTACT WHERE id="+id
If DatabaseQuery(0,Query$)=0
MessageRequester("Erreur Sql",DatabaseError())
CloseDatabase(0)
ProcedureReturn #False
EndIf
While NextDatabaseRow(0)
SetGadgetText(DialogGadget(#D_contact_sheet,"name"),GetDatabaseString(0,1))
SetGadgetText(DialogGadget(#D_contact_sheet,"adress"),GetDatabaseString(0,2))
SetGadgetText(DialogGadget(#D_contact_sheet,"city"),GetDatabaseString(0,3))
SetGadgetText(DialogGadget(#D_contact_sheet,"zip_code"),GetDatabaseString(0,4))
SetGadgetText(DialogGadget(#D_contact_sheet,"contry"),GetDatabaseString(0,5))
SetGadgetText(DialogGadget(#D_contact_sheet,"phone"),GetDatabaseString(0,6))
SetGadgetText(DialogGadget(#D_contact_sheet,"mail"),GetDatabaseString(0,7))
Wend
CloseDatabase(0)
EndProcedure
Runtime Procedure Edit()
If GetGadgetState(DialogGadget(#D_contact_table,"table"))=-1
ProcedureReturn #False
EndIf
gModeEdit=#True
DisableWindow(0,#True)
OpenContactSheet()
ReadSheet()
EndProcedure
Runtime Procedure Delete()
If GetGadgetState(DialogGadget(#D_contact_table,"table"))=-1
ProcedureReturn #False
EndIf
If MessageRequester("Contact","Etes-vous sur de vouloir supprimer ce contact?",#PB_MessageRequester_YesNo)=#PB_MessageRequester_No
ProcedureReturn
EndIf
OpenDatabase(0,gDbName$,"","",#PB_Database_SQLite)
Protected Query$,line,id
line=GetGadgetState(DialogGadget(#D_contact_table,"table"))
id=GetGadgetItemData(DialogGadget(#D_contact_table,"table"),line)
Query$="DELETE FROM CONTACT WHERE id="+id
If DatabaseUpdate(0,Query$)=0
MessageRequester("Erreur Sql",DatabaseError())
CloseDatabase(0)
ProcedureReturn #False
EndIf
ReadItem()
ProcedureReturn #True
EndProcedure
Runtime Procedure Exit()
gExit=#True
EndProcedure
Runtime Procedure Submit()
If Len(GetGadgetText(DialogGadget(#D_contact_sheet,"name")))=0
MessageRequester("Contact","Le nom est obligatoire")
SetActiveGadget(DialogGadget(#D_contact_sheet,"name"))
ProcedureReturn #False
EndIf
Select gModeEdit
Case #False
AddContact()
Case #True
UpdateContact()
EndSelect
ReadItem()
PostEvent(#PB_Event_CloseWindow,1,0)
EndProcedure
Runtime Procedure Chancel()
PostEvent(#PB_Event_CloseWindow,1,0)
EndProcedure
Runtime Procedure New()
DisableWindow(0,#True)
OpenContactSheet()
SetActiveGadget(DialogGadget(#D_contact_sheet,"name"))
EndProcedure
Runtime Procedure SelectTable()
TestEdit()
EndProcedure
If Not LoadForm():End :EndIf
MakeDb()
If Not OpenContactTable():End :EndIf
Repeat
gEvent=WaitWindowEvent()
Select EventWindow()
Case 1
If gEvent=#PB_Event_CloseWindow
CloseWindow(1)
DisableWindow(0,#False)
SetActiveWindow(0)
EndIf
Case 0
If gEvent=#PB_Event_CloseWindow
gExit=#True
EndIf
EndSelect
Until gExit=#True
End
Code : Tout sélectionner
<?xml version="1.0"?>
<dialogs>
<window id="0" name="Contact_Table" text="Liste des contacts" minwidth="800" minheight="600" flags="#PB_Window_SizeGadget |
#PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget| #PB_Window_ScreenCentered">
<hbox expand="item:1">
<listicon name="table" width="auto" height="auto" flags="#PB_ListIcon_GridLines|#PB_ListIcon_FullRowSelect" onevent="SelectTable()"/>
<vbox expand="no">
<button name="add" text="Nouveau" width="150" height="30" onevent="New()"/>
<button name="edit" text="Editer" width="150" height="30" onevent="Edit()"/>
<button name="delete" text="Supprimer" width="150" height="30" onevent="Delete()"/>
<button name="exit" text="Sortir" width="150" height="30" onevent="Exit()"/>
</vbox>
</hbox>
</window>
<window id="1" name="Contact_Sheet" text="Ficher d'un contact" minwidth="800" minheight="600" flags="#PB_Window_SizeGadget |
#PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget| #PB_Window_ScreenCentered">
<hbox expand="item:1">
<vbox expand="no">
<text text="Nom"/>
<string name="name" width="auto" maxheight="60"/>
<text text="Adresse"/>
<string name="adress" width="auto" maxheight="60"/>
<text text="Localite"/>
<string name="city" width="auto" maxheight="60"/>
<text text="Code postal"/>
<string name="zip_code" width="auto" maxheight="60"/>
<text text="Pays"/>
<string name="contry" width="auto" maxheight="60"/>
<text text="Telephone"/>
<string name="phone" width="auto" maxheight="60"/>
<text text="Email"/>
<string name="mail" width="auto" maxheight="60"/>
</vbox>
<singlebox width="40"/>
<vbox expand="no">
<button name="submit" text="Valider" width="150" height="30" onevent="Submit()"/>
<button name="chancel" text="Annuler" width="150" height="30" onevent="Chancel()"/>
</vbox>
</hbox>
</window>
</dialogs>