2

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:

enter image description here

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.

2
  • try to find Custom Containers Pack. If it would work on D7 - you just make a regular form with groupbox, edit and button, and then compile it into new component Commented Apr 24, 2013 at 14:22
  • general hint: both GExperts and CnWizards have component-to-code actions. Drop any component onto the form, then run that action and you would have creation code for it. Learn it and do the same with your component if you like it. Commented Apr 24, 2013 at 14:25

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

6 Comments

I understand that, but still how do I create an object from the TSearchBox class on my form? Can you help me with that? because I'm not sure how to do that. I tried to but it wouldn't compile due to the errors in the code. Though it seemed to me that what I wrote was correct.
The final part of my answer covers that.
@MikhailRybkin you just drop it on form like any other component
@Arioch'The: only if you install the component into the IDE. Otherwise, you have to create it in code.
Alright. I wrote FSearchBox: TSearchBox; in the form's object and variable declaration section. Everything seems to work, but when I try to save the project Delphi shows this message: "Field BookManager.FSearchBox does not have a corresponding component. Remove the declaration?"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.