Dans un edit, je voudrais lire une cellule de tableau Excel de manière aléatoire mais je ne veux pas que le même champ s'affiche 2 fois.
Aussi, j'ai eu l'idée de recourir à un StringList :
- génération d'une liste reprenant la numérotation des champs (à partir de la ligne 2),
- tirage d'un nombre aléatoire,
- affichage de champ (ligne-n° tiré).
Or, le programme me tire plusieurs fois le même n° (on pourrait penser qu'il a mis en mémoire chaque champ).
Où est l'erreur ?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 public ExcelApplication1:variant; procedure TForm1.FormShow(Sender: TObject); var path:variant; i, NbQ:integer; begin path := ExtractFilePath(Application.ExeName); // chemin de l'application path := path+'\QR.xlsx'; ExcelApplication1:= CreateOleObject('Excel.Application'); ExcelApplication1.visible:=false; ExcelApplication1.Workbooks.Open(path); NbQ:=strtoint(string(ExcelApplication1.Cells[2,10].Value))+1; // Nb enr {création liste} Str := TStringList.Create; For i := 2 To NbQ Do begin Str.Add(inttostr(i)); listbox1.Items.Add(Str.Strings[i-2]); end; //randomize; end; procedure TForm1.BtnQClick(Sender: TObject); var l: integer; tir, i:integer; begin randomize; tir:=Random(Str.Count-1); edit4.Text:='tir : '+inttostr(tir); edit5.Text:='reste : '+inttostr(Str.Count); {-------MàJ de la liste-------} Str.Delete(tir); listbox1.Clear; For i := 1 To Str.Count-1 Do listbox1.Items.Add(Str.Strings[i-1]); {-------Affichage des valeurs-------} if Str.count<>0 then begin l:= strtoint(Str.Strings[tir]); listbox2.Items.Add(inttostr(l-1)); edit3.Text:=string(ExcelApplication1.Cells[l,3].Value); // l,c end else BtnQ.Enabled:=false; end; procedure TForm1.FormClose(Sender: TObject); //var CloseAction: TCloseAction); begin ExcelApplication1.DisplayAlerts := False; //mess enregistrer fichier ExcelApplication1.quit; ExcelApplication1:= Unassigned; Str.Free; end;
Partager