Slette Listbox items
Hej,Jeg står her med noget kode som irriterer mig grænseløst - tror det er nemmest hvis jeg kopierer koden ind her.
Der bruges to forme, Unit2 og Unit3.
-----Unit 2-----
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm2 = class(TForm)
ListBox1: TListBox;
Number: TEdit;
Button1: TButton;
procedure NumberChange(Sender: TObject);
procedure OpenForm3(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
antal : integer;
implementation
{$R *.dfm}
uses Unit3;
procedure TForm2.NumberChange(Sender: TObject);
begin
if Number.Text = '' then // Hvis '' så: antal = 0
antal := 0;
if Number.Text <> '' then // <>: Hvis '' IKKE er gældende
antal := StrToInt(Number.Text);
end;
procedure TForm2.OpenForm3(Sender: TObject);
begin
Form3.ShowModal;
end;
end.
---------------------------------
-----Unit 3-----
unit Unit3;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm3 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
Edit6: TEdit;
Button1: TButton;
procedure Save(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form3: TForm3;
implementation
{$R *.dfm}
uses Unit2;
procedure TForm3.Save(Sender: TObject);
var
t01,t02,t03,t04,t05,t06 : string;
begin
// Gemme inputfelter i string-variabler
t01 := Edit1.Text;
t02 := Edit2.Text;
t03 := Edit3.Text;
t04 := Edit4.Text;
t05 := Edit5.Text;
t06 := Edit6.Text;
// Indsætte tekst fra felter i ListBox1 boxen
if antal = 1 then
begin
Unit2.Form2.ListBox1.Items[0] := t01;
Unit2.Form2.ListBox1.Items.Delete(1);
Unit2.Form2.ListBox1.Items.Delete(2);
Unit2.Form2.ListBox1.Items.Delete(3);
Unit2.Form2.ListBox1.Items.Delete(4);
Unit2.Form2.ListBox1.Items.Delete(5);
end
else if antal = 2 then
begin
Unit2.Form2.ListBox1.Items[0] := t01;
Unit2.Form2.ListBox1.Items[1] := t02;
Unit2.Form2.ListBox1.Items.Delete(2);
Unit2.Form2.ListBox1.Items.Delete(3);
Unit2.Form2.ListBox1.Items.Delete(4);
Unit2.Form2.ListBox1.Items.Delete(5);
end
else if antal = 3 then
begin
Unit2.Form2.ListBox1.Items[0] := t01;
Unit2.Form2.ListBox1.Items[1] := t02;
Unit2.Form2.ListBox1.Items[2] := t03;
Unit2.Form2.ListBox1.Items.Delete(3);
Unit2.Form2.ListBox1.Items.Delete(4);
Unit2.Form2.ListBox1.Items.Delete(5);
end
else if antal = 4 then
begin
Unit2.Form2.ListBox1.Items[0] := t01;
Unit2.Form2.ListBox1.Items[1] := t02;
Unit2.Form2.ListBox1.Items[2] := t03;
Unit2.Form2.ListBox1.Items[3] := t04;
Unit2.Form2.ListBox1.Items.Delete(4);
Unit2.Form2.ListBox1.Items.Delete(5);
end
else if antal = 5 then
begin
Unit2.Form2.ListBox1.Items[0] := t01;
Unit2.Form2.ListBox1.Items[1] := t02;
Unit2.Form2.ListBox1.Items[2] := t03;
Unit2.Form2.ListBox1.Items[3] := t04;
Unit2.Form2.ListBox1.Items[4] := t05;
Unit2.Form2.ListBox1.Items.Delete(5);
end
else if antal = 6 then
begin
Unit2.Form2.ListBox1.Items[0] := t01;
Unit2.Form2.ListBox1.Items[1] := t02;
Unit2.Form2.ListBox1.Items[2] := t03;
Unit2.Form2.ListBox1.Items[3] := t04;
Unit2.Form2.ListBox1.Items[4] := t05;
Unit2.Form2.ListBox1.Items[5] := t06;
end;
// Lukke vinduet
Close;
end;
end.
------------------------------------------------
Formene ser således ud:
http://i48.tinypic.com/2mcftw8.jpg
http://i49.tinypic.com/2vwbj1y.jpg
Den tekst der står i felterne 1-6 i Form3 er noget der står i forvejen.
Dette er formålet med programmet:
Man indtaster et tal fra 1-6 i feltet til venstre for Edit knappen i Form2. Dernæst går man ind i Edit (dvs Form3), og hvis man fx indtastede 3, så indtaster man noget tekst (eller lader være, der står jo som sagt noget tekst i forvejen) i felterne 1-3 og trykker på Save. Når man trykker på Save, så går proceduren "TForm3.Save" i gang. Nu er variablen antal lig 3, og vi kigger så på "if antal = 3 then". I kan se, at items 0-2 i listboxen udfyldes med teksten fra edit felterne 1-3. Og det virker skam fint.
Lad os nu antage at brugeren ikke vil have 3, men 6 i stedet for - antal bliver 6 og items 0-5 bliver udfyldt med teksterne fra edit felterne 1-6. Virker også fint.
Lad os nu antage at brugeren ikke vil have 6 mere, men fx 2. antal = 2. Kig nu på hvad der sker for "if antal = 2 then". Cellerne 2-5 skal slettes, fordi det kun er cellerne 0-1 der skal vises (da brugeren har valgt antal = 2).
Problemet opstår i, at det ikke er alle de celler, man har sagt der skal slettes, der bliver slettet. Det er virkelig mærkeligt, synes jeg.
Et eksempel: start programmet, tast 6, tryk Edit, tryk Save. Indtast nu 1 i stedet for 6, tryk Edit, tryk Save. Nu er der både 01, 03 og 05 i listen, hvor det kun skulle være 01.
Hvad sker der?
