delphidelphi-7

How to set event to a Panel created runtime


In designtime I use a second panel inside of another panel to be as a header. What I want is when user press mouse button inside the second panel both are moved and for that I use this in OnMouseDown event belong to the second panel:

ReleaseCapture;
panel1.Perform(wm_nclbuttondown,HTCAPTION,0);

When user presses mouse button inside panel2 it moves both panels because panel2 is created inside panel1.

My problem is when I create this two panels at runtime is use this:

ReleaseCapture;
(Sender as Tpanel).Perform(wm_nclbuttondown,HTCAPTION,0);

but it moves each panel individually and not both together.

How to do this?


Solution

  • In the first case, your event handler on the 2nd panel is calling Perform on the 1st (ie the parent) panel.

    In the second case, your event handler on the 2nd panel is calling Perform on the 2nd panel itself. You can use this instead for the 2nd panel's event:

    ReleaseCapture;
    (Sender as TPanel).Parent.Perform(WM_NCLBUTTONDOWN, HTCAPTION, 0);