Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Creating a VCL object in a selected object at design time with IDE


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 2 - Last Post: Dec 22, 2017 8:48 PM Last Post By: Lex Dean
Lex Dean

Posts: 132
Registered: 3/26/07
Creating a VCL object in a selected object at design time with IDE  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 17, 2017 5:32 PM
We select and drop VCL components into the design time Form and into VCL objects as standard practice in Delphi

I want a type of ComboBox that is selected from the IDE toolbar and dropped into a blank canvas VCL Base object in csDesignTime.

I want to register the Combobox with special pointer connections to the VCL base object including parent
So I started writing below code.
But I'm dealing with memory access issues or something, So what is happening and whats the fix.
I was hoping the IDE would fix the issue.
I do not want to run TTimer

procedure TTComboBox.Free;
Var WC: TWinControl;
begin
WC := inherited Parent;
If (WC <> Nil) and (WC is TabTBar) Then
Begin
TabTBar(WC).FButs[FBarIndex].FCombo := nil;
TabTBar(WC).FButs[FBarIndex].Free;
end;
inherited Free;
end;

procedure TabTComboBox.SetParent(AParent: TWinControl);
Var Butt: TButt;
begin
If (AParent <> Nil) and (AParent is TabTBar) Then
begin
FBarIndex := TabTBar(AParent).FButs.Count;
Butt := TabTBar(AParent).FButs.Add;
Butt.FCombo := Self;
Butt.FGBut.Free;
end else Free;
end;

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Creating a VCL object in a selected object at design time with IDE  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 18, 2017 11:27 AM   in response to: Lex Dean in response to: Lex Dean
Lex Dean wrote:

We select and drop VCL components into the design time Form and into
VCL objects as standard practice in Delphi

I want a type of ComboBox that is selected from the IDE toolbar and
dropped into a blank canvas VCL Base object in csDesignTime.

I want to register the Combobox with special pointer connections to
the VCL base object including parent So I started writing below code.
But I'm dealing with memory access issues or something, So what is
happening and whats the fix. I was hoping the IDE would fix the
issue. I do not want to run TTimer

Seems to me like you need a pointer to the actual button instead of
just its index. Think what happens if other buttons get freed before
your component is freed? Your index will be off!

Also, if TButt derives from TComponent, you should call
FreeNotification() on that button so you can be notified if that button
gets freed before your component is freed, so you can nil your
reference to it.

Try something more like this:

type
  TTComboBox = class(...)
  private
    FButt: TButt;
  protected
    procedure Notification(AComponent: TComponent; Operation:
TOperation); override;
  public
    destructor Destroy; override;
  end;
 
destructor TTComboBox.Destroy;
begin
  if FButt <> nil then
  begin
    FButt.FCombo := nil;
    FButt.RemoveFreeNotification(Self);
    FButt.Free;
  end;
  inherited Destroy;
end;
 
procedure TTComboBox.Notification(AComponent: TComponent; Operation:
TOperation);
begin
  inherited;
  if (Operation = opRemove) and (AComponent = FButt) then
    FButt := nil;
end;
 
procedure TabTComboBox.SetParent(AParent: TWinControl);
begin
  if AParent <> Self.Parent then
  begin
    if FButt <> nil then
    begin
      FButt.FCombo := nil;
      FButt.RemoveFreeNotification(Self);
      FButt.Free;
      FButt := nil;
    end;
 
    inherited;
 
    if (AParent <> nil) and (AParent is TabTBar) then
    begin
      FButt := TabTBar(AParent).FButs.Add;
      Butt.FreeNotification(Self);
      Butt.FCombo := Self;
    end;
  end else
    inherited;
end;


--
Remy Lebeau (TeamB)
Lex Dean

Posts: 132
Registered: 3/26/07
Re: Creating a VCL object in a selected object at design time with IDE  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 22, 2017 8:48 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau

This is a subject I have not researched much until you brought it up.
Notification I have not considered to use before, yet it looks simple to implement.
I see its use for safe coding with links to other components.

Thank you.
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02