2014-01-28 73 views
0

嗨我爲我的朋友做一個抽獎程序。一切都很好,但當我刪除一個值,結果正在改變...請幫助我!列表框(列表錯誤)

實施例:

列表框;

  • 1-A
  • 2-C
  • 3-B
  • 4-F
  • 5-H
  • 6-J

刪除線3後:

  • 1-A
  • 2-C
  • 4-F
  • 5-H
  • 6-J
  • 6克

我想要的:

  • 1-a
  • 2-c
  • 3-F
  • 4-H
  • 5-J
  • 6克

下面是代碼:

unit Unit1; 

interface 

uses 
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
    Dialogs, StdCtrls, ExtCtrls; 

type 
    TForm1 = class(TForm) 
    ListBox1: TListBox; 
    Label1: TLabel; 
    ComboBox1: TComboBox; 
    Edit1: TEdit; 
    Button1: TButton; 
    Label2: TLabel; 
    ComboBox2: TComboBox; 
    Label3: TLabel; 
    Button2: TButton; 
    Edit2: TEdit; 
    Edit3: TEdit; 
    Edit4: TEdit; 
    Edit5: TEdit; 
    Edit6: TEdit; 
    Button3: TButton; 
    Button4: TButton; 
    SaveDialog1: TSaveDialog; 
    OpenDialog1: TOpenDialog; 
    Button5: TButton; 
    Button6: TButton; 
    procedure Button1Click(Sender: TObject); 
    procedure Button2Click(Sender: TObject); 
    procedure FormCreate(Sender: TObject); 
    procedure Button3Click(Sender: TObject); 
    procedure Button4Click(Sender: TObject); 
    procedure Button5Click(Sender: TObject); 
    procedure Button6Click(Sender: TObject); 

    private 
    { Private declarations } 
    public 
    { Public declarations } 
    end; 

var 
    Form1: TForm1; 
    b,sayac:integer; 
    sonkayit,deneme:integer; 
implementation 

{$R *.dfm} 

procedure TForm1.FormCreate(Sender: TObject); 
begin 
sayac:=0; 

listbox1.MultiSelect:=true; 
end; 

procedure TForm1.Button1Click(Sender: TObject); 
begin 
sayac:=sayac+1; 

b:=listbox1.Count + 1; 

listbox1.Items.Add(IntToStr(b) + ' ' + edit1.Text); 
edit1.Text:=''; 
end; 

procedure TForm1.Button2Click(Sender: TObject); 
var 
a:integer; 
kisi:string; 

begin 
Randomize; 
a:=Random(b); 
kisi:= listbox1.Items.Strings[a]; 
edit2.Text:=(kisi); 

if combobox1.ItemIndex=0 then 
begin 
edit2.Visible:=true; 
edit3.Visible:=false; 
edit4.Visible:=false; 
edit5.Visible:=false; 
edit6.Visible:=false; 
end; 

if combobox1.ItemIndex=1 then 
begin 
edit2.Visible:=true; 
edit3.Visible:=true; 
edit4.Visible:=false; 
edit5.Visible:=false; 
edit6.Visible:=false; 
end; 

if combobox1.ItemIndex=2 then 
begin 
edit2.Visible:=true; 
edit3.Visible:=true; 
edit4.Visible:=true; 
edit5.Visible:=false; 
edit6.Visible:=false; 
end; 

if combobox1.ItemIndex=3 then 
begin 
edit2.Visible:=true; 
edit3.Visible:=true; 
edit4.Visible:=true; 
edit5.Visible:=true; 
edit6.Visible:=false; 
end; 

if combobox1.ItemIndex=4 then 
begin 
edit2.Visible:=true; 
edit3.Visible:=true; 
edit4.Visible:=true; 
edit5.Visible:=true; 
edit6.Visible:=true; 
end; 
end; 

procedure TForm1.Button3Click(Sender: TObject); 
begin 
savedialog1.FileName:='çekiliş'; 
if savedialog1.Execute then 
begin 
listbox1.Items.SaveToFile(savedialog1.FileName + '.txt'); 
end; 
end; 

procedure TForm1.Button4Click(Sender: TObject); 
begin 
if opendialog1.Execute then 
begin 
listbox1.Items.LoadFromFile(opendialog1.FileName); 
end; 
end; 

