Solved

No Duplicates in combobox

Posted on 2003-10-25
11
598 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Virtuailstring tree compare node issue 14 117
how to center only a line in richedit? 4 58
update joined tables 2 46
Slow Restore if incremental backups using RDiff.exe 4 21
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

803 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