?
Solved

No Duplicates in combobox

Posted on 2003-10-25
11
Medium Priority
?
614 Views
Last Modified: 2010-04-05
There ara many items in combobox but I want to make No Duplicates for these itmes...

Fore example if the itemes are (10,20,10,30,50,20) ...
I want to filter them to (10,20,30,50)
0
Comment
Question by:sal1150
  • 3
  • 3
  • 2
  • +2
11 Comments
 
LVL 2

Expert Comment

by:ccrdude
ID: 9620565
Here's an example that copies all entries from listbox1 to listbox2, ignoring all duplicates.

procedure TForm1.Button1Click(Sender: TObject);
var sl: TStringList;
    i: integer;
begin
   sl := TStringList.Create;
   sl.Duplicates := dupIgnore;
   sl.Sorted := true;
   sl.AddStrings(listbox1.Items);
   listbox2.Items.AddStrings(sl);
   sl.Free;
end;

Important:
Duplicates only works for entries added *after* Duplicates was set to dupIgnore.
And Duplicated only works when the list is sorted (that's why I added the Sorted := true).
0
 
LVL 2

Accepted Solution

by:
alsantos earned 200 total points
ID: 9621776
You can use this code to add without duplicates.

procedure TForm1.Button1Click(Sender: TObject);
begin
  if ComboBox1.Items.IndexOf(Edit1.Text) < 0 then
    ComboBox1.Items.Add(Edit1.Text);
end;

and You can use this code to delete all duplicates in a ComboBox

procedure TForm1.Button2Click(Sender: TObject);
var
  Strs: TStrings;
  i: Integer;
begin
  Strs:=TStringList.Create;
  for i:=0 to ComboBox1.Items.Count-1 do
    if Strs.IndexOf(ComboBox1.Items[i]) < 0 then
      Strs.Add(ComboBox1.Items[i]);
  ComboBox1.Items := Strs;
end;

alsantos
0
 
LVL 2

Expert Comment

by:alsantos
ID: 9621784
Sorry, I forgot the "Strs.Free;" after the "ComboBox1.Items := Strs;" , at the end of code, before the end;

alsantos
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:sal1150
ID: 9621844
My values in array of integer... when I applay this code I recieve error '[Error] Unit1.pas(472): Incompatible types: 'TStrings' and 'String''

for I:1 to 72 do
 SL := TStringList.Create;
  SL.Sorted := true;
  SL.Duplicates := dupIgnore;
  SL.AddStrings(INTTOSTR(MYLISTr[I,3]));
  listcb.Items.Assign(SL);
  SL.Free;
0
 
LVL 2

Expert Comment

by:ccrdude
ID: 9622065
@davidhusselmann: as you can read in sal1150s comment above, the values are inside an array, not inside the combobox.
@sal1150: AddStrings works on a TStringList, but IntToStr returns only a single string from a single int. If you want a good answer, please specifcy your problem exactly. The way it is right now, everyone's just providing *any* bit of code that *could* apply to the situation, hoping it would get him some points ;)

But I don't see your connection between the array and the combobox yet. If you have a combobox, why store them in an additional array?


For the array, there are multiple methods.
The preferred one would be to write your own TIntegerList class that even stores them sorted and without duplicates. Or that at least returns a TIntegerList object that is sorted.
Or you could write a short algo to sort and filter out the dupes.
Or you could do the sorting and filtering while pasting the contents into the combobox, which is what I *guess* you want. If that doesn't fit your needs, please specify your task more detailed.

procedure IArrayToTStrings(ar: array of integer; strings: TStrings);
var i: integer;
begin
   strings.Clear;
   for i := Low(ar) to High(ar)
    do if strings.IndexOf(IntToStr(ar[i]))<0
     then strings.Add(IntToStr(ar[i]));
end;

procedure TForm1.Button1Click(Sender: TObject);
const myarray: array[0..10] of integer = (0,10,20,50,30,20,40,20,10,60,10);
begin
   Combobox1.Sorted := true;
   IArrayToTStrings(myarray,Combobox1.Items);
   // replace the myarray with your array, and Combobox1 with your TComboBox or TListBox
end;

0
 

Expert Comment

by:davidhusselmann
ID: 9622025
Here's an O(N) procedure that removes the duplicates from a sorted combo box without using any other objects:

procedure DeDupeCombo(Combo : TComboBox);
var i : Integer;
begin
  Combo.Sorted := true;
  i := 1;
  while i < Combo.Items.Count do
    if (Combo.Items[i] = Combo.Items[i-1]) then
      Combo.Items.Delete(i)
    else
      inc(i);
end;

David
0
 

Expert Comment

by:PGKalle
ID: 9622527
Why don't you use a set structure when copying? It filters your data automatically, and if you eclare it dynamically, you can even delete it afterwards, so that it doesn't waste any memory.

Kalle
0
 

Author Comment

by:sal1150
ID: 9625414
I USE alsantos CODE to add without duplicates.

procedure TForm1.Button1Click(Sender: TObject);
begin
  if ComboBox1.Items.IndexOf(Edit1.Text) < 0 then
    ComboBox1.Items.Add(Edit1.Text);
end;
+++++++++++++++++++++++++++++++++++++++++++++++++++
Now my problem is add the value of array in TStringGrid becuse when I ap[play
to add the value some of grid rows become empty...
I USE THIS CODE:-

procedure TForm1.mcdCBClick(Sender: TObject);
VAR I:INTEGER;
begin
FOR I:=1 TO 72  DO
   IF MYLISTR[I,3]=STRTOINT(LISTCB.Text)  THEN
   GRID1.Cells[0,I]:=inttostr(MYLISTr[I,1]);

{
WHERE 1-mylistr is array of integer with 72 rows
            2-grid is tstringgrid
            3-listcb is combobox which I make it without duplicates.
}
0
 
LVL 2

Expert Comment

by:alsantos
ID: 9626829
what you would like to do?

I don't know certainly what you would like to do...

   IF MYLISTR[I,3]=STRTOINT(LISTCB.Text)  THEN
   GRID1.Cells[0,I]:=inttostr(MYLISTr[I,1]);

but, this code "STRTOINT(LISTCB.Text)" convert to int the selected text. So, IF MYLISTR[I,3]=STRTOINT(LISTCB.Text)  test if mylistr[i,3] is equal to selected item on LISTCB(and only selected item, not all the items of LISTCB). if you would like to test if mylistr[i,3] is item (was inserted) of LISTCB, you use this: "IF LISTCB.Items.IndexOf(IntToStr(MYLISTR[I,3])) >= 0 THEN"

and this code "GRID1.Cells[0,I]:=inttostr(MYLISTr[I,1]);" you only write on the first column of the grid.

alsantos
0
 

Expert Comment

by:PGKalle
ID: 9630387
I also have problems with what you like to do. What you're actually doing is to copy the content of the first line of your ComboBox (the one prompted when the box is "closed") to the appropriate line of your StringGrid. If you want to copy all the box dependent on item-specific properties, you must query ComboBox properties different to *.Text (i.e. *.Items class). But I can't see the reason for checking the item first.

Try the following code: (generally adapted from ccrdude's posting)

procedure TForm1.Button1Click(Sender: TObject);
const length_of_my_list = 72;
var sl: TStringList;
    i: integer;
begin
   sl := TStringList.Create;
   sl.Duplicates := dupIgnore;
   sl.Sorted := true;

   //copy old content into stringlist:
   sl.AddStrings(ListCB.Items);

   //copy it into grid:
   for i := 1 to length_of_my_list
      do Grid1.Cells[0,i] := sl[i];

   sl.Free;
end;


That should work, but I still don't know why you want to have it in a StringGrid...

Kalle
0
 

Author Comment

by:sal1150
ID: 9632766
I use array of integer which has 72 rows ...and 3 colum
I use stringgrid to browse these array values...
I use combobox to filter stringgrid values...

===========================
I want to show all the array value in stringgrid where third colum of array=combobox1.text
==========================
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month14 days, 9 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question