views:

137

answers:

3

Hello,

I have two forms,one is main and other is inherited form main.Lets say I have a function on the main form:

procedure FormMain.CreateButton;
begin
  with TsButton.Create(Self) do begin
    Width := 31;
    Height := 31;
    Left := 31;
    Top := 31;
    Visible := true;
    Parent := Self;
  end;
end;

Usually everything on the main form should be on the inherited form,but this is what I do:

I call CreateButton from mainForm ,but the button is only on the main form.

Is it possible to inherit that button too?

+2  A: 

If you mean "inherited" the way it's normally meant, then the answer is no. (By normal, I mean you created your main form in the IDE, and then in the IDE created a descendant of that main form.)

In that case, controls created at runtime are not part of the inheritance tree, and the descendant knows nothing about it. You'll have to add the same code manually to the descendant as well.

What exactly are you trying to accomplish? If you know ahead of time that the button will be needed on both the base and the descendant forms (which you obviously do, since you're writing code to create the button), why not just actually drop the button on the ancestor?

Ken White
Yeah--put it there but make it invisible. Simply turn it visible when you want it.
Loren Pechtel
+1  A: 

If this were to inherit you would have no way of doing anything different on the two forms. Thus you do not want it to inherit your runtime changes!

Loren Pechtel
+4  A: 

There's a difference between design-time and runtime. The form designer creates a definition for your form, which it instantiates at runtime. If you inherit one form from another, then it takes the base template and adds to it. But form-designer forms are only templates, like class definitions.

Now, at runtime, you instantiate a base form and a derived form, and it creates them from the templates stored in the resource section of your app. If you add something to the instance of the base form, you're modifying an individual instance, not the definition, so of course it's not going to show up on another instance. If you want to add a button dynamically to a form, you have to create it on that instance (in this case, the derived form) individually.

Mason Wheeler