Link to home
Start Free TrialLog in
Avatar of drama22
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

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;

Open in new window


current code fail to add with same approach i needed
Avatar of olmy
olmy

Hi drama22

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

procedure TForm1.btn_SetupClick(Sender: TObject);
begin
  (* values for testing *)
  cmbThemes.Items.Add( '1' );
  cmbThemes.Items.Add( '8' );
  cmbThemes.Items.Add( '3' );
  cmbThemes.Items.Add( '4' );
end;

function TForm1.FindTheHighestValue( aList : TStrings ) : integer;
var ii : word;
begin
  result := 0;

  if aList.count > 0 then
  for ii := 0 to pred( aList.count ) do
    if result < StrToIntDef( aList.Strings[ ii ], 0 ) then
      result := StrToIntDef( aList.Strings[ ii ], 0 );
end;(* FindTheHighestValue *)

procedure TForm1.btn_AddNewClick(Sender: TObject);
begin
  cmbThemes.items.add( IntToStr( FindTheHighestValue( cmbThemes.Items ) + 1 ));
  cmbThemes.ItemIndex := pred( cmbThemes.Items.count );
end;

Open in new window

Avatar of drama22

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.
Avatar of drama22

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
Avatar of olmy
olmy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sinisa Vuk
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);
...

Open in new window

Avatar of drama22

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
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.
Avatar of drama22

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