Compound Control Class

Reply

Join Date: Sep 2006
Posts: 3
Reputation: jgriff31 is an unknown quantity at this point 
Solved Threads: 0
jgriff31 jgriff31 is offline Offline
Newbie Poster

Compound Control Class

 
0
  #1
Sep 19th, 2006
Hi Folks,

In trying to build a class that consists of several controls placed on a panel, I've encountered two problems.

With the exception of a RadioGroup, I've been able to succesfully create the object and surface properties from the various controls. However, the radio buttons for the RadioGroup are defined by having their labels in the Items list, and I've been unble to come up with acceptable code to do this.

My big problem is that I have not been able to surface events from these controls. Specifically, I need the OnClick events from Buttons and the UpDown button.

Thanks,
Jon
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 71
Reputation: Micheus is an unknown quantity at this point 
Solved Threads: 4
Micheus's Avatar
Micheus Micheus is offline Offline
Junior Poster in Training

Re: Compound Control Class

 
0
  #2
Sep 23rd, 2006
Originally Posted by jgriff31 View Post
Hi Folks,

In trying to build a class that consists of several controls placed on a panel, I've encountered two problems.

With the exception of a RadioGroup, I've been able to succesfully create the object and surface properties from the various controls. However, the radio buttons for the RadioGroup are defined by having their labels in the Items list, and I've been unble to come up with acceptable code to do this.

My big problem is that I have not been able to surface events from these controls. Specifically, I need the OnClick events from Buttons and the UpDown button.

Thanks,
Jon
Just a sample. I hoppe help you.
Pascal and Delphi Syntax (Toggle Plain Text)
  1. unit PanelControls;
  2. interface
  3. uses
  4. Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  5. ExtCtrls, StdCtrls;
  6.  
  7. type
  8. TPanelControls = class(TPanel)
  9. constructor Create(AOwner :TComponent); override;
  10. destructor Destroy; override;
  11. private
  12. FEdit :TEdit;
  13. FRadioGroup :TRadioGroup;
  14. FButton :TButton;
  15. protected
  16. procedure SetText(value :string);
  17. function GetText :string;
  18. procedure SetItems(value :TStrings);
  19. function GetItems :TStrings;
  20. procedure SetItemIndex(value :Integer);
  21. function GetItemIndex :Integer;
  22. procedure SetOnClick(value :TNotifyEvent);
  23. function GetOnClick :TNotifyEvent;
  24. public
  25. { Public declarations }
  26. published
  27. property EditValue :string read GetText write SetText;
  28. property RadioItems :TStrings read GetItems write SetItems;
  29. property RadioItemIndex :Integer read GetItemIndex write SetItemIndex;
  30. property OnButtonClick :TNotifyEvent read GetOnClick write SetOnClick;
  31. end;
  32.  
  33. procedure Register;
  34.  
  35. implementation
  36.  
  37. constructor TPanelControls.Create(AOwner :TComponent);
  38. begin
  39. inherited;
  40. Caption := '';
  41. Width := 8 *18;
  42. Height := 8 *15;
  43. FEdit := TEdit.Create(Self);
  44. FEdit.Parent := Self;
  45. FEdit.Top := 8;
  46. FEdit.Left := 8;
  47. FEdit.Width := 54;
  48.  
  49. FButton := TButton.Create(Self);
  50. FButton.Parent := Self;
  51. FButton.Top := FEdit.Top;
  52. FButton.Left := FEdit.Left + FEdit.Width + 8;
  53.  
  54. FRadioGroup := TRadioGroup.Create(Self);
  55. FRadioGroup.Parent := Self;
  56. FRadioGroup.Top := FEdit.Top +FEdit.Height +4;
  57. FRadioGroup.TabStop := True;
  58. FRadioGroup.Left := FEdit.Left;
  59. FRadioGroup.Width := 8 *16;
  60. FRadioGroup.Height := 3 *16;
  61. end;
  62.  
  63. destructor TPanelControls.Destroy;
  64. begin
  65. FRadioGroup.Free;
  66. FEdit.Free;
  67. FButton.Free;
  68. inherited;
  69. end;
  70.  
  71. procedure TPanelControls.SetText(value :string);
  72. begin
  73. if FEdit.Text <> value then
  74. FEdit.Text := value;
  75. end;
  76.  
  77. function TPanelControls.GetText :string;
  78. begin
  79. Result := FEdit.Text;
  80. end;
  81.  
  82. procedure TPanelControls.SetItems(value :TStrings);
  83. begin
  84. FRadioGroup.Items.Assign(value);
  85. end;
  86.  
  87. function TPanelControls.GetItems :TStrings;
  88. begin
  89. Result := FRadioGroup.Items;
  90. end;
  91.  
  92. procedure TPanelControls.SetItemIndex(value :Integer);
  93. begin
  94. if FRadioGroup.ItemIndex <> value then
  95. FRadioGroup.ItemIndex := value;
  96. end;
  97.  
  98. function TPanelControls.GetItemIndex :Integer;
  99. begin
  100. Result := FRadioGroup.ItemIndex;
  101. end;
  102.  
  103. procedure TPanelControls.SetOnClick(value :TNotifyEvent);
  104. begin
  105. FButton.OnClick := value;
  106. end;
  107.  
  108. function TPanelControls.GetOnClick :TNotifyEvent;
  109. begin
  110. Result := FButton.OnClick;
  111. end;
  112.  
  113. procedure Register;
  114. begin
  115. RegisterComponents('Samples', [TPanelControls]);
  116. end;
  117.  
  118. end.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 3
