add combobox item based on numbers

drama22
drama22 used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
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

Author

Commented:
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

Commented:
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.

Author

Commented:
the user will click a button to add a new number this number that will be click is the lastnumber + 1 inside the combobox
Commented:
Ok. Lets try once more. Still, I'm not confident that I understand the exact functionality you are looking for.

Replace previous btn_AddNewClick method with this one.

Coded functionality:
1) ask new theme number from the user
2) if new theme number already exists, select it. If it doesn't exist, add new theme number that is the highest used theme number + 1 and select it.

procedure TForm1.btn_AddNewClick(Sender: TObject);
var
  iHighestThemeNumber : word;
  iAskedThemeNumber : string;
  iThemeNumberFound : integer;
begin
  (* Find out the highest used theme number *)
  iHighestThemeNumber := FindTheHighestValue( cmbThemes.Items );

  (* Ask new theme number from the user. Suggest iTheHighestNumber + 1 as a default *)
  iAskedThemeNumber := InputBox( 'New theme', 'Type a number for the new theme', IntToStr( iHighestThemeNumber + 1 ));

  (* check if theme number already exists *)
  iThemeNumberFound := cmbThemes.items.IndexOf( iAskedThemeNumber );
  (* if it was not found -> add and select it *)
  if iThemeNumberFound = -1 then
  begin
    (* Always add iTheHighestNumber + 1 as new theme number *)
    cmbThemes.items.add( IntToStr( iHighestThemeNumber + 1 ));
    cmbThemes.ItemIndex := pred( cmbThemes.Items.count );
  end else
    (* if theme number was found, select it *)
    cmbThemes.ItemIndex := iThemeNumberFound;
end;

Open in new window

Sinisa VukSoftware architect
Top Expert 2012

Commented:
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

Author

Commented:
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

Commented:
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.

Author

Commented:
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial