列表框(列表错误)

时间:2014-01-28 23:08:19

标签: delphi listbox

我正在为我的朋友做一个抽奖活动。一切都很顺利但是当我删除一个值时,结果却在改变......请帮助我!

示例:

列表框;

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

删除第3行后:

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

我想要的是什么:

  • 1-A
  • 2-C
  • 3-F
  • 4小时
  • 5-J
  • 6-G

以下是代码:

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.

2 个答案:

答案 0 :(得分:0)

listbox1.Items.Add(IntToStr(b) + ' ' + edit1.Text); 而不是直接添加到列表框,将IntToStr(b)edit1.Text分开存储在两个字符串列表中,并从字符串列表中填充列表框数据。

还从第二个stringlist中执行删除删除相应的索引,并在listbox中重新填充

或者您可以将edit1.Text存储在字符串列表中,并从列表框中删除您从列表框中删除的字符串。并使用索引+字符串组合填充列表框中的数据.....

答案 1 :(得分:0)

我会在这里使用虚拟列表框。这些是基本步骤:

  1. 将数据存储在GUI控件以外的容器中,例如字符串列表。无论如何,这都是很好的做法。
  2. Style设置为lbVirtual
  3. OnData事件处理程序上实现列表。它需要返回由索引和容器中的基础项组成的字符串。
  4. 删除项目时,将其从字符串列表容器中删除,并在列表框中调用Invalidate以强制执行绘制周期。该绘制周期将通过调用OnData来请求新值,并且您的代码可以提供更新的文本。
  5. 每当修改基础容器时,您必须通过设置列表框控件的Count属性让控件知道它显示的项目数。
  6. 这是一个非常简单的例子:

    Pascal单位

    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