Reputation: 826
I want to create a search box for my application. The search box will include two things: a search filed and a search button. I think, the right thing to do would be to put those two components in a group box which will serve as a container to hold them inside. I figured that I needed to create a class derived from the TGroupBox class which upon creation will receive a table name as a parameter to search in. The two components, the search box and the button, are going to be its children - and those are the basic principles how it will be working.
This picture illustrates what the search box will look like:
Here's what I've done so far:
unit clsTSearchBox;
interface
uses Classes, SysUtils, StdCtrls, Dialogs, ADODB, DataModule;
type
TSearchBox = class (TGroupBox)
constructor CreateNew(AOwner: TObject; Dummy: Integer);
end;
implementation
constructor TSearchBox.CreateNew(AOwner: TObject; Dummy: Integer);
begin
inherited;
Self.Height := 200;
Self.Width := 400;
Self.Caption := 'Test:'
end;
end.
As you can see, not much. I just created a class that I derived from the TGroupBox class. Please, help me write proper code to instantiate that search box component on my form because I don't really know how to do that. I only need code for proper object creation.
Thank you all in advance.
Upvotes: 2
Views: 1363
Reputation: 395
It sounds like it might be easier if you just put all 3 components in a TFrame, added whatever code you needed to on the controls, and then instantiated an instance of the frame.
Then the frame holds the groupbox, edit, & the button. You simply create the frame with a TYourFrame.Create or do it at design time.
Upvotes: 3
Reputation: 612794
Your group box wants to look something like this:
type
TSearchBox = class(TGroupBox)
private
FSearchEdit: TEdit;
FFindButton: TButton;
public
constructor Create(AOwner: TComponent); override;
end;
For TComponent
descendents you should generally override the virtual constructor named Create
. That will allow your component to be instantiated by the streaming framework.
The implementation looks like this:
constructor TSearchBox.Create(AOwner: TComponent);
begin
inherited;
FSearchEdit := TEdit.Create(Self);
FSearchEdit.Parent := Self;
FSearchEdit.SetBounds(...);//set position and size here
FFindButton := TButton.Create(Self);
FFindButton.Parent := Self;
FFindButton.SetBounds(...);//set position and size here
end;
Probably the most important lesson is that you must set the Parent
property of dynamically created controls. This is needed to impose the parent/child relationship of the underlying windows.
In order to create one of these at runtime you code it like this:
FSearchBox := TSearchBox.Create(Self);
FSearchBox.Parent := BookTabSheet;
FSearchBox.SetBounds(...);//set position and size here
This code would run in the form's constructor, or an OnCreate
event.
I trust you get the basic idea now.
Upvotes: 2