Reputation: jgriff31 is an unknown quantity at this point 
Solved Threads: 0
jgriff31 jgriff31 is offline Offline
Newbie Poster

Re: Compound Control Class

 
0
  #3
Sep 28th, 2006
Micheus,

Thanks for the sample code. It was very helpful. However, I have a couple of problems you may be able to help with.

First, I've been unable to compile the event procedures for the UpDown button. They require a parameter for the button -- up or down. Do you have a sample of code for this?

Second, I don't understand how my event handler would work with the code you've given me. When a button is clicked, what procedure (with my event handling code) would be involked? I'm sure I'm missing the obvious here, so please excuse my ignorance.

Thanks in advance,
Jon
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 71
Reputation: Micheus is an unknown quantity at this point 
Solved Threads: 4
Micheus's Avatar
Micheus Micheus is offline Offline
Junior Poster in Training

Re: Compound Control Class

 
0
  #4
Oct 1st, 2006
First, I've been unable to compile the event procedures for the UpDown button. They require a parameter for the button -- up or down. Do you have a sample of code for this?
It is very similar to others. So include into the old post:
Pascal and Delphi Syntax (Toggle Plain Text)
  1. Private
  2. ...
  3. FUpDown :TUpDown;
  4. Protected
  5. ...
  6. procedure SetOnUDChanging(value :TUDChangingEvent);
  7. function GetOnUDChanging :TUDChangingEvent;
  8. Published
  9. ...
  10. property OnUpDownChanging: TUDChangingEvent read GetOnUDChanging write SetOnUDChanging;
  11. end;
  12.  
  13. constructor TPanelControls.Create(AOwner :TComponent);
  14. begin
  15. inherited;
  16. ...
  17. FEdit.Left := 8;
  18. FEdit.Width := 54;
  19.  
  20. FUpDown := TUpDown.Create(Self);
  21. FUpDown.Parent := Self;
  22. FUpDown.Top := FEdit.Top;
  23. FUpDown.Left := FEdit.Left +FEdit.Width +4;
  24.  
  25. FButton := TButton.Create(Self);
  26. FButton.Parent := Self;
  27. FButton.Top := FEdit.Top;
  28. FButton.Left := FEdit.Left + FEdit.Width + 20; // *** Modified
  29. ...
  30.  
  31. destructor TPanelControls.Destroy;
  32. begin
  33. ...
  34. FEdit.Free;
  35. FUpDown.Free;
  36. ...
  37.  
  38. procedure TPanelControls.SetOnUDChanging(value :TUDChangingEvent);
  39. begin
  40. FUpDown.OnChanging := value;
  41. end;
  42.  
  43. function TPanelControls.GetOnUDChanging :TUDChangingEvent;
  44. begin
  45. Result := FUpDown.OnChanging;
  46. end;
  47.  
  48. procedure Register;
  49. ...
