
' Base de données relationnelle Démo Gérald VERDIER 
$include "rapidq.inc"

Type FormatRange       ' DC = Device Context
    hdc As Long       ' DC actuel à recevoir
    hdcTarget As Long ' DC cible pour déterminer le formatage du texte
    rc_left as long   ' zone du DC à imprimer (en twips)
    rc_top as long
    rc_right as long
    rc_bottom as long
    rcPage_left as long ' Zone entière du DC (taille de la page en twips)
    rcPage_top as long
    rcPage_right as long
    rcPage_bottom as long
    chrg_cpMin As Long     ' 1er caractère (0 début de document)
    chrg_cpMax As Long     ' dernier caractère (-1 fin de document)
End Type 

Const WM_USER As Long = &H400
Const EM_FORMATRANGE As Long = WM_USER + 57

Const SWP_HIDEWINDOW = &H80
Const SWP_SHOWWINDOW = &H40
Const GW_CHILD = 5
Const GW_HWNDNEXT = 2
Const GWL_STYLE = -16
Const GWL_HWNDPARENT = -8 ' réduction barre des tâches
Const HWND_DESKTOP = 0 '----------------------------


'==========================
Type SYSTEMTIME 
    wYear As word 
    wMonth As word 
    wDayOfWeek As word
    wDay As word 
    wHour As word
    wMinute As word
    wSecond As word
    wMilliseconds As word
End Type   
'==========================
type enrCde 'commande
    idCde$ as string * 9
    noCde$ as string * 9
    dateCde$ as string * 10
    libelleCde$ as string * 255
    montantCde$ as string * 10
    finCde$ as string * 1
end type    
'========================== 
type enr 'client                      pos
    id$ as string * 9                '0   à 8
    civilite$ as string * 5          '9   à 13
    prenom$ as string * 20           '14  à 33
    nom$ as string * 20              '34  à 53
    adresse$ as string * 50          '54  à 103
    raison_sociale$ as string * 20   '104 à 123
    courriel$ as string * 30         '124 à 153
    telephone$ as string * 14        '154 à 167
    codeP$ as string * 5             '168 à 172
    ville$ as string * 20            '173 à 192
    acces$ as string * 20            '193 à 212
    distance$ as string * 20         '213 à 232
    adresseL$ as string * 50         '233 à 282
    observation$ as string * 255     '283 à 537
    consigne$ as string * 255        '538 à 792
    fin$ as string * 1               '793 à 794
end type

const posId = 0
const posCiv = 9
const posPre = 14
const posNom = 34
const posAdr = 54
const posRsl = 104
const posCou = 124
const posTel = 154
const posCod = 168
const posVil = 173
const posAcc = 193
const posDis = 213
const posAdl = 233
const posObs = 283
const posCsg = 538
const posFin = 793

    '==========================
dim zone$
dim sz as long
dim szCde as long
dim supp as QFileStream
dim L as long
    '==========================    
dim client as enr
dim fichier as QFileStream 
dim mem as QMemorystream : dim memR as QMemorystream
dim rsize as Word 
    rsize = sizeof(client)
dim recno as long
dim nbc as long
dim rech as byte
dim tabClientR1$(16,100) 'tableau de premier stockage recherche (se vide dès que le second est rempli ...)
dim tabClientR2$(16,100) 'tableau de second stockage recherche (se vide au 2ème critère pour remplir le 1er ...)
dim tabClient$(16,100) 'tableau contenant les clients (peut être redim en fonction du nombre de clients ...)
dim tabRClient$(16,100)
dim tabClientR$(100)
'une entète eventuellement ...
    tabClient$(1,0) = "Identifiant"
    tabClient$(2,0) = "Prénom"
    tabClient$(3,0) = "Nom / Société"    
    '==========================
dim commande as enrCde
dim fichiercde as QFileStream
dim cdesize as long 
    Cdesize = sizeof(commande)
dim recnoCde as long, nbr as long
dim nbcde as long
dim tabCde$(5,1000)  'idem que client  
'une entète eventuellement ...
    tabCde$(1,0) = "N°"
    tabCde$(2,0) = "Date"
    tabCde$(3,0) = "Libellé"
    tabCde$(4,0) = "Montant"
    '==========================
dim montantCde as single
dim MyTime As SYSTEMTIME  

dim tabCdeFont as QFont
tabCdeFont.Name = "Courier New"
tabCdeFont.size = 8
tabCdeFont.color = &hff0077


dim richFont as QFont 'font pour le preview et l'impression
richFont.Name = "Courier New" '"arial" 
richFont.AddStyles(fsBold, fsItalic)
richFont.size = 10
DIM Item1 AS QMenuItem : Item1.tag = 51 'commandes
DIM Item2 AS QMenuItem : Item2.tag = 52 'recherche
DIM Item3 AS QMenuItem : Item3.tag = 53 'preview
DIM Item4 AS QMenuItem : Item4.tag = 54 'liste clients
DIM Item5 AS QMenuItem : Item5.tag = 55 'tris clients
    '========================== sous programmes globaux
Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (hWnd1 As Long, hWnd2 As Long, Byref lpsz1 As String, Byref lpsz2 As String) As Long    
declare Function FindWindow Lib "user32" Alias "FindWindowA" (lpClassName As Long, byref lpWindowName As string) As Long    
declare Function GetWindow Lib "user32" Alias "GetWindow" (hwnd As Long, wCmd As Long) As Long
declare Function SetFocus Lib "user32" Alias "SetFocus" (hwnd As Long) As Long    
declare Function SendMessageB Lib "USER32" Alias "SendMessageA" (hWnd As Long, msg As Long, wp As Long, lp As FormatRange) As Long    
declare sub GetLocalTime Lib "kernel32" alias "GetLocalTime" (lpSystemTime As SYSTEMTIME)    
declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (hwnd As Long, nIndex As Long) As Long
declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (hwnd As Long, nIndex As Long, dwNewLong As Long) As Long
declare sub fixButton(btnHandle AS LONG)
declare sub init
declare sub initComboCde
declare sub initGrilleCde
declare sub razGrilleCde
declare sub initListRech
declare sub initStrlist
declare sub finAp
'declare sub listF(sender as QMenuItem)
declare sub ouvF(sender as QMenuItem)
declare sub fermF
declare sub apropos
declare sub navigMenu(sender as QmenuItem)
declare sub infoF
    '========================== sous programmes pour les clients
declare sub initCli    
declare sub particulier
declare sub societe
declare sub adr_livraison
declare sub zone
declare sub tabChange
declare sub bouton (Sender AS QButton)
declare sub NbClients
declare sub nouveau
declare sub lecture(rec as word)
declare sub enregistrer
declare sub modifier
declare sub supprimer
declare sub fin
declare sub rechNoEnr
declare sub rechercheC
declare sub lien_cli_Cde
declare sub validBTN
declare sub validBTN2
declare sub listeCli
declare sub cellcli_Cde(col%,row%)
declare sub export
declare sub etat
declare sub preview
declare sub recherche
declare sub critereCell(Col%,Row%)
declare sub lignePM(Sender as QButton)
declare sub trouvRech
declare sub trouvRech2
declare sub selRech
declare sub affRech(Col%,Row%,value$) 
declare sub trouvCli
declare sub lectureTrouv(rec as word)
declare sub rechPart
    '========================== sous programmes pour les commandes
declare sub commandes
declare sub CdeNom
declare sub nouveauCde
declare sub lectureCde(recCde as word)
declare sub enregistrerCde
declare sub modifierCde
declare sub supprimerCde
declare sub finCommande
declare sub lien_Cde_cli
declare sub menuCde (Sender AS QMenuItem)
declare sub changeCde
declare sub NbCommandes
declare sub totalCde

