Recent

Author Topic: Combobox and additem?  (Read 3745 times)

Jvan

  • Full Member
  • ***
  • Posts: 181
Combobox and additem?
« on: May 18, 2021, 01:15:09 am »
I have this code:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.  
  4.   Combobox1.AddItem('Item A', TObject('A'));
  5.   Combobox1.AddItem('Item B', TObject('B'));
  6.   Combobox1.AddItem('Item C', TObject('C'));
  7.  
  8. end;
  9.  

How to get the secondary value when the selected item changes? and how to use the secondary value to change the selected item?

jamie

  • Hero Member
  • *****
  • Posts: 4582
Re: Combobox and additem?
« Reply #1 on: May 18, 2021, 01:45:36 am »
combobox.Items.IndexOfObject('?');

and when selection has been made...

Combobox.ItemIndex.

etc
The only true wisdom is knowing you know nothing

Jvan

  • Full Member
  • ***
  • Posts: 181
Re: Combobox and additem?
« Reply #2 on: May 18, 2021, 01:59:01 am »
combobox.Items.IndexOfObject('?');

and when selection has been made...

Combobox.ItemIndex.

etc

I want to get A, B or C when its item is selected. And select a item by code using A, B and C.
Code: Pascal  [Select][+][-]
  1.  
  2. procedure TForm1.Button1Click(Sender: TObject);
  3. begin
  4.  
  5.   ShowMessage(Combobox1.Items.Objects[Combobox1.ItemIndex]);
  6.  
  7. end;
  8.  
« Last Edit: May 18, 2021, 02:01:44 am by Jvan »

jamie

  • Hero Member
  • *****
  • Posts: 4582
Re: Combobox and additem?
« Reply #3 on: May 18, 2021, 02:25:49 am »
Hmm
I would of thought that to be enough info for you..

Code: Pascal  [Select][+][-]
  1.  
  2. ShowMessage(Combobox.Items[Combobox.Items.IndexOfObject('?')]);
  3.  
The only true wisdom is knowing you know nothing

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 932
    • Lebeau Software
Re: Combobox and additem?
« Reply #4 on: May 18, 2021, 02:53:51 am »
I want to get A, B or C when its item is selected.

You are close.  Since you are storing pointers to string literals, you just need to type-cast the stored TObject pointers to PChar:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.ComboBox1Change(Sender: TObject);
  2. var
  3.   Index: Integer;
  4. begin
  5.   Index := ComboBox1.ItemIndex;
  6.   if Index <> -1 then
  7.     ShowMessage(PChar(Combobox1.Items.Objects[Index]))
  8.   else
  9.     ShowMessage('Nothing Selected');
  10. end;
  11.  

And select a item by code using A, B and C.

That is a little trickier.  First instinct may be to use IndexOfObject(), like this:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   ComboBox1.ItemIndex := ComboBox1.Items.IndexOfObject(TObject('A'));
  4. end;
  5.  

However, as this is merely comparing pointers, this will only work when storing and searching string literals if the compiler merges duplicate string literals together so they have the same memory address.  This won't work if you use a String variable instead for the search, as the pointers will not match:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   S: String;
  4. begin
  5.   S := ...;
  6.   ComboBox1.ItemIndex := ComboBox1.Items.IndexOfObject(TObject(PChar(S)));
  7. end;
  8.  

In which case, you would have to loop and compare the stored string literals manually, eg:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   S: String;
  4.   Index, I: Integer;
  5. begin
  6.   S := ...;
  7.   Index := -1;
  8.   for I := 0 to ComboBox1.Items.Count-1 do
  9.   begin
  10.     if S = PChar(ComboBox1.Items.Objects[I]) then
  11.     begin
  12.       Index := I;
  13.       Break;
  14.     end;
  15.   end;
  16.   ComboBox1.ItemIndex := Index;
  17. end;
  18.  

That being said, I would not recommend storing actual strings in the Objects[] list at all.  Better to use a separate TStringList instead:

Code: Pascal  [Select][+][-]
  1. private
  2.   Values: TStringList;
  3.  
  4. procedure TForm1.FormCreate(Sender: TObject);
  5. var
  6.   I: Integer;
  7. begin
  8.   Values := TStringList.Create;
  9.   Values.Add('A');
  10.   Values.Add('B');
  11.   Values.Add('C');
  12.  
  13.   Combobox1.Items.Add('Item A');
  14.   Combobox1.Items.Add('Item B');
  15.   Combobox1.Items.Add('Item C');
  16. end;
  17.  
  18. procedure TForm1.FormDestroy(Sender: TObject);
  19. begin
  20.   Values.Free;
  21. end;
  22.  
  23. procedure TForm1.ComboBox1Change(Sender: TObject);
  24. var
  25.   Index: Integer;
  26. begin
  27.   Index := ComboBox1.ItemIndex;
  28.   if Index <> -1 then
  29.     ShowMessage(Values[index])
  30.   else
  31.     ShowMessage('Nothing Selected');
  32. end;
  33.  
  34. procedure TForm1.Button1Click(Sender: TObject);
  35. begin
  36.   ComboBox1.ItemIndex := Values.IndexOf('A');
  37. end;
  38.  
« Last Edit: May 18, 2021, 03:20:21 am by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Jvan

  • Full Member
  • ***
  • Posts: 181
Re: Combobox and additem?
« Reply #5 on: May 18, 2021, 03:05:08 am »
I have this error:

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 932
    • Lebeau Software
Re: Combobox and additem?
« Reply #6 on: May 18, 2021, 03:18:47 am »
I have this error:

Using which code exactly?
« Last Edit: May 18, 2021, 03:22:24 am by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Jvan

  • Full Member
  • ***
  • Posts: 181
Re: Combobox and additem?
« Reply #7 on: May 18, 2021, 03:30:37 am »
This:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ComboBox1Change(Sender: TObject);
  2. var
  3.   Index: Integer;
  4. begin
  5.   Index := ComboBox1.ItemIndex;
  6.   if Index <> -1 then
  7.     ShowMessage(PChar(Combobox1.Items.Objects[Index]))
  8.   else
  9.     ShowMessage('Nothing Selected');
  10. end;
  11.  
  12.  

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 932
    • Lebeau Software
Re: Combobox and additem?
« Reply #8 on: May 18, 2021, 05:54:30 pm »
All the more reason not to stuff string literals into the Objects[] property at all.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

TinyPortal © 2005-2018