procedure TForm1.Button5Click(Sender: TObject); 
begin 
listbox1.DeleteSelected; 
end; 

procedure TForm1.Button6Click(Sender: TObject); 
begin 
listbox1.Clear; 
end; 

end. 
+1

使用虛擬列表。或重新填充它。該計劃正在做你所說的。你不能指望程序讀你的想法和重新編號的項目。或者使用網格。 –

+0

你做了這個listbox1.Items.Add(IntToStr(b)+''+ edit1.Text);.重新編號。或者更好的是在其他地方疼痛,然後在有變化時清除更新列表框。 ListBox不是您的要求的好選擇。 –

+1

或者自己繪製ListBox並動態繪製數字,而不是將它們存儲在ListBox本身中。只存儲自己的實際編輯值。 –

回答

0

listbox1.Items.Add(IntToStr(b) + ' ' + edit1.Text); ,而不是直接加入到列表框,存儲在兩個字符串列表中分別輸入IntToStr(b)edit1.Text,並從字符串列表中填充列表框數據。

也進行刪除第二的StringList刪除對應的索引,並在列表框

重新填充,也可以直接存儲edit1.Text中的StringList,並刪除您從列表框中刪除StringList的字符串。並使用索引+字符串組合填充列表框中的數據.....

0

我會在這裏使用虛擬列表框。這些是基本步驟:

  1. 將數據存儲在GUI控件以外的容器中,例如字符串列表。無論如何,這是一個很好的做法。
  2. Style設置爲lbVirtual
  3. OnData實施該列表的事件處理程序。它需要返回由索引和容器中的基礎項目組成的字符串。
  4. 當您刪除一個項目時,將其從字符串列表容器中刪除並在列表框上調用Invalidate來強制執行一個繪畫循環。通過調用OnData,該繪畫循環將請求新值,並且您的代碼可以提供更新的文本。
  5. 每當底層容器被修改時,必須通過設置列表框控件的Count屬性來讓控件知道它顯示的項目數。

這是一個很簡單的例子:

帕斯卡爾單元

unit Unit1; 

interface 

uses 
    SysUtils, Classes, Controls, StdCtrls, Forms; 

type 
    TForm1 = class(TForm) 
    List: TListBox; 
    Delete: TButton; 
    procedure FormCreate(Sender: TObject); 
    procedure ListData(Control: TWinControl; Index: Integer; var Data: string); 
    procedure DeleteClick(Sender: TObject); 
    private 
    FItems: TStringList; 
    end; 

implementation 

{$R *.dfm} 

procedure TForm1.FormCreate(Sender: TObject); 
var 
    c: Char; 
begin 
    FItems := TStringList.Create; 
    for c := 'a' to 'z' do 
    FItems.Add(c); 
    List.Count := FItems.Count; 
end; 

procedure TForm1.ListData(Control: TWinControl; Index: Integer; 
    var Data: string); 
begin 
    Data := Format('%d %s', [Index+1, FItems[Index]]); 
end; 

procedure TForm1.DeleteClick(Sender: TObject); 
var 
    Index: Integer; 
begin 
    for Index := FItems.Count-1 downto 0 do 
    if List.Selected[Index] then 
     FItems.Delete(Index); 
    List.Count := FItems.Count; 
    List.Invalidate; 
end; 

end. 

關聯的表單文件

object Form1: TForm1 
    Left = 0 
    Top = 0 
    Caption = 'Form1' 
    ClientHeight = 303 
    ClientWidth = 307 
    OnCreate = FormCreate 
    PixelsPerInch = 96 
    TextHeight = 13 
    object List: TListBox 
    Left = 8 
    Top = 8 
    Width = 201 
    Height = 287 
    Style = lbVirtual 
    Anchors = [akLeft, akTop, akRight, akBottom] 
    MultiSelect = True 
    TabOrder = 0 
    OnData = ListData 
    end 
    object Delete: TButton 
    Left = 224 
    Top = 8 
    Width = 75 
    Height = 23 
    Anchors = [akTop, akRight] 
    Caption = 'Delete' 
    TabOrder = 1 
    OnClick = DeleteClick 
    end 
end