Recent

Author Topic: [SOLVED] How to trigger an OnChange event in a sub-component  (Read 1472 times)

bobonwhidbey

  • Hero Member
  • *****
  • Posts: 592
    • Double Dummy Solver - free download
I want a GroupBox with both a ComboBox and Label. When the index on the combobox changes, I want to have the label change. This is easy to do with design time coding but I want to create a visual component with this capability.

I don't know how to create an OnChange event for my ComboBox. On a change I want to do some coding and then call the GetCaption array. Below - I've tried to generalize the coding. The component looks fine on a form but has no functionality.

Code: Pascal  [Select][+][-]
  1. type
  2.   TMyBox = class(TCustomGroupBox)
  3.   private
  4.     FL: integer;
  5.   protected
  6.   public
  7.     Lev: TComboBox;
  8.     Lab: TLabel;
  9.     constructor Create(AOwner: TComponent); override;
  10.     destructor Destroy; override;
  11.     function GetCaption: string;
  12.   published
  13.     property Lev: integer read fL write fL;
  14.     property Align;
  15. // all the usual published properties
  16.   end;
  17.  
  18. implementation
  19.  
  20. constructor TMyBox.Create(AOwner: TComponent);
  21. var
  22.   i: integer;
  23. begin
  24.   inherited Create(AOwner);
  25.   Parent := TWinControl(AOwner);
  26.   ParentColor := True;
  27.   Top := 100;
  28.   Left := 100;
  29.   Height := 79;
  30.   Width := 160;
  31.   Caption := 'Contract';
  32.   Font.Name := 'Tahoma';
  33.   Font.Style := [fsBold];
  34.   Font.Size := 10;
  35.  
  36.   Lev := TComboBox.Create(Self);
  37.   Lab := TLabel.Create(Self);
  38.  
  39.   Lev.Parent := Self;
  40.   Lev.Height := 26;
  41.   Lev.Width := 40;
  42.   Lev.Left := 6;
  43.   Lev.Top := 6;
  44.   for i := 0 to 7 do
  45.     Lev.Items.Add(IntToStr(i));
  46.   Lev.ItemIndex := 1;
  47.  
  48.   Lab.Parent := Self;
  49.   Lab.Height := 20;
  50.   Lab.Width := 55;
  51.   Lab.Left := 59;
  52.   Lab.Top := 35;
  53.   Lab.Caption := GetCaption;
  54. end;
  55.          
  56. function TMyBox.GetCaption: string;
  57. begin
  58.   if Lev.ItemIndex = -1 then
  59.       Result := 'Select Level'
  60.   else
  61.       Result := inttostr(Lev.ItemIndex)+ ' was selected';
  62. end;    
« Last Edit: May 14, 2019, 03:57:21 am by bobonwhidbey »
Lazarus 3.0RC2, FPC 3.2.2 x86_64-win64-win32/win64

furious programming

  • Hero Member
  • *****
  • Posts: 853
Re: How to trigger an OnChange event in a sub-component
« Reply #1 on: May 14, 2019, 01:51:52 am »
Just add a simple method to the TMyBox class, with one parameter (in the shape of TNotifyEvent):

Code: Pascal  [Select][+][-]
  1. type
  2.   TMyBox = class(TCustomGroupBox)
  3.   {..}
  4.   protected
  5.     procedure ComboBoxChange(ASender: TObject); virtual;
  6.   {..}
  7.   end;

and assign it to the control event, right after its creation:

Code: Pascal  [Select][+][-]
  1. constructor TMyBox.Create(AOwner: TComponent);
  2. var
  3.   i: integer;
  4. begin
  5.   inherited Create(AOwner);
  6.   Parent := TWinControl(AOwner);
  7.   {..}
  8.  
  9.   Lev := TComboBox.Create(Self);
  10.   Lev.OnChange := @ComboBoxChange;
  11.  
  12.   {..}
  13. end;


One additional thing — don't keep the references of the internal controls as public variables. Declare them as local and then declare a public properties with getters only. Generally, in this way:

Code: Pascal  [Select][+][-]
  1. type
  2.   TMyBox = class(TCustomGroupBox)
  3.   private
  4.     FLev: TComboBox;
  5.     FLab: TLabel;
  6.   {..}
  7.   public
  8.     property Lev: TComboBox read FLev;
  9.     property Lab: TLabel read FLab;
  10.   {..}
  11.   end;
« Last Edit: May 14, 2019, 02:02:01 am by furious programming »
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

bobonwhidbey

  • Hero Member
  • *****
  • Posts: 592
    • Double Dummy Solver - free download
Re: How to trigger an OnChange event in a sub-component
« Reply #2 on: May 14, 2019, 03:53:08 am »
Thank you FP.   I had a problem triggering the OnDrawItem event of the ComboBox. But all was fixed when I set Style := csOwnerDrawFixed;

Thank you very much
« Last Edit: May 14, 2019, 03:56:55 am by bobonwhidbey »
Lazarus 3.0RC2, FPC 3.2.2 x86_64-win64-win32/win64

 

TinyPortal © 2005-2018