Brandon
01-16-2012, 01:43 AM
I have a form that fills an array when an item is clicked in a list box.. How can I ensure that the array does not have duplicates? I've tried the code below.
What it does is set the size of Food to the number of items in the listbox.. then looks for a blank spot in the array to add the food too if the array does not already contain that food.. But it only works on the first try! After 2 clicks, it starts ignoring the rules and adding to the array anyway :S So.. How can I ensure only unique values get added?
Ohh and how can I delete values from the array?
begin
SetLength(Food, FoodBox.Items.Count);
K:= 0;
For I := FoodBox.Items.Count - 1 downto 0 do
begin
if FoodBox.Selected[I] then
For J:= 0 To High(Food) do
if (Food[J] = '') then
begin
For K:= 0 To High(Food) do
if Food[K] <> FoodBox.ITEMS.Strings[I] then
Food[J]:= FoodBox.Items.Strings[I];
break;
end;
end;
K:= 0;
For I:= 0 To High(Food) do
if (Food[I] <> '') then
K:= K + 1;
SetLength(Food, K);
end;
What it does is set the size of Food to the number of items in the listbox.. then looks for a blank spot in the array to add the food too if the array does not already contain that food.. But it only works on the first try! After 2 clicks, it starts ignoring the rules and adding to the array anyway :S So.. How can I ensure only unique values get added?
Ohh and how can I delete values from the array?
begin
SetLength(Food, FoodBox.Items.Count);
K:= 0;
For I := FoodBox.Items.Count - 1 downto 0 do
begin
if FoodBox.Selected[I] then
For J:= 0 To High(Food) do
if (Food[J] = '') then
begin
For K:= 0 To High(Food) do
if Food[K] <> FoodBox.ITEMS.Strings[I] then
Food[J]:= FoodBox.Items.Strings[I];
break;
end;
end;
K:= 0;
For I:= 0 To High(Food) do
if (Food[I] <> '') then
K:= K + 1;
SetLength(Food, K);
end;