Attribuer des valeurs aléatoires toutes différentes
john726
Messages postés5Date d'inscriptiondimanche 23 novembre 2008StatutMembreDernière intervention25 février 2009
-
23 févr. 2009 à 15:35
Caribensila
Messages postés2527Date d'inscriptionjeudi 15 janvier 2004StatutMembreDernière intervention16 octobre 2019
-
25 févr. 2009 à 16:18
bonjour a tous,
j'ai un pti projet scolaire en delphi, mais le probleme c'est que je n'arrive pas a coder deux choses.
Dans la premiere, j'ai besoin d'attribuer a environ 14 TButton.Caption un entier aléatoire compris entre 1 et 14 et ce, a chaque fois différent pour chacun des TButton.
Dans la seconde, je dois attribuer cette fois ci des valeurs constantes fixées dans 14 TEdit et d'une maniere aléatoire...
Le tout doit etre généré quand je clique sur un autre TButton !
Tous ceux qui pourront m'aider seront les bienvenue...merci de votre aide !
A voir également:
Attribuer des valeurs aléatoires toutes différentes
var
Form1: TForm1;
tabCaption: array of integer;
BoutonEnCours: TButton;
const
nbrButton = 14;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
i, StoNum: integer;
begin
for i := 0 to nbrButton - 1 do
begin
BoutonEnCours := TButton.Create(self);
BoutonEnCours.Parent := self;
BoutonEnCours.Top := i * 30;
BoutonEnCours.Left := 12;
BoutonEnCours.Width := 50;
BoutonEnCours.Height := 20;
StoNum := RandomFRom(tabCaption);
BoutonEnCours.Caption := IntToStr(StoNum);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
i: integer;
begin
randomize;
SetLength(TabCaption, NbrButton);
for i := 1 to nbrButton do
TabCaption[i] := i;
end;
procedure TForm1.FormDestroy(Sender: TObject);
var
i: integer;
begin
for i := nbrButton to 0 do
BoutonEnCours.free;
end;
end.
reste à trouver comment éviter les doublons dans les captions
john726
Messages postés5Date d'inscriptiondimanche 23 novembre 2008StatutMembreDernière intervention25 février 2009 24 févr. 2009 à 15:42
Merci beaucoup Cantador pour ton aide....mais pourrais tu avoir l'amabilité de m'envoyer le TForm qui va avec parce que je ne vois pas bien comment ça marche...je pense en effet qu'avec la fiche conception j'arriverais mieux a faire les liens et comprendre ton code(parce que je suis encore debutant^^) merci !
Vous n’avez pas trouvé la réponse que vous recherchez ?
cs_cantador
Messages postés4720Date d'inscriptiondimanche 26 février 2006StatutModérateurDernière intervention31 juillet 202114 24 févr. 2009 à 16:55
Tu as tout ce qu'il faut :
la méthode : Nouvelle application
On pose un bouton en plein milieu
On fait un copier coller du code dans Unit1 et
on branche les trois évènements :
OnClick
OnCreate
OnDestroy
Caribensila
Messages postés2527Date d'inscriptionjeudi 15 janvier 2004StatutMembreDernière intervention16 octobre 201918 25 févr. 2009 à 16:18
const
NbrButton = 3;
procedure GetRandomArray(var Tab : array of string);
var RandomIndex, LastIndex : Integer;
TempVal : String;
begin
LastIndex := length(Tab);
while LastIndex>1 do begin
RandomIndex := Random(LastIndex);
TempVal := Tab[RandomIndex];
Tab[RandomIndex] := Tab[LastIndex-1];
Tab[LastIndex-1] := TempVal;
Dec(LastIndex);
end;
end;
{Utilisation et vérification.}
procedure TForm1.Button1Click(Sender: TObject);
var CaptionsArray : array of string;
i : Integer;
begin
SetLength(CaptionsArray, NbrButton);
for i := 0 to High(CaptionsArray) do begin
CaptionsArray[i] := IntToStr(i+1);
end;
GetRandomArray(CaptionsArray); //Mélange aléatoirement le tableau.
Memo1.Clear;
for i := 0 to High(CaptionsArray) do begin
Memo1.Lines.Add(CaptionsArray[i]);
end;
end;