listbox1有N行字符串. listbox2里有400行字符串.
我要把listbox1的每一行 都跟listbox2的400行进行比较.(listbox400行是变化的)1.例如:按钮button1按下:  如果listbox1的第8行=listbox2.items.string[n] 就得到listbox1的选中行号8并break.   
2.button1再次按下listbox1就从第9行起开始比较,有相等结果就就得到listbox1的选中行号并break;以此类推. 如果listbox1的所有行都比较不等就退出循环.  比较完后,button1再按下又循环从第一行开始比较

解决方案 »

  1.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        ListBox1: TListBox;
        ListBox2: TListBox;
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
         gIndex : integer;
    implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    var
      i : integer;
    begin
      for i := gIndex to ListBox1.Items.Count -1 do
      begin
        if  ListBox2.Items.IndexOf(Trim(ListBox1.Items.Strings[i])) <> -1 then
        begin
          ShowMessage('ListBox1中的 '+ListBox1.Items.Strings[i]+' 在listbox2中存在');
          gIndex := i + 1;
          break;
        end;
      end;end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      gIndex := 0;
    end;end.
      

  2.   

    也可以这样,不要全局变量:
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        ListBox1: TListBox;
        ListBox2: TListBox;
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
    implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    var
      i : integer;
    begin
      if ListBox1.ItemIndex = -1 then ListBox1.ItemIndex := 0;
      for i := ListBox1.ItemIndex to ListBox1.Items.Count -1 do
      begin
        if  ListBox2.Items.IndexOf(Trim(ListBox1.Items.Strings[i])) <> -1 then
        begin
          ShowMessage('ListBox1中的 '+ListBox1.Items.Strings[i]+' 在listbox2中存在');
          ListBox1.ItemIndex := i+1;
          break;
        end;
      end;end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      ListBox1.ItemIndex := 0;
    end;end.
      

  3.   

    这个更加合适点。呵呵unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        ListBox1: TListBox;
        ListBox2: TListBox;
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
    implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    var
      i : integer;
    begin
      if ListBox1.ItemIndex = -1 then ListBox1.ItemIndex := 0;
      for i := ListBox1.ItemIndex to ListBox1.Items.Count -1 do
      begin
        if  ListBox2.Items.IndexOf(Trim(ListBox1.Items.Strings[i])) <> -1 then
        begin
          ShowMessage('ListBox1中的 '+ListBox1.Items.Strings[i]+' 在listbox2中存在');
          if i = ListBox1.Count -1 then ListBox1.ItemIndex := 0 else
          ListBox1.ItemIndex := i+1;
          break;
        end;
      end;   if i = ListBox1.Count then ListBox1.ItemIndex := 0;end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      ListBox1.ItemIndex := 0;
    end;end.
      

  4.   

    非常感谢,但是我需要知道得到结果,也要得到listbox2所在的行。  还得麻烦下。
      

  5.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        ListBox1: TListBox;
        ListBox2: TListBox;
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    var
      i : integer;
    begin
      if ListBox1.ItemIndex = -1 then ListBox1.ItemIndex := 0;
      for i := ListBox1.ItemIndex to ListBox1.Items.Count -1 do
      begin
        if  ListBox2.Items.IndexOf(Trim(ListBox1.Items.Strings[i])) <> -1 then
        begin
           ListBox2.ItemIndex := ListBox2.Items.IndexOf(Trim(ListBox1.Items.Strings[i]));
          ShowMessage('ListBox1中的 '+ListBox1.Items.Strings[i]+' 在listbox2的第'+IntTostr(ListBox2.Itemindex+1)+'行');
          if i = ListBox1.Count -1 then ListBox1.ItemIndex := 0 else
          ListBox1.ItemIndex := i+1;
          break;
        end;
      end;   if i = ListBox1.Count then ListBox1.ItemIndex := 0;
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      ListBox1.ItemIndex := 0;end;end.