Creating custom component lazarus / 31-LCL ---------------------------------------------------------------------- M[a]nny · 2012-12-06 18:52 Hi all, can anybody help me how to write own component based on TComboBox but new one would have these functions: Regular Items.Add would be replaced by this: Code: [Select]ComboBox1.Items.Add(s: string, id: integer); You would be also able to get selected id of selected item: Code: [Select]ComboBox1.Selected.Id; Or you would be able to set ID: Code: [Select]ComboBox1.SetId(15); // this will find item with id 15 and select it Is it possible? If yes, how? ---------------------------------------------------------------------- kpeters58 · 2012-12-06 19:20 There's no need for a custom component for your purposes: Look at the Items property of TCombobox: TStrings have support for attached objects via their PutObject/GetObject methods. You could simply stick your integer keys in there (or reference any other objects for that matter) Cheers, Kai ---------------------------------------------------------------------- M[a]nny · 2012-12-06 20:32 Thank you for your response. Excuse me, i am not such a good programmer... how can i use this method for my purpose? Can you write some example for me, please? ---------------------------------------------------------------------- kpeters58 · 2012-12-06 21:29 Make a form with 1 combobox and three buttons and wire the procedures below in and you will get the point (notice that you will have to use QWord instead of Integer if you use literals like I did in my example): Code: [Select]procedure TForm1.button_LoadClick(Sender: TObject); begin   combobox.AddItem('Chevrolet', TObject(17));   combobox.AddItem('Nissan',    TObject(22));   combobox.AddItem('Ford',      TObject(13));   combobox.AddItem('Toyota',    TObject(42));   combobox.AddItem('VW',        TObject(56));   //   combobox.ItemIndex    := 0;   button_Key.Enabled    := True;   button_String.Enabled := True; end; procedure TForm1.button_StringClick(Sender: TObject); var   index: Integer; begin   // get the 'key' for the string 'Toyota' (if present in combobox)   index := combobox.Items.IndexOf('Toyota');   if (index <> -1) then     ShowMessage('Key for Toyota: ' + IntToStr(QWord(combobox.Items.Objects[index])))   else     ShowMessage('String ' + QuotedStr('Toyota') + ' not found.'); end; procedure TForm1.button_KeyClick(Sender: TObject); var   index: Integer; begin   // see if we have a string in the combobox for key #13   index := combobox.Items.IndexOfObject(TObject(13));   if (index <> -1) then     ShowMessage('String for Key #13: ' + combobox.Items[index])   else     ShowMessage('Key #13 not found.'); end; ---------------------------------------------------------------------- Jvan · 2021-05-18 00:44 Quote from: kpeters58 on December 06, 2012, 10:29:04 pmMake a form with 1 combobox and three buttons and wire the procedures below in and you will get the point (notice that you will have to use QWord instead of Integer if you use literals like I did in my example): Code: [Select]procedure TForm1.button_LoadClick(Sender: TObject); begin   combobox.AddItem('Chevrolet', TObject(17));   combobox.AddItem('Nissan',    TObject(22));   combobox.AddItem('Ford',      TObject(13));   combobox.AddItem('Toyota',    TObject(42));   combobox.AddItem('VW',        TObject(56));   //   combobox.ItemIndex    := 0;   button_Key.Enabled    := True;   button_String.Enabled := True; end; procedure TForm1.button_StringClick(Sender: TObject); var   index: Integer; begin   // get the 'key' for the string 'Toyota' (if present in combobox)   index := combobox.Items.IndexOf('Toyota');   if (index <> -1) then     ShowMessage('Key for Toyota: ' + IntToStr(QWord(combobox.Items.Objects[index])))   else     ShowMessage('String ' + QuotedStr('Toyota') + ' not found.'); end; procedure TForm1.button_KeyClick(Sender: TObject); var   index: Integer; begin   // see if we have a string in the combobox for key #13   index := combobox.Items.IndexOfObject(TObject(13));   if (index <> -1) then     ShowMessage('String for Key #13: ' + combobox.Items[index])   else     ShowMessage('Key #13 not found.'); end; If instead of TObject(13), it is TObject('ABC'), what to do? ---------------------------------------------------------------------- bpranoto · 2021-05-18 04:39 Quote from: Jvan on January 19, 1970, 07:21:38 pmIf instead of TObject(13), it is TObject('ABC'), what to do? Make your own class Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i typeinterface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs; type   { TStringObject }   TStringObject = class(TObject)  public    TheString:String;    constructor Create( AString:String);  end; implementation { TStringObject } constructor TStringObject.Create(AString: String);begin  Self.TheString:=AString;end;   and then Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.button_LoadClick(Sender: TObject);begin  combobox.AddItem('Chevrolet', TStringObject.Create('Chevrolet'));  ....end; ---------------------------------------------------------------------- alpine · 2021-05-18 06:42 May be with a class helper (TComboBoxHelper) ? The names will somewhat differ from requested, but may be not so important. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    btnLoad: TButton;    btnSelect42: TButton;    ComboBox1: TComboBox;    Label1: TLabel;    procedure btnLoadClick(Sender: TObject);    procedure btnSelect42Click(Sender: TObject);    procedure ComboBox1Select(Sender: TObject);  private   public   end;   { TComboBoxHelper }   TComboBoxHelper = class helper for TComboBox  public    procedure ItemAdd(S: String; Id: Integer);  function SelectedId: Integer;    function SetId(Id: Integer): Boolean;  end; var  Form1: TForm1; implementation {$R *.lfm} { TComboBoxHelper } procedure TComboBoxHelper.ItemAdd(S: String; Id: Integer);begin  Items.AddObject(S, TObject(PtrInt(Id)));end; function TComboBoxHelper.SelectedId: Integer;begin  if ItemIndex > -1 then    Result := Integer(PtrInt(Items.Objects[ItemIndex]))  else    Result := -1;end; function TComboBoxHelper.SetId(Id: Integer): Boolean;var I: Integer;begin  for I := 0 to Pred(Items.Count) do    if Integer(PtrInt(Items.Objects[I])) = Id then    begin      ItemIndex := I;      Exit(True);    end;  Result := False;end; { TForm1 } procedure TForm1.btnLoadClick(Sender: TObject);begin  ComboBox1.ItemAdd('Chevrolet', 17);  ComboBox1.ItemAdd('Nissan',    22);  ComboBox1.ItemAdd('Ford',      13);  ComboBox1.ItemAdd('Toyota',    42);  ComboBox1.ItemAdd('VW',        56);  ComboBox1.ItemIndex    := 0;end; procedure TForm1.btnSelect42Click(Sender: TObject);begin  if ComboBox1.SetId(42) then    ShowMessage('Item with Id=42 is ' + ComboBox1.Text)  else    ShowMessage('No item 42');end; procedure TForm1.ComboBox1Select(Sender: TObject);begin  Label1.Caption := ComboBox1.SelectedId.ToString;end; end.