dim hTab$
dim temp
dim tempList$
dim hDir$
    hdir$ = left$(command$(0), rinstr(command$(0), "\"))
dim selTri as Byte    
dim raz as byte  
'==========================      
declare sub enrList
declare sub ouvList
declare sub inithList
declare sub listCell(Col%,Row%)
declare sub listCombo
declare sub triStrlist
declare sub listeCliTri
declare sub trier
declare sub finTri
'========================== 
    CREATE cliStrList as QStringList
    END CREATE     
' ======================================form tri clients====================================   
CREATE triListForm AS QFORM
    Caption = "Tris clients"
    Width = 842
    Height = 405
    Center    
    color = &hffffef
    onShow = listeCliTri
    onClose = finTri  
    CREATE grille_CliTri AS QSTRINGGRID
        Left = 5
        Top = 30
        Height = 300
        Width = 820
        Color = 16374739
        FixedColor = 8967383
        DefaultRowHeight = 15
        ColCount = 16 '15
        RowCount = 2
        fixedRows = 1
        FixedCols = 0
        separator = ","
        addOptions (goColSizing) 'colonne réglable
        cell(0,0)="id"
        cell(1,0)="-"
        cell(2,0)="Prénom"
        cell(3,0)="Nom"
        cell(4,0)="Adresse"
        cell(5,0)="Société"
        cell(6,0)="Courriel"
        cell(7,0)="Téléphone"
        cell(8,0)="Code Postal"
        cell(9,0)="Ville"
        cell(10,0)="Accès"
        cell(11,0)="Distance"
        cell(12,0)="Livraison"
        cell(13,0)="Observations"
        cell(14,0)="Consignes"   
        cell(15,0)="n°" 
        onSelectCell = listCell
    END CREATE
    CREATE StatusBarAA AS QSTATUSBAR
        AddPanels "Cellule : "
        Panel(0).Alignment = taCenter    
    END CREATE  
    CREATE nbTriPanel as QPanel
        left = 5
        top = 5  
        height = 20
        width = 130
        onClick = listeCliTri
    END CREATE      
    CREATE triCombo AS QComboBox
        top = 5
        left = 450
        width = 100
        text = "Clé de tri"
        onChange = listCombo
    END CREATE    
    CREATE triListBTN as QButton
        left = 600
        top = 5  
        height = 20
        width = 200
        caption  = "Trier"
        onClick = triStrlist
    END CREATE     
END CREATE     
    
' ======================================form commande====================================
CREATE cdeForm AS QFORM
    Caption = "Fichier commandes"
    Width = 338
    Height = 463
    Center
    color = &hffffff
    onClose = finCommande
    create menu as qmainmenu 
        create menuNouveau as qmenuitem 
              caption = "&Nouveau"  
                     onclick = menuCde  
        end create
        create menuEnrCde as qmenuitem
            caption = "&Enregistrer"
                     onclick = menuCde          
        end create
        create menuModifCde as qmenuitem
            caption = "&Modifier"
            enabled = false
                     onclick = menuCde          
        end create  
        create menuEffCde as qmenuitem
            caption = "&Supprimer"
                     onclick = menuCde          
        end create              
    end create     
    CREATE IdCde_lbl AS QLABEL
        Caption = "Id client"
        Left = 29
        Top = 86
        Width = 56
        Transparent = 1
    END CREATE
    CREATE PrenomCde_lbl AS QLABEL
        Caption = "Prénom client"
        Left = 28
        Top = 130
        Width = 112
        Transparent = 1
    END CREATE
    CREATE NomCde_lbl AS QLABEL
        Caption = "Nom client"
        Left = 187
        Top = 130
        Width = 88
        Transparent = 1
    END CREATE
    CREATE NoCde_lbl AS QLABEL
        Caption = "N° commande"
        Left = 28
        Top = 205
        Width = 96
        Transparent = 1
    END CREATE
    CREATE DateCde_lbl AS QLABEL
        Caption = "Date commande"
        Left = 182
        Top = 205
        Width = 112
        Transparent = 1
    END CREATE
    CREATE Mont_lbl AS QLABEL
        Caption = "Montant"
        Left = 35
        Top = 376
        Width = 64
        Transparent = 1
    END CREATE
    CREATE Cde_lbl AS QLABEL
        Caption = "Fichier commandes"
        Left = 10
        Top = 8
        Width = 112
        Transparent = 1
    END CREATE
    CREATE Combo_id AS QCOMBOBOX
        Left = 30
        Top = 30
        Width = 275
        TabOrder = 1
        onChange = CdeNom
    END CREATE    
    create Panel_nbCommandes as QPanel
        top = 66
        left = 110
        width = 194
        height = 20
    end create
    CREATE EditCde_Id AS QEDIT
        Text = ""
        Left = 30
        Top = 66
        width = 50
        Enabled = 0
    END CREATE    
    CREATE EditCde_Prenom AS QEDIT
        Text = ""
        Left = 30
        Top = 106
        Enabled = 0
    END CREATE
    CREATE EditCde_Nom AS QEDIT
        Text = ""
        Left = 184
        Top = 107
        Enabled = 0
    END CREATE
    CREATE editCde_No AS QEDIT
        Text = ""
        Left = 30
        Top = 185
        TabOrder = 2
    END CREATE
    CREATE EditCde_date AS QEDIT
        Text = ""
        Left = 182
        Top = 184
        TabOrder = 3
    END CREATE
    CREATE RichCde_libelle AS QRICHEDIT
        Left = 30
        Top = 234
        Width = 281
        TabOrder = 4
    END CREATE
    CREATE editCde_montant  AS QEDIT
        Text = ""
        Left = 31
        Top = 352
        Width = 81
        TabOrder = 5
    END CREATE   
    create slide_Cde as QScrollBar
        left = 212
        top = 352
        width = 100
        height = 20
        onChange = changeCde
    end create
    create slide_Lbl as QLabel
        left = 120
        top = 355
        caption = "-"
    end create
END CREATE
' ======================================form aperçu et impression====================================
create apForm as QForm
    Caption = "Aperçu avant impression"   
    Width = 800
    height = 600
    center
    color = &hffffff
    onClose = finAp
    create menuAp as qmainmenu 
        create menuImp as qmenuitem 
              caption = "&Imprimer"  
              onclick = etat 
        end create 
    end create
    create richEditA as QRichEdit
        top = 10
        left = 10
        height = 520
        width = 780
        ScrollBars = ssBoth
        PlainText = True
        WordWrap = False
        wantTabs = true
        font = richFont
        readOnly = true
     end create      
end create 
' ======================================form recherche clients====================================   
create rechForm as QForm
    caption = "Critère de recherche pour les clients"
    Width = 842
    Height = 360  
    color = &hffffef
    onClose = validBTN2
    create trouverBTN as QButton
        left = 25
        width = 780
        top = 5
        caption = "Trouver"
        onClick = selRech 'trouvRech2
    end create
    create LignePBTN as QButton
        left = 5
        width = 20
        top = 5
        caption = "v"  
        enabled = false 'en construction
        onclick = lignePM
    end create
    create LigneMBTN as QButton
        left = 806
        width = 20
        top = 5
        caption = "^"  
        enabled = false 'en construction
        onclick = lignePM
    end create    
    CREATE grilleR_Cli AS QSTRINGGRID
        Left = 5
        Top = 35
        Height = 75
        Width = 820
        Color = 16374739
        FixedColor = 8967383
        DefaultRowHeight = 15
        ColCount = 15
        RowCount = 2
        fixedRows = 1
        FixedCols = 0
        separator = ","
        onSelectCell = critereCell
        OnSetEditText = affRech
        colWidths(0)=40
        colWidths(1)=20
        colWidths(2)=50
        colWidths(3)=50
        colWidths(4)=100
        colWidths(5)=50
        colWidths(6)=120
        colWidths(7)=70
        colWidths(8)=30
        colWidths(9)=50
        colWidths(10)=50
        colWidths(11)=50
        colWidths(12)=100
        colWidths(13)=150
        colWidths(14)=150
        cell(0,0)="id"
        cell(1,0)="-"
        cell(2,0)="Prénom"
        cell(3,0)="Nom"
        cell(4,0)="Adresse"
        cell(5,0)="Société"
        cell(6,0)="Courriel"
        cell(7,0)="Téléphone"
        cell(8,0)="Code Postal"
        cell(9,0)="Ville"
        cell(10,0)="Accès"
        cell(11,0)="Distance"
        cell(12,0)="Livraison"
        cell(13,0)="Observations"
        cell(14,0)="Consignes"   
        AddOptions(goEditing,goAlwaysShowEditor,goDrawFocusSelected,goTabs,goColSizing) 
   end create
   create ou_etRechPanel as QPanel
       top = 114
       left = 640
       width = 80
       height = 23
       color = 16374739
       visible = 0
       create ouRad as QRadioButton
           caption = "ou"
           left = 5
           top = 5
           width = 40
       end create
       create etRad as QRadioButton
           caption = "et"
           left = 50
           top = 5
           width = 30
           checked = true
       end create       
    end create           
   create plusRechBTN as QButton
       caption = "Recherche spécifique"
       left = 5
       top = 115
       width = 150
       onClick = rechPart
   end create       
   create rechCliEdit as QEdit
       top = 115
       left = 200
       width = 421
       'readOnly = true
       enabled = false
       color = 16374739 '&hb4f9ff
   end create
   create aideLbl as QLabel
       left = 5
       top = 150
       width = 500
       caption = "Pour rechercher par champs, cliquer sur une cellule et choisir dans la liste déroulante. _
                  Cliquer sur le bouton pour une recherche spécifique ou différente en respectant la casse."
   end create
end create
' ======================================form liste clients====================================   
CREATE cliListForm AS QFORM
    Caption = "Liste clients"
    Width = 842
    Height = 405
    'Center    
    color = &hffffef
    onClose = validBtn                  
    CREATE grille_Cli AS QSTRINGGRID
        Left = 5
        Top = 30
        Height = 300
        Width = 820
        Color = 16374739
        FixedColor = 8967383
        DefaultRowHeight = 15
        ColCount = 16 '15
        RowCount = 2
        fixedRows = 1
        FixedCols = 0
        separator = ","
        addOptions (goColSizing) 'colonne réglable
        onSelectCell = cellcli_Cde 
        colWidths(0)=40
        colWidths(1)=20
        colWidths(2)=50
        colWidths(3)=50
        colWidths(4)=100
        colWidths(5)=50
        colWidths(6)=120
        colWidths(7)=70
        colWidths(8)=30
        colWidths(9)=50
        colWidths(10)=50
        colWidths(11)=50
        colWidths(12)=100
        colWidths(13)=150
        colWidths(14)=150
        colWidths(15)=20
        cell(0,0)="id"
        cell(1,0)="-"
        cell(2,0)="Prénom"
        cell(3,0)="Nom"
        cell(4,0)="Adresse"
        cell(5,0)="Société"
        cell(6,0)="Courriel"
        cell(7,0)="Téléphone"
        cell(8,0)="Code Postal"
        cell(9,0)="Ville"
        cell(10,0)="Accès"
        cell(11,0)="Distance"
        cell(12,0)="Livraison"
        cell(13,0)="Observations"
        cell(14,0)="Consignes"   
        cell(15,0)="n°" 
    END CREATE
    CREATE StatusBarA AS QSTATUSBAR
        AddPanels "Cellule : "
        Panel(0).Alignment = taCenter    
    END CREATE  
    CREATE nbCliPanel as QPanel
        left = 5
        top = 5  
        height = 20
        width = 300
    END CREATE       
END CREATE

' ======================================form client====================================
CREATE cliForm AS QFORM
    Caption = "Fichier client"
    Width = 842
    Height = 630
    Center
    color = &hffffef
    onShow = init
    onClose = fin
    create menuP as qmainmenu 
        create menufile as qmenuitem 
              caption = "&Fiche" 
              create menuNouv as qmenuitem 
                     caption = "&Nouvelle fiche" 
                     onclick = nouveau 
              end create
              create lineAMenu AS QMENUITEM
            	    Caption = "-"
              end create              
              create menuEnr as qmenuitem 
                     caption = "Enregistrer la fiche" 
                     onclick = enregistrer                    
              end create
              create menuModif as qmenuitem 
                     caption = "Modifier la fiche" 
                     onclick = modifier                     
              end create   
              create menuSup as qmenuitem 
                     caption = "Supprimer la fiche" 
                     onclick = supprimer                     
              end create               
              create menuExp as qmenuitem 
                     caption = "Exporter" 
                     create menuExpCli as qmenuitem 
                         caption = "Les fiches clients" 
                         onclick = export
                     end create
                     create menuExpCde as qmenuitem 
                         caption = "Les fiches commandes" 
                         'onclick = exportCde
                     end create                     
              end create      
              create lineBMenu AS QMENUITEM
            	    Caption = "-"
              end create         
              create menuEta as qmenuitem 
                     caption = "Etats" 
                     onclick = preview
              end create       
              create line1bMenu AS QMENUITEM
            	    Caption = "-"
              end create                        
              create menuImprim as qmenuitem 
                     caption = "Imprimer La page écran" 
                     'onclick = imprimF
              end create
              create menuInfo as qmenuitem 
                     caption = "&Info fiche" 
                     onclick = InfoF 
              end create               
              create lineCMenu AS QMENUITEM
            	    Caption = "-"
              end create                                                                               
              create menuexit as qmenuitem
                     caption = "Quitter"
                     onclick = fin
              end create               
        end create
        create menuFNavig as qmenuitem 
              caption = "Navigation"    
              create menuVoirList as qmenuitem 
                     caption = "Liste des fiches" 
                     onclick = navigMenu 
              end create    
              create line1Menu AS QMENUITEM
            	    Caption = "-"
              end create   
              create menuF1 as qmenuitem 
                     caption = "Première fiche" 
                     onclick = navigMenu
              end create 
              create menuDF as qmenuitem 
                     caption = "Dernière fiche" 
                     onclick = navigMenu
              end create       
              create menuFP as qmenuitem 
                     caption = "Fiche précédente" 
                     onclick = navigMenu
              end create   
              create menuFS as qmenuitem 
                     caption = "Fiche suivante" 
                     onclick = navigMenu
              end create                                                        
              create line2bMenu AS QMENUITEM
            	    Caption = "-"
              end create                                                             
              create menuAller as qmenuitem 
                     caption = "Trouver une fiche" 
                     onclick = navigMenu
              end create                                                                                                                                                     
        end create 
        create menuFenetre as qmenuitem 
              caption = "Fenetres" 
              create menuFF as qmenuitem 
                     caption = "Fermer les fenetres" 
                     enabled = false
                     onclick = fermF
              end create               
              create menuSousF as qmenuitem 
                     caption = "-" 
                     'onclick = Aide
              end create                                        
        end create                  
        create menuInfoEdit as qmenuitem 
              caption = "Aide" 
              create menuAide as qmenuitem 
                     caption = "Contenu" 
                     'onclick = Aide
              end create                  
              create menuApropos as qmenuitem 
                     caption = "A propos ..." 
                     onclick =  Apropos
              end create                       
        end create                    
    end create     
    CREATE Gauge AS QGAUGE
        Left = 65
        Top = 515
        Width = 204
        Height = 20
        Kind = 1
        BackColor = 16374739
        Color = 8967383
    END CREATE
    CREATE MonetaireLBL AS QLABEL
        Caption = "en €"
        Left = 366
        Top = 520
        Transparent = 1
    END CREATE
    CREATE Image AS QIMAGE
        Transparent = 1
        BMP = hDir$ + "\BUZZBUG.BMP" 'changer le chemin
        Left = 15
        Top = 8
        Width = 33
        Height = 33
    END CREATE
    CREATE clientGB AS QGROUPBOX
        Caption = "Identification client "
        Left = 65
        Top = 52
        Width = 753
        Height = 121
        Color = 16374739
        CREATE idLBL AS QLABEL
            Caption = "Identifiant"
            Left = 100 
            Top = 50
            Color = 7702726
            Transparent = 1
        END CREATE        
        CREATE nomLBL AS QLABEL
            Caption = "Nom"
            Left = 355 
            Top = 50
            Color = 7702726
            Transparent = 1
        END CREATE
        CREATE prénomLBL AS QLABEL
            Caption = "Prénom"
            Left = 228 
            Top = 50
            Width = 40
            Color = 7702726
            Transparent = 1
        END CREATE
        CREATE adresseLBL AS QLABEL
            Caption = "Adresse"
            Left = 483
            Top = 50
            Width = 48
            Color = 7702726
            Transparent = 1
        END CREATE
        CREATE codePLBL AS QLABEL
            Caption = "Code postal"
            Left = 555
            Top = 95
            Width = 56
            Color = 7702726
            Transparent = 1
        END CREATE
        CREATE villeLBL AS QLABEL
            Caption = "Ville"
            Left = 618
            Top = 95
            Color = 7702726
            Transparent = 1
        END CREATE
        CREATE courrielLBL AS QLABEL
            Caption = "Courriel"
            Left = 232
            Top = 95
            Width = 40
            Color = 7702726
            Transparent = 1
        END CREATE
        CREATE societeLBL AS QLABEL
            Caption = "Raison sociale"
            Left = 100
            Top = 95
            Width = 70
            Color = 16374739
            Transparent = 1
        END CREATE           
        CREATE telephoneLBL AS QLABEL
            Caption = "Téléphone"
            Left = 406
            Top = 95
            Width = 54
            Color = 16374739
        END CREATE    
        CREATE panel_id AS QPanel
            Left = 100
            Top = 30
            width = 50
            height = 20
            BevelInner  =  bvLowered
            color = &hffffff
            showHint = true
            Hint = "Identifiant du client"
        END CREATE          
        CREATE Edit_prenom AS QEDIT
            Text = ""
            Left = 228 
            Top = 30
            showHint = true
            Hint = "Prénom du client"
            TabOrder = 0
        END CREATE
        CREATE Edit_nom AS QEDIT
            Text = ""
            Left = 355 
            Top = 30
            showHint = true
            Hint = "Nom du client"
            TabOrder = 1
        END CREATE
        CREATE Edit_adresse AS QEDIT
            Text = ""
            Left = 483
            Top = 30
            Width = 257
            Hint = "Adresse habituelle"
            TabOrder = 2
        END CREATE
        CREATE Edit_codeP AS QEDIT
            Text = ""
            Left = 555
            Top = 73
            Width = 57
            showHint = true
            Hint = "Code postal à 5 chiffres"
            TabOrder = 5
        END CREATE
        CREATE Edit_ville AS QEDIT
            Text = ""
            Left = 618
            Top = 73
            showHint = true
            Hint = "Localité"
            TabOrder = 6
        END CREATE
        CREATE particulierRB AS QRADIOBUTTON
            Caption = "Particulier"
            Left = 24
            Top = 32
            Width = 74
            TabOrder = 7
            onClick = particulier
        END CREATE
        CREATE societeRB AS QRADIOBUTTON
            Caption = "Société"
            Left = 24
            Top = 74
            Width = 66
            TabOrder = 8
            onClick = societe
        END CREATE
        CREATE Edit_courriel AS QEDIT
            Text = ""
            Left = 228
            Top = 73
            Width = 169
            Hint = "Adresse internet"
            TabOrder = 3
        END CREATE
        CREATE cb_civilite AS QCOMBOBOX
            Text = "Mr"
            Left = 157
            Top = 30
            Width = 65
            Enabled = 0
            showHint = true
            Hint = "Civilité"
            TabOrder = 9
            addItems "Mr","Mme","Melle"
        END CREATE
        CREATE Edit_societe AS QEDIT
            Text = ""
            Left = 100
            Top = 74
            Enabled = 0
            showHint = true
            Hint = "Nom de la société"
            TabOrder = 10
        END CREATE
        CREATE Edit_telephone AS QEDIT
            Text = ""
            Left = 407
            Top = 73
            showHint = true
            Hint = "Téléphone habituel"
            TabOrder = 4
        END CREATE
    END CREATE
    CREATE accesGB AS QGROUPBOX
        Caption = "Accès"
        Left = 65
        Top = 181
        Width = 753
        Color = 16374739
        create Cde_BTN as QButton
            caption = "Fichier commandes"
            left = 20
            top = 70
            width = 145
            height = 20
            color = &hff00ff
            onclick = commandes
        end create
        CREATE typeLBL AS QLABEL
            Caption = "Type"
            Left = 20
            Top = 20
            Height = 14
            Color = 16374739
        END CREATE
        CREATE distanceLBL AS QLABEL
            Caption = "Distance"
            Left = 191
            Top = 20
            Width = 46
            Color = 16374739
            Transparent = 1
        END CREATE
        CREATE cb_acces AS QCOMBOBOX
            Text = "Accès direct"
            Left = 20
            Top = 38
            TabOrder = 1
            showHint = true
            hint = "Type d'accès"
            addItems "Accès direct","Accès difficile","Accès dépot","Accès contrôlé"
        END CREATE
        CREATE cb_distance AS QCOMBOBOX
            Text = "- 1km"
            Left = 190
            Top = 38
            TabOrder = 2
            showHint = true
            hint = "Zone de distance"
            addItems "- 1km","1 à 5km","5 à 25km","+ 25km"
        END CREATE
        CREATE Check_livraison AS QCHECKBOX
            Caption = " Livraison       -       Adresse"
            Left = 400
            Top = 32
            Width = 202
            onClick = adr_livraison
        END CREATE
        CREATE Edit_adresseL AS QEDIT
            Text = ""
            Left = 367
            Top = 65
            Width = 233
            Enabled = 0
            showHint = true
            Hint = "Adresse de livraison"
        END CREATE
        CREATE lb_zone AS QLISTBOX
            Left = 621
            Top = 16
            Height = 81
            addItems "Zone urbaine","Zone industrielle","Zone rurale","Zone militaire","Zone protégée","Autre zone"
            onClick = zone
        END CREATE
    END CREATE
    CREATE StatusBar AS QSTATUSBAR
        AddPanels "fichier : "
        Panel(0).Alignment = taCenter    
    END CREATE
    CREATE grille_Cde AS QSTRINGGRID
        Left = 65
        Top = 320
        Height = 184
        Width = 525
        font = tabCdeFont
        Color = 16374739
        FixedColor = 8967383
        DefaultRowHeight = 19
        TabOrder = 10
        ColCount = 4
        RowCount = 9
        FixedCols = 0
        colWidths(0)=60
        colWidths(1)=60
        colWidths(2)=305
        colWidths(3)=90
        cell(0,0)="N°"
        cell(1,0)="Date"
        cell(2,0)="Libellé"
        cell(3,0)="Montant en €"
    END CREATE
    CREATE tab_Edit as QTABCONTROL
        Left = 610
        Top = 290
        Width = 205
        Height = 140
        AddTabs " - Observations -","- Consignes -"
        Color = 8967383
        OnChange = TabChange
        CREATE Rich_observations AS QRICHEDIT
            Left = 5
            Top = 25
            Width = 195
            Height = 110
            ScrollBars = 2
            TabOrder = 12
            visible = 1
            color = &hc4e4ff
        END CREATE
        CREATE Rich_consignes AS QRICHEDIT
            Left = 5
            Top = 25
            Width = 195
            Height = 110
            ScrollBars = 2
            TabOrder = 12
            visible = 0    
            color = &hefefbf        
        END CREATE
    END CREATE
    CREATE Panel_CdeT AS QPANEL
        Alignment = 0
        Left = 404
        Top = 512
        Caption = "Total commandes : "
        Height = 25
        Color = 8967383
    END CREATE
    CREATE enrBTN AS QBUTTON
        Caption = "Enregistrer"
        Left = 610
        Top = 512
        Width = 59
        Color = clRed
        onClick = bouton
    END CREATE
    CREATE modifBTN AS QBUTTON
        Caption = "Modifier"
        Left = 610
        Top = 512
        Width = 59
        Color = clBlue
        onClick = bouton
        visible = false
    END CREATE    
    CREATE effBTN AS QBUTTON
        Caption = "Effacer"
        Left = 672
        Top = 512
        Width = 39
        Color = &h008cff
        onClick = bouton
    END CREATE    
    CREATE nouvBTN AS QBUTTON
        Caption = "Nouveau"
        Left = 715
        Top = 512
        Width = 50
        Color = &hffff00
        onClick = bouton
    END CREATE      
    CREATE quitterBTN AS QBUTTON
        Caption = "Quitter"
        Left = 775
        Top = 512
        width = 40
        Color = clYellow
        onClick = bouton
    END CREATE
    CREATE aideBTN AS QBUTTON
        Caption = "Aide"
        Left = 775
        Width = 40
        Top = 472
        onClick = bouton
    END CREATE     
    CREATE debutBTN AS QBUTTON
        Caption = "|<"
        Left = 610
        Top = 472
        Width = 25
        Color = &hff00ff
        onClick = bouton
    END CREATE
    CREATE avantBTN AS QBUTTON
        Caption = "<"
        Left = 640
        Top = 472
        Width = 25
        Color = &hff00ff
        onClick = bouton
    END CREATE
    CREATE NoBTN AS QBUTTON
        Caption = "n°"
        Left = 739
        Top = 472
        Width = 25
        Color = &h0045ff
        onClick = bouton
    END CREATE
    CREATE apresBTN AS QBUTTON
        Caption = ">"
        Left = 671
        Top = 472
        Width = 25
        Color = &h00ff00
        onClick = bouton
    END CREATE
    CREATE finBTN AS QBUTTON
        Caption = ">|"
        Left = 702
        Top = 472
        Width = 25
        Color = &h00ff00
        onClick = bouton
    END CREATE   
    CREATE Panel_nbClients AS QPANEL
        Alignment = 0
        Left = 611
        Top = 438
        Width = 205
        Height = 25
        Color = 8967383
    END CREATE
    create edit_no as QEdit
        top = 440 '5
        width = 50
        left = 740 '5
        Color = 16374739
        visible = false
    end create
    create NoEnrBTN as QButton
        caption = "Ok"
        top = 441 '5
        width = 20
        height = 20
        left = 793 '60
        color = &h0000ff
        onClick = rechNoEnr
        visible = false
    end create    
    CREATE Panel_saisie AS QPANEL
        Left = 116
        Top = 8
        Caption = "Bzzz THE BUG - Saisie des clients"
        Width = 700
        Height = 33
        Color = 8967383
    END CREATE
    CREATE Panel_Fiche AS QPANEL
        left = 65
        height = 33
        top = 8
        width = 50 
        Color = 8967383
    END CREATE     
    CREATE Panel_commandes AS QPANEL
        Caption = "Commandes associées"
        Left = 65
        Top = 290
        Width = 525
        Height = 25
        Color = 8967383
    END CREATE
    CREATE etatsBTN AS QBUTTON
        Caption = "Etats"
        Left = 5
        Top = 50
        Width = 50
        Height = 80
        color = &h808080
        onClick = bouton
    END CREATE
    CREATE listesBTN AS QBUTTON
        Caption = "Listes"
        Left = 5
        Top = 150
        Width = 50
        Height = 80
        color = &h808080
        onClick = bouton
    END CREATE
    CREATE triTabBTN AS QBUTTON
        Caption = "Tris"
        Left = 5
        Top = 250
        Width = 50
        Height = 80
        color = &h808080
        onClick = bouton
    END CREATE    
    CREATE trouvBTN AS QBUTTON
        Caption = "Trouver"
        Left = 5
        Top = 350
        Width = 50
        Height = 80
        color = &h808080
        onClick = bouton
    END CREATE
    CREATE exportBTN AS QBUTTON
        Caption = "Export"
        Left = 5
        Top = 450
        Width = 50
        Height = 80
        color = &h808080
        onClick = bouton
    END CREATE
END CREATE

for i = 0 to 14
    grilleR_Cli.ColumnStyle(i) = gcsList
next i
'XP Button
fixButton(cde_BTN.Handle)
fixButton(enrBTN.Handle)
fixButton(modifBTN.Handle)
fixButton(effBTN.Handle)
fixButton(nouvBTN.Handle)
fixButton(quitterBTN.Handle)
fixButton(aideBTN.Handle)
fixButton(debutBTN.Handle)
fixButton(avantBTN.Handle)
fixButton(noBTN.Handle)
fixButton(apresBTN.Handle)
fixButton(finBTN.Handle)
fixButton(noEnrBTN.Handle)
fixButton(etatsBTN.Handle)
fixButton(listesBTN.Handle)
fixButton(trouvBTN.Handle)
fixButton(exportBTN.Handle)
fixButton(ligneMBTN.Handle)
fixButton(lignePBTN.Handle)
fixButton(trouverBTN.Handle)
fixButton(plusRechBTN.handle)
fixButton(triTabBTN.handle)
fixButton(triListBTN.handle)
'fixButton(plusRechBTN.handle)

'minimize to taskbar
'SetWindowLong(cliForm.Handle, GWL_HWNDPARENT, HWND_DESKTOP)
'SetWindowLong(Application.Handle, GWL_HWNDPARENT, cliForm.Handle)

' ======================================SUBs et Fonctions====================================

'======================================SUBs d'initialisation============================
SUB fixButton(btnHandle AS LONG)
	SetWindowLong(btnHandle, GWL_STYLE, GetWindowLong(btnHandle, GWL_STYLE) XOR 11)
END SUB

sub init 'création ou lecture fichier
    IF FileExists(hDir$ + "\client.bdr") = false THEN 
        fichier.open("client.bdr",fmCreate)
        fichier.close
    end if
    fichier.open(hDir$ + "\client.bdr",fmOpenReadWrite)    
    
    IF FileExists(hDir$ + "\commande.bdr") = false THEN 
        fichierCde.open(hDir$ + "\commande.bdr",fmCreate)
        fichierCde.close
    end if     
    fichierCde.open(hDir$ + "\commande.bdr",fmOpenReadWrite)   
    initGrilleCde
    initCli 
    'initListRech
    nouveau : NbClients : lecture(0)
    nouveauCde : NbCommandes : lectureCde(0)
    lien_cli_Cde : totalCde
    'showmessage str$(nbc)
    for i = 1 to nbc
        combo_id.addItems = ""
    next i
        
end sub
'==========================
sub initGrilleCde 'tabCde$
szCde = fichierCde.size
if szCde <> 0 then
    fichierCde.position = 0
        mem.position = 0
    mem.copyFrom(fichierCde,szCde)
    mem.position = 0  
    for LL = 0 to (szCde /CdeSize)-1
        mem.readUDT(commande)
        tabCde$(0,LL+1)=commande.idCde$ 
        tabCde$(1,LL+1)=commande.noCde$ 
        tabCde$(2,LL+1)=commande.dateCde$
        tabCde$(3,LL+1)=commande.libelleCde$
        tabCde$(4,LL+1)=commande.montantCde$
        montantCde = montantCde + val(rtrim$(tabCde$(4,l+1)))
    next LL
else
    exit sub
end if
    mem.close
end sub    
'==========================
sub initcli 'tabclient$
    inithList
sz = fichier.size
if sz <> 0 then
    fichier.position = 0
    mem.position = 0
    mem.copyFrom(fichier,sz)
    mem.position = 0    
    for L = 0 to (sz /794)-1
        mem.readUDT(client)
        'dans tableau
        tabClient$(1,l+1) = client.id$ 
        tabClient$(2,l+1) = client.civilite$ 
        tabClient$(3,l+1) = client.prenom$
        tabClient$(4,l+1) = client.nom$
        tabClient$(5,l+1) = client.adresse$
        tabClient$(6,l+1) = client.raison_sociale$
        tabClient$(7,l+1) = client.courriel$
        tabClient$(8,l+1) = client.telephone$
        tabClient$(9,l+1) = client.codeP$
        tabClient$(10,l+1) = client.ville$
        tabClient$(11,l+1) = client.acces$
        tabClient$(12,l+1) = client.distance$
        tabClient$(13,l+1) = client.adresseL$
        tabClient$(14,l+1) = client.observation$
        tabClient$(15,l+1) = client.consigne$
    next L    
else
    exit sub
end if
    mem.close 

    'initComboCde   
end sub   
'==========================
sub initComboCde 'remplissage du comboBox de la fiche commande avec les clients pour choisir à la saisie 
    initcli 
        combo_id.text = tabClient$(1,0)+" - "+tabClient$(2,0)+" - "+tabClient$(3,0)+" clients = "+str$(l) 
        combo_id.Item(0) = tabClient$(1,0)+" - "+tabClient$(2,0)+" - "+tabClient$(3,0)+" clients = "+str$(l)
    for i = 1 to L   
        combo_id.Item(i) = rtrim$(tabClient$(1,i))+" - "+rtrim$(tabClient$(3,i))+" "+rtrim$(tabClient$(4,i))+" - "+rtrim$(tabClient$(6,i))
    next i      
end sub      
'==========================
sub initListRech
    for i = 1 to L
      'grilleR_Cli.ColumnStyle(i-1) = gcsList
        listId$ = rtrim$(listId$+tabClient$(1,i))+chr$(13)
        listPrenom$ = rtrim$(listPrenom$+tabClient$(3,i))+chr$(13) 
        listNom$ = rtrim$(listNom$+tabClient$(4,i))+chr$(13)
        listAdresse$ = rtrim$(listAdresse$+tabClient$(5,i))+chr$(13)
        listRS$ = rtrim$(listRS$+tabClient$(6,i))+chr$(13)
        listCourriel$ = rtrim$(listCourriel$+tabClient$(7,i))+chr$(13)
        listTe$ = rtrim$(listTe$+tabClient$(8,i))+chr$(13)
        listCodeP$ = rtrim$(listCodeP$+tabClient$(9,i))+chr$(13)
        listVille$ = rtrim$(listVille$+tabClient$(10,i))+chr$(13)
        listAcces$ = rtrim$(listAcces$+tabClient$(11,i))+chr$(13)       
        listLiv$ = rtrim$(listLiv$+tabClient$(13,i))+chr$(13)
        listObs$ = rtrim$(listObs$+tabClient$(14,i))+chr$(13)
        listConsig$ = rtrim$(listConsig$+tabClient$(15,i))+chr$(13) 
   next i
   listCiv$ = "Mr"+chr$(13)+"Mme"+chr$(13)+"Melle"
   listDist$ = "- 1km"+chr$(13)+"1 à 5km"+chr$(13)+"5 à 25km"+chr$(13)+"+ 25km"
    grilleR_Cli.ColumnList(0) = listId$
    grilleR_Cli.ColumnList(1) = listCiv$
    grilleR_Cli.ColumnList(2) = listPrenom$
    grilleR_Cli.ColumnList(3) = listNom$
    grilleR_Cli.ColumnList(4) = listAdresse$
    grilleR_Cli.ColumnList(5) = listRS$
    grilleR_Cli.ColumnList(6) = listCourriel$
    grilleR_Cli.ColumnList(7) = listTe$
    grilleR_Cli.ColumnList(8) = listCodeP$
    grilleR_Cli.ColumnList(9) = listVille$
    grilleR_Cli.ColumnList(10) = listAcces$
    grilleR_Cli.ColumnList(11) = listDist$
    grilleR_Cli.ColumnList(12) = listLiv$
    grilleR_Cli.ColumnList(13) = listObs$
    grilleR_Cli.ColumnList(14) = listConsig$
end sub
'==========================
sub razGrilleCde 'effacement du tableau de commandes dans la fiche client
    for co = 0 to grille_Cde.colcount
        for ro = 1 to grille_Cde.rowcount
            grille_Cde.cell(co,ro) = ""
        next ro
    next co
end sub
'==========================
sub fin
    fichier.close
    application.terminate
end sub    
'======================================SUBs de mise en forme client============================

sub particulier 'choix entre particulier ou société
    cb_civilite.enabled = true
    Edit_societe.enabled = false
end sub
'==========================
sub societe 'choix entre particulier ou société
    cb_civilite.enabled = false
    Edit_societe.enabled = true
end sub
'==========================
sub adr_livraison 'cochage livraison
    if Check_livraison.checked = true then
        Edit_adresseL.enabled = true
    else
        Edit_adresseL.enabled = false
    end if
end sub
'==========================
sub zone
    zone$ = lb_zone.item(lb_zone.itemIndex)
    Rich_observations.text = zone$
end sub
'==========================
sub tabChange 'visualisation onglets
    select case Tab_Edit.tabindex
        case 0 
        Rich_observations.visible = true
        Rich_consignes.visible = false
        case 1
        Rich_observations.visible = false
        Rich_consignes.visible = true      
    end select
end sub
'======================================SUBs de navigation et tâches client============================
sub navigMenu(sender as QmenuItem)
    select case sender.caption
        case "Première fiche"
            lecture(0)
        case "Dernière fiche"
            lecture(fichier.size\rsize-1)
        case "Fiche précédente"
            if recno = 0 then 'pas de débordement ou de faux affichages en dehors des données
                lecture(recno)
            else     
                lecture(recno-1) 
            end if                       
        case "Fiche suivante"
            if recno = nbc-1 then 'pas de débordement ou de faux affichages en dehors des données
                lecture(recno)
            else     
                lecture(recno+1) 
            end if         
        case "Trouver une fiche"
            rechercheC
        case "Liste des fiches"
            listeCli
    end select   
end sub
'==========================         
SUB bouton (Sender AS QButton) 'clic bouton et appel des subs selon le caption du bouton
    select case sender.caption
        case "|<"  
            lecture(0)
        case "<"       
            if recno = 0 then 'pas de débordement ou de faux affichages en dehors des données
                lecture(recno)
            else     
                lecture(recno-1) 
            end if          
        case "n°" 
            rechercheC        
        case ">"  
            if recno = nbc-1 then 'pas de débordement ou de faux affichages en dehors des données
                lecture(recno)
            else     
                lecture(recno+1) 
            end if         
        case ">|" 
            lecture(fichier.size\rsize-1)
        case "Enregistrer"
            enregistrer
        case "Nouveau"
            nouveau
        case "Quitter"
            fin
        case "Modifier"
            modifier
        case "Effacer"
            supprimer
        case "Tris"
            trier            
        case "Listes"
            listeCli
        case "Export"
            export
        case "Etats"
            preview 'etat
        case "Trouver"
            recherche
            
    end select
END SUB
'======================================SUBs de base client============================

sub lien_cli_Cde 'lien relationnel client / commandes 1 pour plusieurs
    r=1
    for i = 0 to nbcde
        if panel_id.caption = tabCde$(0,i) then
            grille_Cde.cell(0,r) = tabCde$(1,i)
            grille_Cde.cell(1,r) = tabCde$(2,i)
            grille_Cde.cell(2,r) = tabCde$(3,i)
            'grille_Cde.cell(3,r) = tabCde$(4,i)
            mtcde$ = Format$("%2.2f", val(tabCde$(4,i))) 'à droite
            if val(tabCde$(4,i))>0 then
                v = 4
            elseif val(tabCde$(4,i))>9 then
                v = 3
            elseif val(tabCde$(4,i))>99 then
                v = 2
            elseif val(tabCde$(4,i))>999 then
                v = 1
            else
                v = 0    
            end if
            m = len(mtcde$)+v
            ms$=string$(12-m," ")
            'grille_Cde.cell(3,r) = ms$+Format$("%2.2m", val(tabCde$(4,i)))
            grille_Cde.cell(3,r) = ms$+mtcde$ 'Format$("%2.2f", val(tabCde$(4,i)))
            r=r+1
        end if    
    next i        
end sub    
'==========================
sub nouveau 'nouvelle fiche
    cb_civilite.enabled = false
    Edit_societe.enabled = false
    cb_civilite.text = "Mr"
    cb_civilite.enabled = false
    edit_prenom.text = ""
    edit_nom.text = ""
    edit_adresse.text = ""
    edit_societe.text = ""
    edit_societe.enabled = false
    edit_courriel.text = ""
    edit_telephone.text = ""
    edit_codeP.text = ""
    edit_ville.text = ""
    cb_acces.text = "Accès direct"
    cb_distance.text = "- 1km"
    Edit_adresseL.enabled = false
    Edit_adresseL.text = ""
    Rich_observations.visible = true
    Rich_consignes.visible = false
    Rich_observations.text = ""
    Rich_consignes.text = ""
    societeRb.checked = false
    particulierRB.checked = false
    Check_livraison.checked = false
    Edit_adresseL.enabled = false  
    razGrilleCde  
    recno = fichier.size\rsize
    NbClients
    GetLocalTime (MyTime) 'obtenir l'année pour constituer un début de n° automatique (et connaitre l'année d'entrée)
    if nbc = 0 then
        panel_id.caption = str$(MyTime.wYear)+str$(nbc+1) 'numéro automatique de départ 
    else   
        NoAuto = val(mid$(rtrim$(tabClient$(1,nbc)),5,5))+1 'numéro automatique
        panel_id.caption = str$(MyTime.wYear)+str$(noAuto)
    end if
    modifBTN.visible = false 'modifier les boutons
    enrBTN.visible = true    ' de modif à enregistrer
    Panel_nbClients.caption = str$(nbc)
    StatusBar.Panel(0).Caption = "Fichier client de "+str$(fichier.size)+" octets   |   enregistrement n°"+str$(recno+1)+"  |  de "+str$(rsize)+" octets  |  "+str$(nbc)+ " clients"
end sub
'==========================
SUB lecture(rec as word) 'lecture en temps réel du fichier ouvert au début
    razGrilleCde
    if nbc = 0 then
    modifBTN.visible = false
    enrBTN.visible = true
    else 
    modifBTN.visible = true
    enrBTN.visible = false   
    end if 
    fposition = rec * rsize 
    if (fposition<0) or (fposition>(fichier.size-1)) then exit sub
    fichier.position = fposition
    fichier.readUDT(client) 'utilisation d'un UDT
    with client
        panel_id.caption = .id$
        cb_civilite.text = .civilite$
        edit_prenom.text = .prenom$
        edit_nom.text = .nom$
        edit_adresse.text = .adresse$
        edit_societe.text = .raison_sociale$
        edit_courriel.text = .courriel$
        edit_telephone.text = .telephone$
        edit_codeP.text = .codeP$
        edit_ville.text = .ville$
        cb_acces.text = .acces$
        cb_distance.text = .distance$
        Edit_adresseL.text = .adresseL$
        Rich_observations.text = .observation$
        Rich_consignes.text = .consigne$
        ff$ = .fin$
    end with
    ' validation des cases à cocher
    if edit_societe = "" then
        cb_civilite.enabled = true
        edit_societe.enabled = false
        societeRb.checked = false
        particulierRB.checked = true
    else
        cb_civilite.enabled = false
        edit_societe.enabled = true
        societeRb.checked = true
        particulierRB.checked = false
    end if    
        
    if Edit_adresseL.text = "" then
        Check_livraison.checked = false
        Edit_adresseL.enabled = false
    else
        Check_livraison.checked = true    
        Edit_adresseL.enabled = true
    end if        
    recno = rec
    NbClients
    'initGrilleCde
    lien_cli_Cde 'appel du lien pour affichage des commandes dans les fiches client    
    gauge.max = nbc ' cette jauge se positionne en fonction du nombre de client et l'affiche en %
    gauge.position = recno+1
    Panel_nbClients.caption = "Client n° "+str$(recno+1)
    Panel_commandes.Caption = "Commandes associées identifiant client : "+panel_id.caption
    StatusBar.Panel(0).Caption = "Fichier client de "+str$(fichier.size)+" octets   |   enregistrement n°"+str$(recno+1)+"  |  de "+str$(rsize)+" octets  |  "+str$(nbc)+ " clients"
    totalCde ' calcul virtuel (non mémorisé) de la somme des commandes
end sub    
'==========================
sub rechercheC 'recherche par n° de client(pas par identifiant) 
    edit_no.visible = true
    NoEnrBTN.visible = true
    Panel_nbClients.caption = "Recherche client n° : "           
end sub
'==========================
sub enregistrer 'enregistrement d'un client par UDT
    with client '794 octets
        .id$ = panel_id.caption
        .civilite$ = cb_civilite.text
        .prenom$ = edit_prenom.text
        .nom$ = edit_nom.text
        .adresse$ = edit_adresse.text 
        .raison_sociale$ = edit_societe.text 
        .courriel$ = edit_courriel.text 
        .telephone$ = edit_telephone.text
        .codeP$ = edit_codeP.text 
        .ville$ = edit_ville.text 
        .acces$ = cb_acces.text
        .distance$ = cb_distance.text
        .adresseL$ = Edit_adresseL.text
        .observation$ = Rich_observations.text
        .consigne$ = Rich_consignes.text
        .fin$ = chr$(255)
    end with
 
    fichier.position = nbc*rsize 'se positionner à la fin
    Panel_nbClients.caption = str$(nbc)
    StatusBar.Panel(0).Caption = "Fichier client de "+str$(fichier.size)+" octets | enregistrement n°"+str$(recno+1)+"  |  de "+str$(rsize)+" octets  |  "+str$(nbc)+ " clients"
    fichier.WriteUDT(client) 'écrire dans le fichier ouvert en read/write
    combo_id.addItems = ""
end sub
'==========================
sub modifier 'modifier les données clients directement dans le fichier ouvert
    with client '794
        .id$ = panel_id.caption
        .civilite$ = cb_civilite.text
        .prenom$ = edit_prenom.text
        .nom$ = edit_nom.text
        .adresse$ = edit_adresse.text 
        .raison_sociale$ = edit_societe.text 
        .courriel$ = edit_courriel.text 
        .telephone$ = edit_telephone.text
        .codeP$ = edit_codeP.text 
        .ville$ = edit_ville.text 
        .acces$ = cb_acces.text
        .distance$ = cb_distance.text
        .adresseL$ = Edit_adresseL.text
        .observation$ = Rich_observations.text
        .consigne$ = Rich_consignes.text
        .fin$ = chr$(255)
    end with

    fichier.position = fichier.size-(rsize*recno) 'se positionner sur le bon client
    fichier.position = rsize*recno
    Panel_nbClients.caption = "Modifier les données du client n°"+str$(recno+1)+" sur "+str$(nbc)
    StatusBar.Panel(0).Caption = "Fichier client de "+str$(fichier.size)+" octets | enregistrement n°"+str$(recno+1)+"  |  de "+str$(rsize)+" octets  |  "+str$(nbc)+ " clients"
    fichier.WriteUDT(client) 'sauvegarder les modifs
end sub    
'==========================
sub supprimer 'supprimer définitivement un client dans la base
    if messageBox  ("supprimer cet enregistrement","n° "+str$(recno+1),1) = 1 then            
    sz = fichier.size
        if sz <> 0 then
            fichier.position = 0
            mem.position = 0
            mem.copyFrom(fichier,sz) 'copier le fichier client en mémoire
            mem.position = 0
            Fichier$ = Mem.ReadStr(sz) 'de la mémoire dans une chaine
            fichier.close 'fermer le stream fichier
            supp$ = mid$(fichier$,rsize*recno,rsize) 'se positionner sur le bon endroit
            suppF$ = replacesubStr$(fichier$,supp$,"") 'remplacer par rien la zone à supprimer
            supp.Open("client.bdr", fmCreate) 'ouvrir un nouveau stream qui 
            supp.writeStr(suppf$,len(suppf$)) 'écrasera l'ancien fichier
            supp.close      
            init 'réouvrir fichier pour une utilisation normale
         else 
            exit sub '0
         end if
     end if
END SUB
'==========================    
sub NbClients ' déterminer le nombre de clients = longueur du fichier / longueur d'un enregistrement
    nbc = fichier.size/rsize
    panel_fiche.caption = str$(nbc)
end sub
'==========================   
sub RechNoEnr 'lecture du n° client recherché
    lecture(val(edit_no.text)-1)
    edit_no.visible = false
    NoEnrBTN.visible = false
    Panel_nbClients.caption = "Client n° "+str$(recno+1)     
end sub    
'========================== 
sub validBTN
    trouvBTN.enabled = true          
    listesBTN.enabled = true  
    menuFenetre.delItems(Item4)
    menuFF.enabled = false
    cliListForm.close         
end sub
'========================== 
sub validBTN2
    trouvBTN.enabled = true          
    listesBTN.enabled = true  
    menuFenetre.delItems(Item2)
    menuFF.enabled = false
    'rechCliEdit.readOnly = true
    rechCliEdit.enabled = false
    rechCliEdit.color = 16374739
    rechForm.close         
end sub                
'==========================
sub listeCli 'lister les clients
    cliListForm.top = cliForm.top +60
    cliListForm.left = cliForm.left     
    cliListForm.show
    menuFF.enabled = true
    cliListForm.caption = "Liste clients"
        menuFenetre.addItems(Item4) 
        item4.caption = cliListForm.caption    
        item4.onClick = ouvF 
    listesBTN.enabled = false 'éviter une autre liste en déclalant la fenêtre
    grille_Cli.rowcount = nbc+1
    for co = 0 to grille_Cli.colcount
        for ro = 1 to nbc'+1 
            grille_Cli.cell(co,ro) = tabClient$(co+1,ro)
            grille_Cli.cell(15,ro) = str$(ro)
        next ro
    next co
    nbCliPanel.caption = "Nombre de clients : "+str$(nbc)+" au " + str$(MyTime.wDay) + " / " + str$(MyTime.wMonth) + " / " + str$(MyTime.wYear)
end sub
'==========================
sub cellcli_Cde(col%,row%) 'en cliquant sur la cellule de l'identifiant les commandes s'affichent dans un message
    cdeList$ = tabCde$(1,0)+"           "+tabCde$(2,0)+"          "+tabCde$(3,0)+"      et      "+tabCde$(4,0)+ " en €"+chr$(13)
    StatusBarA.Panel(0).Caption = "Cellule : "+str$(col%)+" x "+str$(row%)
    for i = 0 to nbcde
        if grille_Cli.cell(0,row%) = tabCde$(0,i) then
        cdeList$ = cdelist$ +tabCde$(1,i)+tabCde$(2,i)+left$(tabCde$(3,i),40)+tabCde$(4,i)+chr$(13)           
        end if           
    next i  
    dim recV as Word
    if col% = 0 then showmessage cdeList$
    if col% = 15 then 
        'showmessage grille_Cli.cell(col%,row%)      
        recV = val(grille_Cli.cell(col%,row%))
        SetFocus(cliForm.handle)
        'showmessage str$(recV)
        lecture(recV-1)
    end if    
        '
end sub
'==========================
sub export 'exporter les fiches clients pour être relu dans Excel (par exemple)
    if messageBox  ("Exporter les fiches clients ?",str$(nbc)+" clients enregistrer ",1) = 1 then 
        listeCli
        DIM SauveCli as QSaveDialog
        SauveCli.Filter = "Fichier export (*.csv)|*.csv" 
        SauveCli.FilterIndex = 1
        SauveCli.Caption = "Exporter le fichier client"  
        IF SauveCli.Execute THEN
            exportCli$ = SauveCli.FileName
            IF UCASE$(RIGHT$(exportCli$,4))<>".CSV"  THEN exportCli$ = exportCli$ + ".csv"
            grille_Cli.saveToFile (exportCli$,0,0,nbc+1)
            showmessage "Fichier client exporté sous "+exportCli$
            cliListForm.close
        end if
    else
        exit sub
    end if
end sub         
'==========================        
sub preview 'visualisation sommaire de l'impression des clients et leurs commandes
    apForm.show
    menuFF.enabled = true
        menuFenetre.addItems(Item3) 
        item3.caption = apForm.caption  
        item3.onClick = ouvF      
if nbc>1 then 
    pluriel$ = "s"
else
    pluriel$ = ""
end if
ligne$ =  "__________________________________________________________________________________________"            

    richEditA.clear 

    richeditA.addstrings "Fichier clients___ L I S T I N G   R E C A P I T U A T I F   "+str$(nbc) + " clients enregistré"+pluriel$+" au  " + str$(MyTime.wDay) + " / " + str$(MyTime.wMonth) + " / " + str$(MyTime.wYear)+"_____"
    richeditA.addstrings " "   
    richeditA.addstrings " "
    richeditA.addstrings ligne$
    
    'cdeList$ = tabCde$(1,0)+" "+rtrim$(tabCde$(2,0))+"          "+tabCde$(3,0)+"      et      "+tabCde$(4,0)+ " en €"+chr$(13)
        for ro = 1 to nbc'+1    
            cdeList$ = ""
            for i = 1 to nbcde
                if tabClient$(1,ro) = tabCde$(0,i) then
                
                    cdeList$ = cdelist$ +chr$(9)+"n° "+tabCde$(1,i)+" au  "+tabCde$(2,i)+"  -  "+left$(tabCde$(3,i),40)+chr$(9)+chr$(9)+rtrim$(tabCde$(4,i))+" €"+chr$(13)           
                end if            
            next i
            richeditA.addstrings tabClient$(0,ro)+chr$(9)+(rtrim$(tabClient$(1,ro))+chr$(9)+rtrim$(tabClient$(3,ro))+chr$(9)+rtrim$(tabClient$(4,ro))+chr$(9)+rtrim$(tabClient$(5,ro))+tabClient$(6,ro)+tabClient$(9,ro)+"  -  "+tabClient$(10,ro))
            richeditA.addstrings chr$(9)+"Commandes associées"
            
            richeditA.addstrings cdeList$
            richeditA.addstrings ligne$            
        next ro
end sub 
'==========================      
sub etat 'impression WISIWIG du préview
    dim printer_name$
    dim buff as long
    dim fr as FormatRange    
    dim Image2 AS QBITMAP
    image2.bmp = hDir$ + "\buzz2bug.bmp"
    dim S AS QRECT
      S.Left = 0
      S.Top = 0
      S.Right = Image2.Width
      S.Bottom = Image2.Height
      DIM D AS QRECT
      D.Left = 100
      D.Top = 220
      D.Right = D.Left + Image2.Width
      D.Bottom = D.Top + Image2.Height

  printer_name$ = field$(printer.printers(0)," on ",1)
 fr.rc_left=56.7*10
 fr.rc_top =56.7*10
 fr.rc_right = printer.pagewidth*56.7
 fr.rc_bottom = printer.pageheight*56.7

 fr.rcPage_left =0
 fr.rcPage_top =0
 fr.rcPage_right =printer.pagewidth*56.7
 fr.rcPage_bottom =printer.pageheight*56.7
   printer.begindoc
   
   Printer.Fillrect( 100,100,2300,150,16374739)
   
 fr.hdc = printer.handle' utiliser le même DC pour le calibrage et le rendu
 fr.hdcTarget = printer.handle' Pointer vers le handle du Printer
 fr.chrg_cpmax = -1
 fr.chrg_cpmin = 0
  SendMessageB(richEditA.handle, EM_FORMATRANGE, true, fr)
  Printer.CopyRect D, Image2, S
 printer.enddoc
end sub  
'==========================
sub finAp
    menuFenetre.delItems(Item3) 
    menuFF.enabled = false
    apForm.close   
end sub     
'======================================SUBs de navigation et tâches Cde============================

sub menuCde (Sender AS QMenuItem) 'clic sur un des menu commande
    select case sender.caption
        case "&Enregistrer"
            enregistrerCde
        case "&Nouveau"
            nouveauCde
        case "&Modifier"
            modifierCde
    end select
END SUB  
'==========================
sub changeCde 'lecture du fichier commande par glissement
    lectureCde(slide_cde.position)
end sub  
'======================================SUBs de mise en forme Cde============================
sub commandes
    initComboCde
    lien_Cde_cli    
    cdeForm.show   
    menuFF.enabled = true
        menuFenetre.addItems(Item1) 
        item1.caption = cdeForm.caption 
        item1.onClick = ouvF
        Panel_nbCommandes.caption = "Montant cumulé progressif : "+strf$(val(tabCde$(4,1)),ffnumber,9,2)+"€"

    Cde_lbl.Caption = "Fichier commande - enregistrement n°"+str$(recnoCde+1)+" | de "+str$(nbcde)+ " commandes"
end sub
'==========================
sub finCommande
    menuFenetre.delItems(Item1) 
    menuFF.enabled = false
    cdeForm.close
end sub
'==========================
sub cdeNom 'remplissage des édits par le texte du combobox
    if combo_id.itemIndex = 0 then editCde_prenom.text = "" : editCde_nom.text = "" : goto finOp
    EditCde_Id.text = rtrim$(tabClient$(1,combo_id.itemIndex))
    editCde_prenom.text = rtrim$(tabClient$(3,combo_id.itemIndex))
    editCde_nom.text = rtrim$(tabClient$(4,combo_id.itemIndex))
    editCde_no.text = str$(nbcde+1)+"/"+EditCde_Id.text
    finOp:
end sub

'======================================SUBs de base Cde============================

sub totalCde 'somme des commandes dans les fiches client
tt = 0
    for ro = 1 to grille_Cde.rowcount
        'tt = tt + val(left$(rtrim$(grille_Cde.cell(3,ro)),len(grille_Cde.cell(3,ro))-1))
        tt = tt + val(grille_Cde.cell(3,ro))
    next ro
    Panel_CdeT.caption = "Total commandes : "+str$(tt)
end sub
'==========================
sub lien_Cde_cli 'lien commandes / client 
    for i = 0 to nbc
        if editCde_Id.text = tabClient$(1,i) then
            editCde_prenom.text = tabclient$(3,i)
            editCde_Nom.text = tabclient$(4,i)
        end if    
    next i        
end sub    
'==========================
sub nouveauCde 'nouvelle commande
    recnoCde = fichierCde.size\cdesize
    NbCommandes
    slide_Cde.max = nbcde-1
    slide_Cde.position = recnoCde+1
    slide_Lbl.caption = "Commande n° "+str$(recnoCde+1)
    menuModifCde.enabled = false
    menuEnrCde.enabled = true
    EditCde_Id.text = ""
    editCde_prenom.text = ""
    editCde_nom.text = ""    
    editCde_no.text = ""
    editCde_date.text = ""
    editCde_montant.text = ""
    RichCde_libelle.text = "" 
end sub 
'==========================
SUB lectureCde(recCde as word) 'lecture du fichier commande ouvert au début 
    montantCde = 0
    if nbCde = 0 then
        menuModifCde.enabled = false
        menuEnrCde.enabled = true
    else 
        menuModifCde.enabled = true
        menuEnrCde.enabled = false   
    end if 
    cdeposition = recCde * Cdesize 
    if (cdeposition<0) or (cdeposition>(fichierCde.size-1)) then exit sub
    fichierCde.position = cdeposition
    fichierCde.readUDT(commande)
    with commande
        EditCde_Id.text = rtrim$(.idCde$)
        editCde_no.text = rtrim$(.noCde$)
        editCde_date.text = rtrim$(.dateCde$)
        RichCde_libelle.text = rtrim$(.libelleCde$)
        editCde_montant.text = rtrim$(.montantCde$)
        ffCde$ = .finCde$
    end with      
    recnoCde = recCde
    NbCommandes
    slide_Cde.max = nbCde -1
    slide_Cde.position = recnoCde
    slide_Lbl.caption = "Commande n° "+str$(recnoCde+1)
    lien_Cde_cli
    for lc = 1 to recnoCde+1
        montantCde = montantCde + val(rtrim$(tabCde$(4,lc)))
    next lc    
    panel_nbcommandes.caption = "Montant cumulé progressif : "+strf$(montantCde,ffnumber,9,2)+"€"
end sub    
'==========================
sub enregistrerCde 'enregistrement par UDT
    with commande
        .idCde$ = EditCde_Id.text 
        .noCde$ = editCde_no.text 
        .dateCde$ = editCde_date.text 
        .libelleCde$ = RichCde_libelle.text 
        .montantCde$ = editCde_montant.text 
        .finCde$ = ffCde$ 
    end with  
    fichierCde.position = nbcde*cdesize
    'Cde_lbl.caption = str$(nbcde)
    Cde_lbl.Caption = "Fichier commande - enregistrement de "+str$(nbcde)+ " commandes"
    fichierCde.WriteUDT(commande)
    NbCommandes
    lecture(nbCde)
    initGrilleCde
    lien_cli_Cde
    showmessage "Commande enregistrée"
end sub    
'==========================
sub modifierCde 'modifier la bonne commande
    with commande
        .idCde$ = EditCde_Id.text 
        .noCde$ = editCde_no.text 
        .dateCde$ = editCde_date.text 
        .libelleCde$ = RichCde_libelle.text 
        .montantCde$ = editCde_montant.text 
        .finCde$ = ffCde$ 
    end with 

    fichierCde.position = fichierCde.size-(cdesize*recnoCde)
    fichiercde.position = cdesize*recnoCde
    Cde_lbl.Caption = "Fichier commande - enregistrement de "+str$(nbcde)+ " commandes"
    fichierCde.WriteUDT(commande)
    lectureCde(nbCde)
    initGrilleCde
    lien_cli_Cde
end sub    
'==========================
sub NbCommandes 'compter le nombre de commande
    nbcde = fichierCde.size/cdesize
    Cde_lbl.Caption = "Fichier commande - enregistrement de "+str$(nbcde)+ " commandes"
end sub
'========================== recherche et trouve clients ==================================================
sub recherche
    rechForm.top = cliForm.top +60
    rechForm.left = cliForm.left      
    rechForm.show
    menuFF.enabled = true
        rechForm.caption = "Critère de recherche pour les clients"
        menuFenetre.addItems(Item2) 
        item2.caption = rechForm.caption
        item2.onClick = ouvF
        trouvBTN.enabled = false        
    if rech = false then
        initListRech
        rech = true
    else
        exit sub
    end if        
end sub
'==========================
sub critereCell(Col%,Row%) 'affichage de la recherche dans le bouton
    for i = 0 to 14
        grilleR_cli.cell(i,row%) = ""
    next i           
    trouverBTN.caption = "Trouver les données selon le critère de recherche pour les clients ligne "+str$(row%)+ " colonne "+str$(col%+1)   
    'rechCliEdit.readOnly = true
    rechCliEdit.enabled = false
    rechCliEdit.color = 16374739
end sub
'==========================
sub affRech(Col%,Row%,value$) 'transfert de la sélection de la grille dans Qedit
    'rechCliEdit.readOnly = true
    rechCliEdit.enabled = false
    rechCliEdit.color = 16374739  
    rechCliEdit.text = value$ 
end sub     
'==========================
sub rechPart 
    'rechCliEdit.readOnly = false
    rechCliEdit.enabled = true
    rechCliEdit.color = &hb4f9ff
    trouverBTN.caption = "Trouver"
end sub
'==========================
sub lignePM(Sender as QButton)
    lig$ = sender.caption
    select case lig$
        case "^"
            if grilleR_cli.rowcount > 2 then grilleR_cli.rowcount = grilleR_cli.rowcount-1
        case "v"
            grilleR_cli.rowcount = grilleR_cli.rowcount+1
    end select
end sub
'==========================
sub selRech 'sélection d'après la couleur de QEdit !!!  
dim edCoul as long
    edCoul = rechCliEdit.color
    select case edCoul
        case 11860479
            trouvRech
        case 16374739
            trouvRech2
    end select
end sub    
'==========================
sub trouvRech 'recherche globale de la sélection
    'rechCliEdit.readOnly = true
    rechCliEdit.enabled = false
    rechCliEdit.color = 16 374739
    dim resRech$ 
    dim pp as word, mp as word : pp = 0 : mp = 0 : nbr = 1
    rechCC$ = rechCliEdit.text
    sz = fichier.size
    if sz <> 0 then
        fichier.position = 0
        mem.position = 0
        mem.copyFrom(fichier,sz)
        mem.position = 0                       
            for i = 0 to sz 
                Mem.position = i  
                resRech$ = Mem.ReadStr(len(rechCC$))
                if resRech$ = rechCC$ then 
                    recR = int(i/794)
                    recBtn = nbr+1
                    tabClientR$(nbr) = str$(recR+1)
                    fichier.position = recR*794
                    memR.position = mp
                    memR.copyFrom(fichier,794)
                    mp = mp + 794
                    nbr = nbr+1
                end if                       
                pp = pp+1                                        
                'if pp = sz then goto trouver  
           next i                        
    end if                          
    'trouver:
    mem.close
    memR.position = 0    
    msz = memR.size
    for L = 0 to (msz /794)'-1
        memR.readUDT(client)
        tabRClient$(1,l+1) = client.id$ 
        tabRClient$(2,l+1) = client.civilite$ 
        tabRClient$(3,l+1) = client.prenom$
        tabRClient$(4,l+1) = client.nom$
        tabRClient$(5,l+1) = client.adresse$
        tabRClient$(6,l+1) = client.raison_sociale$
        tabRClient$(7,l+1) = client.courriel$
        tabRClient$(8,l+1) = client.telephone$
        tabRClient$(9,l+1) = client.codeP$
        tabRClient$(10,l+1) = client.ville$
        tabRClient$(11,l+1) = client.acces$
        tabRClient$(12,l+1) = client.distance$
        tabRClient$(13,l+1) = client.adresseL$
        tabRClient$(14,l+1) = client.observation$
        tabRClient$(15,l+1) = client.consigne$        
    next L 
    memR.close
    trouvCli
end sub
'==========================  
sub trouvRech2 'recherche stricte de la sélection
    'rechCliEdit.readOnly = true
    rechCliEdit.enabled = false
    rechCliEdit.color = 16374739
    dim resRech$
    dim pp as word, mp as word : pp = 0 : mp = 0 : nbr = 1
    dim idCol as byte : idCol = val(right$(trouverBTN.caption,2))
    select case idCol
        case 1
            posRech = posId
        case 2 
            posRech = posCiv
        case 3
            posRech = posPre
        case 4
            posRech = posNom
        case 5
            posRech = posAdr
        case 6
            posRech = posRsl
        case 7
            posRech = posCou
        case 8
            posRech = posTel
        case 9
            posRech = posCod
        case 10
            posRech = posVil
        case 11
            posRech = posAcc
        case 12
            posRech = posDis
        case 13
            posRech = posAdl
        case 14
            posRech = posObs
        case 15
            posRech = posCsg
        case 16
            posRech = posFin
    end select
            
    rechCC$ = rechCliEdit.text
    sz = fichier.size
    if sz <> 0 then
        fichier.position = 0
        mem.position = 0
        mem.copyFrom(fichier,sz)
        mem.position = posRech                       
            for i = posRech to sz step 794
                Mem.position = i  
                resRech$ = Mem.ReadStr(len(rechCC$))
                if resRech$ = rechCC$ then 
                    recR = int(i/794)
                    recBtn = nbr+1
                    tabClientR$(nbr) = str$(recR+1)
                    fichier.position = recR*794
                    memR.position = mp
                    memR.copyFrom(fichier,794)
                    mp = mp + 794
                    nbr = nbr+1
                end if                       
                pp = pp+1                                        
                'if pp = sz then goto trouver  
           next i                        
    end if                          
    'trouver:
    mem.close
    memR.position = 0    
    msz = memR.size
    for L = 0 to (msz /794)'-1
        memR.readUDT(client)
        tabRClient$(1,l+1) = client.id$ 
        tabRClient$(2,l+1) = client.civilite$ 
        tabRClient$(3,l+1) = client.prenom$
        tabRClient$(4,l+1) = client.nom$
        tabRClient$(5,l+1) = client.adresse$
        tabRClient$(6,l+1) = client.raison_sociale$
        tabRClient$(7,l+1) = client.courriel$
        tabRClient$(8,l+1) = client.telephone$
        tabRClient$(9,l+1) = client.codeP$
        tabRClient$(10,l+1) = client.ville$
        tabRClient$(11,l+1) = client.acces$
        tabRClient$(12,l+1) = client.distance$
        tabRClient$(13,l+1) = client.adresseL$
        tabRClient$(14,l+1) = client.observation$
        tabRClient$(15,l+1) = client.consigne$        
    next L 
    memR.close
    trouvCli
end sub
'==========================      
sub TrouvCli 'affichage de la sélection    
    cliListForm.top = rechForm.top +30
    cliListForm.left = rechForm.left              
    cliListForm.show
    cliListForm.caption = "Liste clients"

    grille_Cli.rowcount = nbr
    for co = 0 to grille_Cli.colcount
        for ro = 1 to nbr 
            grille_Cli.cell(15,ro) = tabClientR$(ro)           
            grille_Cli.cell(co,ro) = tabRClient$(co+1,ro)
        next ro
    next co       
    nbCliPanel.caption = "Nombre d'occurences trouvées : "+str$(nbr-1) + " au " + str$(MyTime.wDay) + " / " + str$(MyTime.wMonth) + " / " + str$(MyTime.wYear)
        menuFenetre.addItems(Item4) 
        item4.caption = cliListForm.caption    
        item4.onClick = ouvF     
end sub    



'************************************************tri de la table*****************************************
sub trier
    triListForm.top = cliForm.top +100
    triListForm.left = cliForm.left     
    menuFF.enabled = true
    triListForm.caption = "Tris clients"
        menuFenetre.addItems(Item5) 
        item5.caption = triListForm.caption    
        item5.onClick = ouvF          
    triListForm.show
end sub        
'==========================
sub listeCliTri 'lister les clients     
    grille_CliTri.rowcount = nbc+1
    for co = 0 to grille_CliTri.colcount
        grille_CliTri.cell(co,0) = rtrim$(tabClient$(co,0)) 'vider l'entête
        for ro = 1 to nbc 'vider le contenu
            grille_CliTri.cell(co,ro) = rtrim$(tabClient$(co+1,ro))
            grille_CliTri.cell(15,ro) = str$(ro)
        next ro
    next co
    nbTriPanel.caption = "Nombre de clients : "+str$(nbc)
end sub    
'==========================
sub enrList
    cliStrList.item(0) = left$(hTab$,len(htab$)-1) 'retirer la virgule de l'entete
    cliStrList.saveToFile(hDir$ + "\clientStrList.csv")   
end sub    
'==========================
sub ouvList 'vider tout
    for co = 0 to grille_CliTri.colcount
        for ro = 0 to nbc'+1 
            grille_CliTri.cell(co,ro) = ""
            grille_CliTri.cell(15,ro) = ""
        next ro
    next co       
    grille_CliTri.loadFromFile(hDir$ + "\clientStrList.csv",0,0,nbc)
end sub    
'==========================
sub inithList 'initialisation entête grille
    hTab$ = ""
    for i = 0 to grille_cliTri.colCount-1
        grille_cliTri.colWidths(i) = 50 ' largeur fixe de colonne
        hTab$ = hTab$ + grille_cliTri.cell(i,0)+"," 'constituer l'entête
        tabClient$(i,0) = grille_cliTri.cell(i,0) ' constituer l'entête tableau
        triCombo.addItems grille_cliTri.cell(i,0) ' ajouter des items au combo
    next     
end sub    
'==========================
sub listCell(Col%,Row%) 'pour la grille clic colonne
    selTri = 1
    dim z$
    dim triList$
    dim tt$ : tt$ = ""
    triList$ = "00010203040506070809101112131415" 'ordre de départ   
    'if col% = 0 then exit sub
    if col% < 10 then 
        z$ = "0"
    else
        z$ = ""
    end if                
    col$ = z$+str$(col%)
    ' opération de raccord de chaines
    tempList$ = replace$(triList$,"  ",(col%*2)+1)
    LList$ = left$(tempList$,(col%*2))
    RList$ = ltrim$(right$(tempList$,32-(col%*2)))
    tempList$ = col$ + LList$ + RList$
    for n = 1 to len(tempList$) step 2
        t$ = mid$(tempList$,n,2)
        tt$ = tt$+" | "+t$
    next n    
    triListBTN.caption = "Trier par " + grille_CliTri.cell(col%,0)
    StatusBarAA.Panel(0).Caption = "Cellule : "+str$(col%)+" x "+str$(row%)+" - ordre des colonnes après tri : " + tt$ 'tempList$
end sub   
'==========================
sub listCombo 'pour le combo choix item
    selTri = 1
    dim z$
    dim triList$
    dim tt$ : tt$ = ""
    triList$ = "00010203040506070809101112131415"    
    'if triCombo.itemIndex = 0 then exit sub
    if triCombo.itemIndex < 10 then 
        z$ = "0"
    else
        z$ = ""
    end if                
    col$ = z$+str$(triCombo.itemIndex)

    tempList$ = replace$(triList$,"  ",(triCombo.itemIndex*2)+1)
    LList$ = left$(tempList$,(triCombo.itemIndex*2))
    RList$ = ltrim$(right$(tempList$,32-(triCombo.itemIndex*2)))
    triListBTN.caption = "Trier par "+triCombo.item(triCombo.itemIndex)
    tempList$ = col$ + LList$ + RList$
    for n = 1 to len(tempList$) step 2
        t$ = mid$(tempList$,n,2)
        tt$ = tt$+" | "+t$
    next n
    StatusBarAA.Panel(0).Caption = "Index : "+str$(triCombo.itemIndex)+" - ordre des colonnes après tri : " + tt$ 'tempList$
end sub           
'==========================
sub triStrlist 'ordonner la série selon le choix de l'utilisateur
    if (selTri = 0 or right$(triListBTN.caption,2) = "id") then listeCliTri : exit sub
    if raz = 0 then listeCliTri 'showmessage "Remettre la base " : exit sub
    cliStrList.clear
    cliStrList.addItems "-"
    for i = 1 to nbc+1 'L+1  
        strClitri$ = ""     
        strClitri$ = _      
        grille_CliTri.cell(val(mid$(tempList$,1,2)),i)+","+_
        grille_CliTri.cell(val(mid$(tempList$,3,2)),i)+","+_ 
        grille_CliTri.cell(val(mid$(tempList$,5,2)),i)+","+_      
        grille_CliTri.cell(val(mid$(tempList$,7,2)),i)+","+_
        grille_CliTri.cell(val(mid$(tempList$,9,2)),i)+","+_
        grille_CliTri.cell(val(mid$(tempList$,11,2)),i)+","+_
        grille_CliTri.cell(val(mid$(tempList$,13,2)),i)+","+_
        grille_CliTri.cell(val(mid$(tempList$,15,2)),i)+","+_
        grille_CliTri.cell(val(mid$(tempList$,17,2)),i)+","+_
        grille_CliTri.cell(val(mid$(tempList$,19,2)),i)+","+_
        grille_CliTri.cell(val(mid$(tempList$,21,2)),i)+","+_
        grille_CliTri.cell(val(mid$(tempList$,23,2)),i)+","+_
        grille_CliTri.cell(val(mid$(tempList$,25,2)),i)+","+_
        grille_CliTri.cell(val(mid$(tempList$,27,2)),i)+","+_
        grille_CliTri.cell(val(mid$(tempList$,29,2)),i)+","+_
        grille_CliTri.cell(val(mid$(tempList$,31,2)),i)  
        cliStrList.addItems strClitri$                     
    next i  
                                 
    cliStrList.delItems(cliStrList.itemCount-1)       
    cliStrList.sort
    selTri = 0
    raz = 0
    'entête 
        hTab$ = ""
        hTab$ = _      
        grille_CliTri.cell(val(mid$(tempList$,1,2)),0)+","+_
        grille_CliTri.cell(val(mid$(tempList$,3,2)),0)+","+_ 
        grille_CliTri.cell(val(mid$(tempList$,5,2)),0)+","+_      
        grille_CliTri.cell(val(mid$(tempList$,7,2)),0)+","+_
        grille_CliTri.cell(val(mid$(tempList$,9,2)),0)+","+_
        grille_CliTri.cell(val(mid$(tempList$,11,2)),0)+","+_
        grille_CliTri.cell(val(mid$(tempList$,13,2)),0)+","+_
        grille_CliTri.cell(val(mid$(tempList$,15,2)),0)+","+_
        grille_CliTri.cell(val(mid$(tempList$,17,2)),0)+","+_
        grille_CliTri.cell(val(mid$(tempList$,19,2)),0)+","+_
        grille_CliTri.cell(val(mid$(tempList$,21,2)),0)+","+_
        grille_CliTri.cell(val(mid$(tempList$,23,2)),0)+","+_
        grille_CliTri.cell(val(mid$(tempList$,25,2)),0)+","+_
        grille_CliTri.cell(val(mid$(tempList$,27,2)),0)+","+_
        grille_CliTri.cell(val(mid$(tempList$,29,2)),0)+","+_
        grille_CliTri.cell(val(mid$(tempList$,31,2)),0)+","  
        
    enrList
    ouvList                     
    'inithList
end sub     
'==========================
sub finTri 
    menuFenetre.delItems(Item5)
    menuFF.enabled = false
    'triListForm.close     
end sub    

'========================== gestion des fenêtres ===================================
sub ouvF(sender as QMenuItem) 'clic sur le menu appel de la fenetre
    dim ft as byte
    ft = sender.tag
    select case ft
        case 51
            cdeForm.show
        case 52
            rechForm.show
        case 53     
            apForm.show               
        case 54
            cliListForm.show
        case 55
            triListForm.show   
    end select
end sub        
'========================== 
sub fermF 'fermeture conditionnelle en fonction de l'affichage
    if cdeForm.visible = true then cdeForm.close
    if rechForm.visible = true then rechForm.close : rechCliEdit.enabled = false 'rechCliEdit.readOnly = true
    if apForm.visible = true then apForm.close
    if cliListForm.visible = true then cliListForm.close
    if triListForm.visible = true then triListForm.close
end sub    
'=========================== infos =================================================================
sub APropos
    showmessage "_____par Gérald VERDIER_____"+chr$(13)+_
                "__Base de donnée relationnelle__"+chr$(13)+_
                "_gerald.verdier@club-internet.fr_"+chr$(13)+_
                "___________________________"
end sub                
'===========================
sub infoF  
panel_nbClients.caption = "Client n° "+str$(recno+1)
    showmessage _
                "Identifiant client : "+tabClient$(1,recno+1)+ chr$(13)+_
                "Client : "+rtrim$(tabClient$(2,recno+1))+" "+rtrim$(tabClient$(3,recno+1))+" "+rtrim$(tabClient$(4,recno+1))+ chr$(13)+_
                "_________________________________"+ chr$(13)+_
                "Adresse : "+rtrim$(tabClient$(5,recno+1))+ chr$(13)+_
                "Code postal : "+rtrim$(tabClient$(9,recno+1))+ chr$(13)+_
                "Ville : "+rtrim$(tabClient$(10,recno+1))+ chr$(13)+_
                "_________________________________"+ chr$(13)+_ 
                "Société : "+rtrim$(tabClient$(6,recno+1))+ chr$(13)+_
                "Courriel : "+rtrim$(tabClient$(7,recno+1))+ chr$(13)+_
                "Téléphone : "+rtrim$(tabClient$(8,recno+1))+ chr$(13)+_
                "_________________________________"+ chr$(13)+_ 
                "Accès : "+rtrim$(tabClient$(11,recno+1))+" - Distance : "+rtrim$(tabClient$(12,recno+1))+ chr$(13)+_
                "Livraison : "+rtrim$(tabClient$(13,recno+1))+ chr$(13)+_
                "_________________________________"+ chr$(13)+_                 
                "Observations : "+rtrim$(tabClient$(14,recno+1))+ chr$(13)+_
                "Consignes : "+rtrim$(tabClient$(15,recno+1))+ chr$(13)+_
                "_________________________________" 
end sub
'======================================SUBs FIN============================

cliForm.ShowModal
