Lazarus

Programming => LCL => Topic started by: Jvan on May 18, 2021, 01:15:09 am

Title: Combobox and additem?
Post by: Jvan 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?
Title: Re: Combobox and additem?
Post by: Jvan 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.  
Title: Re: Combobox and additem?
Post by: Remy Lebeau 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.  
Title: Re: Combobox and additem?
Post by: Jvan on May 18, 2021, 03:05:08 am
I have this error:
Title: Re: Combobox and additem?
Post by: Remy Lebeau on May 18, 2021, 03:18:47 am
I have this error:

Using which code exactly?
Title: Re: Combobox and additem?
Post by: Jvan 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.  
Title: Re: Combobox and additem?
Post by: Remy Lebeau on May 18, 2021, 05:54:30 pm
All the more reason not to stuff string literals into the Objects[] property at all.
TinyPortal © 2005-2018