Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8389145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:38:44+00:00 2026-06-09T18:38:44+00:00

I create a Custom Tpanel and inside I put various Custom Components … procedure

  • 0

I create a Custom Tpanel and inside I put various Custom Components …

procedure Panel_Comp(Location: TWinControl; NumOfComp:     Integer;Left,Top,Height,width:Integer);  
begin  
  MyPanel := TsPanel.Create(Conf);  
  MyPanel.Name := 'MyPanel' + IntToStr(NumOfComp);  
  MyPanel.Parent := Location;  
  MyPanel.Left := Left;  
  MyPanel.Top := Top;  
  MyPanel.Height := Height;  
  MyPanel.Width := width;  
  MyPanel.Caption := '';  
end; 

and i call it like this

Panel_Comp(Conf.ScrollBox1,1,8,10,70,322);  

in the same logic i put inside the new panel other custom components including a tBitbtn the have a onclick event..

procedure BitBtn_Comp(Location: TWinControl; NumOfComp: Integer; Left,Top,Height,Width,ImageNum: Integer);  
begin  
  MyBitBtn := TBitBtn.Create(Conf);  
  ......  
  MyBitBtn.tag := NumOfComp;  
  MyBitBtn.OnClick:= Conf.CloseCurrentPanel;
end;

In the main Forn The TConf.CloseCurrentPanel;

procedure TConf.CloseCurrentPanel(Sender: TObject);  
var  
  panelComp: TComponent;  
begin  
  panelComp := FindComponentEx('Conf.MyPanel'+ IntToStr(TBitBtn(Sender).tag);
  TPanel(panelComp).Free;
  Application.ProcessMessages;  
end;

When I call that I get access violation…
I think is something that I must free all the components inside the panel before free the panel but how I free the BitBtn before the panel and continue the action of the click event?

Here is the FindComponetEx function instead you need it…

function FindComponentEx(const Name: string): TComponent;  
var  
  FormName: string;  
  CompName: string;  
  P: Integer;  
  Found: Boolean;  
  Form: TForm;  
  I: Integer;  
begin  
// Split up in a valid form and a valid component name  
  P := Pos('.', Name);  
  if P = 0 then  
  begin  
    raise Exception.Create('No valid form name given');  
  end;  
  FormName := Copy(Name, 1, P - 1);  
  CompName := Copy(Name, P + 1, High(Integer));  
  Found    := False;    
  // find the form  
  for I := 0 to Screen.FormCount - 1 do  
    begin  
      Form := Screen.Forms[I];  
   // case insensitive comparing  
      if AnsiSameText(Form.Name, FormName) then  
        begin  
          Found := True;  
          Break;  
        end;  
    end;  
  if Found then  
    begin  
      for I := 0 to Form.ComponentCount - 1 do  
        begin  
          Result := Form.Components[I];  
         if AnsiSameText(Result.Name, CompName) then Exit;  
        end;  
     end;  
  Result := nil;  
end;  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T18:38:46+00:00Added an answer on June 9, 2026 at 6:38 pm

    The AV occurs because you are destroying a component (MyBitBtn) while it is still handling Windows messages. The solution is to postpone the destruction until later via PostMessage, similar to this:

    unit Unit1;
    
    interface
    
    uses
      Windows,
      Messages,
      SysUtils,
      Variants,
      Classes,
      Graphics,
      Controls,
      Forms,
      Dialogs,
      ExtCtrls,
      StdCtrls;
    
    const
      UM_DESTROYPANEL = WM_APP + 623; // some "unique" number; UM = user message
    
    type
      TConf = class(TForm)
        Panel1: TPanel;
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      strict private
        procedure UMDestroyPanel(var Message: TMessage); message UM_DESTROYPANEL;
      public
        { Public-Deklarationen }
      end;
    
    var
      Conf: TConf;
    
    implementation
    
    {$R *.dfm}
    
    procedure TConf.Button1Click(Sender: TObject);
    begin
      PostMessage(Handle, UM_DESTROYPANEL, 0, 0);
    end;
    
    procedure TConf.UMDestroyPanel(var Message: TMessage);
    begin
      Panel1.Free;
    end;
    
    end.
    

    If needed you can use wParam and lParam to pass through parameters like so:

    procedure TConf.Button1Click(Sender: TObject);
    begin
      PostMessage(Handle, UM_DESTROYPANEL, WPARAM(Panel1), 0);
    end;
    
    procedure TConf.UMDestroyPanel(var Message: TMessage);
    begin
      TObject(Message.WParam).Free;
    end;
    

    EDIT:
    In your situation I’d probably rewrite TConf.CloseCurrentPanel like this:

    procedure TConf.CloseCurrentPanel(Sender: TObject);
    var
      panelComp: TComponent;
    begin
      panelComp := FindComponentEx('Conf.MyPanel'+ IntToStr(TBitBtn(Sender).Tag);
      PostMessage(Handle, UM_DESTROYPANEL, WPARAM(panelComp), 0); 
    end;
    

    Alternatively you can pass through the Tag (might be the better solution because there’s less casting involved):

    procedure TConf.CloseCurrentPanel(Sender: TObject);
    begin
      PostMessage(Handle, UM_DESTROYPANEL, TBitBtn(Sender).Tag, 0);
    end;
    
    procedure TConf.UMDestroyPanel(var Message: TMessage);
    var
      panelComp: TComponent;
    begin
      panelComp := FindComponentEx('Conf.MyPanel'+ IntToStr(Message.WParam));
      panelComp.Free;
    end;
    

    AFAICT the Application.ProcessMessages isn’t needed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need create custom dialog and put JPanel into it. Is it possible?
I want to create custom templated control which controls inside of template coluld be
I want to create a custom control derived from TPanel that contains an image
I want to create custom WPF control that has a single child control inside.
I want to create a custom TPanel that has a TImage32 in it. The
OK, I'm trying to create some custom number of TPanel's at runtime on TScrollBox
I need to create custom folder to put multiple applications and get Zend_Tool to
I am attempting to create custom colors of the tabs in a TabHost. I
How to create Custom shaped buttons in c#: say triangular
Can i create custom touch events for iPhone?? will the device support for creating

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.