The idea is: all that property from component's child that you need to have access (out from main component) must be declared using Set/Get method.

Second, I don't understand how my event handler would work with the code you've given me. When a button is clicked, what procedure (with my event handling code) would be involked? I'm sure I'm missing the obvious here, so please excuse my ignorance.
Opss.. I'm missing the something obvious here.
I think that you don't want export some event's like Button.OnClick. So if you want process this event only into the main component there is no problem, just declare a procedure like the event handler for OnClick:
Pascal and Delphi Syntax (Toggle Plain Text)
  1. type
  2. TPanelControls = class(TPanel)
  3. ...
  4. private
  5. ...
  6. FUpDown :TUpDown;
  7. procedure BtnClick(Sender: TObject); // ***
  8. ...
  9. constructor TPanelControls.Create(AOwner :TComponent);
  10. begin
  11. ...
  12. FButton.Top := FEdit.Top;
  13. FButton.Left := FEdit.Left + FEdit.Width + 20;
  14. FButton.OnClick := BtnClick; // ***
  15. ...
  16. end;
  17. ...
  18. procedure TPanelControls.BtnClick(Sender :TObject); // ***
  19. begin
  20. ShowMessage('My buttom was pressed');
  21. end;
  22.  
  23. procedure Register;
  24. ...
Take care if you process events into main component and exporte this event too. If user writes a event handler to it, your internal event doesn't will be called. To work by this way the code must be ajusted. Let me know about.

p.s. Sorry my poor english. I speek portuguese.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 3
Reputation: jgriff31 is an unknown quantity at this point 
Solved Threads: 0
jgriff31 jgriff31 is offline Offline
Newbie Poster

Re: Compound Control Class

 
0
  #5
Oct 18th, 2006
Micheus,

Sorry I took so long to acknowledge your last post. Your analysis of my problem was right on! Thanks to you, the class I was after is now working perfectly, and I've learned a few things too.

Thanks again,
Jon
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 71
Reputation: Micheus is an unknown quantity at this point 
Solved Threads: 4
Micheus's Avatar
Micheus Micheus is offline Offline
Junior Poster in Training

Re: Compound Control Class

 
0
  #6
Oct 26th, 2006
Originally Posted by Micheus View Post
Take care if you process events into main component and exporte this event too. If user writes a event handler to it, your internal event doesn't will be called. To work by this way the code must be ajusted. Let me know about.
I think is better to put this situation here. :cheesy:
Attention to "// ***" notation.

[]s
Pascal and Delphi Syntax (Toggle Plain Text)
  1. type
  2. TPanelControls = class(TPanel)
  3. ...
  4. private
  5. ...
  6. FOnBtnClick :TNotifyEvent; // *** buffer to local event
  7. procedure BtnClick(Sender: TObject);
  8. published
  9. OnBtnClick :TNotifyEvent read FOnBtnClick write FOnBtnClick;
  10. ...
  11. constructor TPanelControls.Create(AOwner :TComponent);
  12. begin
  13. ...
  14. FButton.Top := FEdit.Top;
  15. FButton.Left := FEdit.Left + FEdit.Width + 20;
  16. FButton.OnClick := BtnClick;
  17. ...
  18. end;
  19. ...
  20. procedure TPanelControls.BtnClick(Sender :TObject);
  21. begin
  22. if Assigned(FOnBtnClick) then // ***
  23. begin
  24. // *** you can make some thing before or after call user event
  25. FOnBtnClick(Sender); // process user button click event
  26. end else
  27. ShowMessage('No event written to button click');
  28. end;
  29.  
  30. procedure Register;
  31. ...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Pascal and Delphi Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC