Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Listbox Duplicates

Status
Not open for further replies.

Destruktion

Programmer
Mar 18, 2011
28
GB
I have two listboxes and a generate button.

Listbox1 is where i generate and add new random numbers.

Listbox2 is the main list where items from Listbox1 are added - if Listbox2 already contains items that are generated in Listbox1 they are not added.

Now to the problem, If i click the generate button and Listbox2 already contains a number found in Listbox1, nothing happens (expected), so I have to keep clicking the generate button until a new number is found and added.

I need to add something like a repeat..until statement to make sure that whenever I click that button a number is added, once it runs out of numbers from Listbox1 to add show a message to say that no more can be added.

The problem is whenever I tried implementing the repeat statement, the Listbox1 went crazy looking for numbers that are not a duplicate in Listbox2 - of course it ran out of numbers and got stuck in a infinite loop.

Here is the code I came up with:

Code:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    Label1: TLabel;
    ListBox1: TListBox;
    Label2: TLabel;
    ListBox2: TListBox;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function ListBoxItemExists(ListBox: TListBox; const Item: string): Boolean;
begin
  Result := ListBox.Items.IndexOf(Item) >= 0;
end;

procedure ShuffleStrings(sl : TStrings; nIntensity: integer);
var
  n1, n2, n3 : integer;
  s1         : string;
begin
  if(0 = nIntensity)then
  begin
    nIntensity := sl.Count;
  end else
  if(nIntensity > sl.Count)then
  begin
    nIntensity := sl.Count;
  end;

  Randomize;

  for n1 := 1 to nIntensity do
  begin
    n2 := Random( nIntensity );
    n3 := Random( nIntensity );

    s1             := sl.Strings[n3];
    sl.Strings[n3] := sl.Strings[n2];
    sl.Strings[n2] := s1;
  end;
end;

function ListBoxGenerateAndCompare(List, ListToCompare: TListBox; MinGen, MaxGen, MinRange, MaxRange: Integer; DoRandomize: Boolean): Boolean;
var
  SL: TStringList;
  i: Integer;
begin
  try
    SL:= TStringList.Create;
    try
      for i:= MinGen to MaxGen do
        SL.Add(IntToStr(i));

      if DoRandomize then ShuffleStrings(SL, 0);
      List.Items.Assign(SL);
    finally
      SL.Free;
    end;
  except on exception do
    //
  end;

  try
    SL:= TStringList.Create;
    try
      for i:= MinRange to MaxRange do
      begin
        if ListBoxItemExists(ListToCompare, List.Items.Strings[i]) then
        begin
          Result:= False
        end else
        begin
          if not ListBoxItemExists(ListToCompare, List.Items.Strings[i]) then
          begin
            SL.Add(List.Items.Strings[i]);
            ListToCompare.Items.AddStrings(SL);

            Result:= True;
          end;
        end;
      end;
    finally
      SL.Free;
    end;
  except on exception do
    //
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  Count: Integer;
begin
  Count:= ListBox2.Items.Count;

  // THIS SHOULD REPEAT UNTIL A NUMBER IS ADDED TO LISTBOX2,
  // IF there are no more numbers available show a message to say so

  ListBoxGenerateAndCompare(ListBox1, ListBox2, 1, 10, 0, 0, True);

  if ListBox2.Items.Count = Count + 1 then
    ShowMessage('New Number was added')
  else
    ShowMessage('No new number was added');
end;

end.

Appreciate some help thanks.
 
Try this:
Code:
for i:= MinRange to MaxRange do
  begin
    if ListBoxItemExists(ListToCompare, List.Items.Strings[i]) > -1 then
      begin
        SL.Add(List.Items.Strings[i]);
        ListToCompare.Items.AddStrings(SL);
        Result:= True;
      end
    else
      Result := false;
  end;

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
No edit function...

This:
Code:
 if ListBoxItemExists(ListToCompare, List.Items.Strings[i]) > -1 then

Should be this:
Code:
if ListToCompare.Items.IndexOf(List.Items.Strings[i]) > -1 then

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top