Solved

No Duplicates in combobox

Posted on 2003-10-25
11
600 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 100 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi cmd execution 6 66
Delphi: how to send PJL commands to printer 3 108
Print Graphic and Text to Epson TM-T88v 12 367
Firemonkey android show image from resource ? 1 46
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

791 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