drama22
asked on
add combobox item based on numbers
i have combobox that have items like
1
2
3
4
i want to add items based on number so if last item = 4 then add new item with
5
6
7
8
and so on
current code fail to add with same approach i needed
1
2
3
4
i want to add items based on number so if last item = 4 then add new item with
5
6
7
8
and so on
function Tpnlsettings.findTheme(tName : string): Integer;
var
I: Integer;
begin
Result := -1;
for I := 0 to cmbThemes.Items.Count - 1 do
begin
if cmbThemes.Items.Strings[i] = tName then
begin
Result := i;
Break;
end;
end;
end;
procedure Tpnlsettings.btnAddThemeClick(Sender: TObject);
var
I: Integer;
begin
i := findTheme(intTostr(cmbThemes.items.Count));
if i <> -1 then
begin
cmbThemes.ItemIndex := i;
end else
begin
cmbThemes.Items.Add(intTostr(cmbThemes.items.Count+1));
cmbThemes.ItemIndex := cmbThemes.items.Count-1;
end;
objectEnabled(btnThemeChange,True);
end;
current code fail to add with same approach i needed
ASKER
I want first check if value exist or not if exist break do nothing if not start adding +1 which I try in my function
That is the unclear part for me. Does the user type the new value?
The list in your description contains only numbers. Will the user type new item only in numbers or can it be something else? My current suggestion doesn't work if the list contains anything else than numbers.
Could you describe the broader context for your question, so I could better assist you.
The list in your description contains only numbers. Will the user type new item only in numbers or can it be something else? My current suggestion doesn't work if the list contains anything else than numbers.
Could you describe the broader context for your question, so I could better assist you.
ASKER
the user will click a button to add a new number this number that will be click is the lastnumber + 1 inside the combobox
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
when want to add item not existing before...
s := edtNum.Text;
i := cmbThemes.Items.IndexOf(s); //find index of number in list...
if i < 0 then //not exist
i := cmbThemes.Items.Add(s);
...
ASKER
the goal is i want to do auto increment to items inside combobox based on my code
i dont want use edit box
i only want to use button to add item with auto increment so if combobox have item = 1 then when i click a button insert item that equals = 2 and so on
i dont want use edit box
i only want to use button to add item with auto increment so if combobox have item = 1 then when i click a button insert item that equals = 2 and so on
Can you explain how my first code example failed in that?
If I understand correctly: If new items are created without asking user (what new number), there is no need to check if item already exists, cause you can simply add highest used value + 1 (as in my first suggestion). Checking if item already exists is needed only when user can type whatever numbers (my second suggestion).
If you would like to have precise answer to your question, you need to give much broader and more exact description on how your application should behave. At least I have a lot of difficulties on understanding what it is exactly that you want. The coding task it self seems to be very simple.
If I understand correctly: If new items are created without asking user (what new number), there is no need to check if item already exists, cause you can simply add highest used value + 1 (as in my first suggestion). Checking if item already exists is needed only when user can type whatever numbers (my second suggestion).
If you would like to have precise answer to your question, you need to give much broader and more exact description on how your application should behave. At least I have a lot of difficulties on understanding what it is exactly that you want. The coding task it self seems to be very simple.
ASKER
your code wont compile in my delphi version iam using xe7 . undefined pred if you can do the same with my current code will be helpful
I find it difficult to understand what you are asking. I assume you are requesting following functionality.
1) find the highest (integer) value from list strings
2) add new value, that is +1 bigger than the highest value
I would suggest following
Open in new window