Recent

Author Topic: Creating custom component  (Read 6107 times)

M[a]nny

  • Full Member
  • ***
  • Posts: 130
  • Dreamer
Creating custom component
« on: December 06, 2012, 07:52:02 pm »
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?
Bad news: Time flies.
Good news: You are the pilot.

Don't try to be perfect, just be unique.

kpeters58

  • Sr. Member
  • ****
  • Posts: 267
Re: Creating custom component
« Reply #1 on: December 06, 2012, 08:20:16 pm »
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
Lazarus 2.0.4/FPC 3.0.4/Win 64

M[a]nny

  • Full Member
  • ***
  • Posts: 130
  • Dreamer
Re: Creating custom component
« Reply #2 on: December 06, 2012, 09:32:37 pm »
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?
Bad news: Time flies.
Good news: You are the pilot.

Don't try to be perfect, just be unique.

kpeters58

  • Sr. Member
  • ****
  • Posts: 267
Re: Creating custom component
« Reply #3 on: December 06, 2012, 10:29:04 pm »
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;
Lazarus 2.0.4/FPC 3.0.4/Win 64

Jvan

  • Full Member
  • ***
  • Posts: 181
Re: Creating custom component
« Reply #4 on: May 18, 2021, 02:44:46 am »
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;


If instead of TObject(13), it is TObject('ABC'), what to do?

bpranoto

  • Full Member
  • ***
  • Posts: 134
Re: Creating custom component
« Reply #5 on: May 18, 2021, 06:39:09 am »
If instead of TObject(13), it is TObject('ABC'), what to do?

Make your own class

Code: Pascal  [Select][+][-]
  1. type
  2. interface
  3.  
  4. uses
  5.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs;
  6.  
  7. type
  8.  
  9.   { TStringObject }
  10.  
  11.   TStringObject = class(TObject)
  12.   public
  13.     TheString:String;
  14.     constructor Create( AString:String);
  15.   end;
  16.  
  17. implementation
  18.  
  19. { TStringObject }
  20.  
  21. constructor TStringObject.Create(AString: String);
  22. begin
  23.   Self.TheString:=AString;
  24. end;
  25.  
  26.  

and then

Code: Pascal  [Select][+][-]
  1. procedure TForm1.button_LoadClick(Sender: TObject);
  2. begin
  3.   combobox.AddItem('Chevrolet', TStringObject.Create('Chevrolet'));
  4.   ....
  5. end;
  6.  
« Last Edit: May 18, 2021, 06:47:48 am by bpranoto »

alpine

  • Hero Member
  • *****
  • Posts: 1038
Re: Creating custom component
« Reply #6 on: May 18, 2021, 08:42:19 am »
May be with a class helper (TComboBoxHelper) ?

The names will somewhat differ from requested, but may be not so important.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     btnLoad: TButton;
  16.     btnSelect42: TButton;
  17.     ComboBox1: TComboBox;
  18.     Label1: TLabel;
  19.     procedure btnLoadClick(Sender: TObject);
  20.     procedure btnSelect42Click(Sender: TObject);
  21.     procedure ComboBox1Select(Sender: TObject);
  22.   private
  23.  
  24.   public
  25.  
  26.   end;
  27.  
  28.   { TComboBoxHelper }
  29.  
  30.   TComboBoxHelper = class helper for TComboBox
  31.   public
  32.     procedure ItemAdd(S: String; Id: Integer);
  33.     function SelectedId: Integer;
  34.     function SetId(Id: Integer): Boolean;
  35.   end;
  36.  
  37. var
  38.   Form1: TForm1;
  39.  
  40. implementation
  41.  
  42. {$R *.lfm}
  43.  
  44. { TComboBoxHelper }
  45.  
  46. procedure TComboBoxHelper.ItemAdd(S: String; Id: Integer);
  47. begin
  48.   Items.AddObject(S, TObject(PtrInt(Id)));
  49. end;
  50.  
  51. function TComboBoxHelper.SelectedId: Integer;
  52. begin
  53.   if ItemIndex > -1 then
  54.     Result := Integer(PtrInt(Items.Objects[ItemIndex]))
  55.   else
  56.     Result := -1;
  57. end;
  58.  
  59. function TComboBoxHelper.SetId(Id: Integer): Boolean;
  60. var I: Integer;
  61. begin
  62.   for I := 0 to Pred(Items.Count) do
  63.     if Integer(PtrInt(Items.Objects[I])) = Id then
  64.     begin
  65.       ItemIndex := I;
  66.       Exit(True);
  67.     end;
  68.   Result := False;
  69. end;
  70.  
  71. { TForm1 }
  72.  
  73. procedure TForm1.btnLoadClick(Sender: TObject);
  74. begin
  75.   ComboBox1.ItemAdd('Chevrolet', 17);
  76.   ComboBox1.ItemAdd('Nissan',    22);
  77.   ComboBox1.ItemAdd('Ford',      13);
  78.   ComboBox1.ItemAdd('Toyota',    42);
  79.   ComboBox1.ItemAdd('VW',        56);
  80.   ComboBox1.ItemIndex    := 0;
  81. end;
  82.  
  83. procedure TForm1.btnSelect42Click(Sender: TObject);
  84. begin
  85.   if ComboBox1.SetId(42) then
  86.     ShowMessage('Item with Id=42 is ' + ComboBox1.Text)
  87.   else
  88.     ShowMessage('No item 42');
  89. end;
  90.  
  91. procedure TForm1.ComboBox1Select(Sender: TObject);
  92. begin
  93.   Label1.Caption := ComboBox1.SelectedId.ToString;
  94. end;
  95.  
  96. end.
  97.  
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

 

TinyPortal © 2005-2018