Autor Beitrag
Cookie007
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 96

win2000 / Xp / Linux9.3
Delphi7
BeitragVerfasst: So 08.01.06 11:08 
Hallo Leute

ich hab folgendes Problem,
Ich versuche ein item aus einer listbox per zufall auszuwählen soweit so gut,
aber er gibt mir dann einen Fatalen fehler an und delphi bricht zusammen
Ich versuche folgendes:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
  Randomize;

   taeter := random(form1.Listver.Count) +1;
   form1.Mordakte.Items.Add(form1.Listver.Items.Strings[taeter]);
   form1.Listver.Items.Delete (taeter);


er sagt mir dann: List Index out of bounds

Brauche hilfe

THX
Cookie 007

_________________
Mein System : A64 2400 Clawhammer, 1534 MB Ram, 200GB Festplatte, GeForce 6600 (Gigabyte)
Warum so ein langes Gesicht auf jede Frage gibt es eine Antwort, du musst sie nur suchen.(suchfunktion)
Fighter#1
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 787

Win XP, Ubuntu 8.04
Turbo Delphi 2006, Delphi 2005 Pe, Delphi 5 Pe, Netbeans 6.1, Eclipse, Microsoft VisualC#, Dev C++, PHP, HTML, CSS
BeitragVerfasst: So 08.01.06 11:33 
Also warum machst du das +1 ???
Gibt eigentlich keinen Sinn.
Ich habs mal so gelöst, da kann nix passieren so viel du auch klickst
Also hier:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
begin
If ListBox1.Items.Count > 0 Then begin
taeter:=Random(ListBox1.Items.Count);

ListBox2.Items.Add(ListBox1.Items.Strings[taeter]);
ListBox1.Items.Delete(taeter);
end;


mfg Fighter#1
Danniolo
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 693



BeitragVerfasst: So 08.01.06 11:36 
Titel: Re: Fehler bei zufälliger auswahl eines Items aus einer list
user profile iconCookie007 hat folgendes geschrieben:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
  Randomize;

   taeter := random(form1.Listver.Count) +1;
   form1.Mordakte.Items.Add(form1.Listver.Items.Strings[taeter]);
   form1.Listver.Items.Delete (taeter);


er sagt mir dann: List Index out of bounds


Kein Wunder, es muss heißen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
  //Randomize; // Besser in OnCreate aufgehoben
  taeter := random(form1.Listver.Count-1);
  form1.Mordakte.Items.Add(form1.Listver.Items.Strings[taeter]);
  form1.Listver.Items.Delete (taeter);


Und das Randomize solltest du lieber ins OnCreate tun...

Edit @Fighter#1: stimmt, da sollte noch Abfrage drin sein, ob überhaupt was drin is^^ :roll:
Cookie007 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 96

win2000 / Xp / Linux9.3
Delphi7
BeitragVerfasst: So 08.01.06 11:55 
hat wer ne ahnung wie ich hier nun eine begrenzung auf 8 hinkrieg
so dass er nicht zu hoch geht?

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
  If form1.Listver.Items.Count > 0 Then begin
  taeter:=Random(form1.Listver.Items.Count);

  form1.Mordakte.Items.Add(form1.Listver.Items.Strings[taeter]);
  form1.Listver.Items.Delete(taeter);

_________________
Mein System : A64 2400 Clawhammer, 1534 MB Ram, 200GB Festplatte, GeForce 6600 (Gigabyte)
Warum so ein langes Gesicht auf jede Frage gibt es eine Antwort, du musst sie nur suchen.(suchfunktion)
Marc.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1876
Erhaltene Danke: 129

Win 8.1, Xubuntu 15.10

BeitragVerfasst: So 08.01.06 12:14 
nicht zu hoch geht?

du meinst ein limit der anzahl der items in einer listbox?
einen befehl gibt es meines wissens nicht aber wir wärs mit:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
 if Listver.Items.Count = 8 then
  exit
 else
  Listver.Items.Add('');


oder soll die Integervariable "taeter" keinen höheren wert als 8 erreichen?

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
If listver.items.count >= 8 then exit
 else begin
  If form1.Listver.Items.Count > 0 Then begin
   taeter:=Random(form1.Listver.Items.Count);

   form1.Mordakte.Items.Add(form1.Listver.Items.Strings[taeter]);
   form1.Listver.Items.Delete(taeter);

end;



Hoffe habe dich nicht falsch verstanden, bin noch nicht ganz wach :roll:
Fighter#1
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 787

Win XP, Ubuntu 8.04
Turbo Delphi 2006, Delphi 2005 Pe, Delphi 5 Pe, Netbeans 6.1, Eclipse, Microsoft VisualC#, Dev C++, PHP, HTML, CSS
BeitragVerfasst: So 08.01.06 12:17 
NA klar,
ist doch ganz easy bau danach noch ne if abfrage ein ob der Wert
größer acht ist ein.
Oder wenn du meinst das nicht mehr als 8 taeter rüber kommen dann mach bei
private ne Variable in Integer ein die sich immer um eins erhöht und bei Acht is
schluss ( wieder ne if abfrage )
Aber das musst du schon selbst hinkriegen, sonst isses ja wie appausen . . .
Cookie007 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 96

win2000 / Xp / Linux9.3
Delphi7
BeitragVerfasst: So 08.01.06 12:22 
Ich will das bei dem random befehl nicht mehr als 8 rauskomt da nur 8 täter bei dem program zur verfügung stehen ich krich da nämlich werte wie "0" oder " " obwohl sie in der Listbox nicht stehen

THX
Cookie 007

_________________
Mein System : A64 2400 Clawhammer, 1534 MB Ram, 200GB Festplatte, GeForce 6600 (Gigabyte)
Warum so ein langes Gesicht auf jede Frage gibt es eine Antwort, du musst sie nur suchen.(suchfunktion)
Marc.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1876
Erhaltene Danke: 129

Win 8.1, Xubuntu 15.10

BeitragVerfasst: So 08.01.06 12:27 
natürlich bekommst du werte wie "0", eine listbox ist null-definiert

0: erster wert
1: zweiter wert
2: ...
usw.


8Täter => 8 Items in der listbox => deine Integervariable Taeter kann einen Wert von 0-7 annehmen ;)
Danniolo
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 693



BeitragVerfasst: So 08.01.06 12:29 
Um Werte 1 bis 8 zu erhhalten einfach: random(7)+1;
Marc.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1876
Erhaltene Danke: 129

Win 8.1, Xubuntu 15.10

BeitragVerfasst: So 08.01.06 12:47 
user profile iconCookie007 hat folgendes geschrieben:
Ich will das bei dem random befehl nicht mehr als 8 rauskomt da nur 8 täter bei dem program zur verfügung stehen{...}


wenn du nicht mehr als 8 Items in der listbox hast, kann Taeter auch NIE! größer gleich 8 werden! Sondern max. 7 ;)

wo ist also dein problem?