Solved

No Duplicates in combobox

Posted on 2003-10-25
11
592 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
Comment Utility
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
Comment Utility
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
Comment Utility
Sorry, I forgot the "Strs.Free;" after the "ComboBox1.Items := Strs;" , at the end of code, before the end;

alsantos
0
 

Author Comment

by:sal1150
Comment Utility
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
Comment Utility
@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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Expert Comment

by:davidhusselmann
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now