Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

OnContextPopUp

22 views
Skip to first unread message

diktyo

unread,
Jul 18, 2008, 1:31:30 PM7/18/08
to

I have 2 labels, label1 and label2. I also have a PopupMenu1 with one
menuitem (for the time being) N1 with the caption RefreshData.
I want to right click on either label1 or label2 , popup the menu and
click on RefreshData, then do whatever I need depending on which label
I right-clicked.
I have a handler for label1ContextPopUp (same for label2) , set Handled
to True and popup the menu with PopupMenu1.Popup(MousePos.X,
MousePos.Y).

2 problems :

1) small. The coordinates MousePos.X, MousePos.Y must be converted to
something else (relative to cursor position ?) . I think I will manage
to do it (someday !).

2) bigger. From inside the popupmenu handler I can find through
PopupMenu1.PopUpComponent which label I am on, but not from the
menuitem N1 click handler (which does the job or calls a RefreshData
procedure).
How am I to do this ?

Thanks,

Petros

Remy Lebeau (TeamB)

unread,
Jul 18, 2008, 3:00:55 PM7/18/08
to

"diktyo" <dik...@axd.forthnet.gr> wrote in message
news:4880d372$1...@newsgroups.borland.com...

> I want to right click on either label1 or label2 , popup the menu
> and click on RefreshData, then do whatever I need depending
> on which label I right-clicked.

Use the TPopupMenu.PopupComponent property to know which TLabel was clicked
on.

> I have a handler for label1ContextPopUp (same for label2) ,
> set Handled to True and popup the menu with
> PopupMenu1.Popup(MousePos.X, MousePos.Y).

Why not just let the TLabel components display the PopupMenu automatically
for you?

> The coordinates MousePos.X, MousePos.Y must be converted
> to something else (relative to cursor position ?) .

The MousePos parameter is expressed in client coordinates relative to the
calling TLabel's top-left corner, but Popup() expects screen coordinates
instead. Use the calling TLabel's ClientToScreen() method for the
conversion, ie:

procedure TForm1.LabelContextPopup(Sender: TObject; const MousePos:
TPoint; var Handled: Boolean);
var
P: TPoint;
begin
P := (Sender as TLabel).ClientToScreen(MousePos);
PopupMenu1.Popup(P.X, P.Y);
Handled := True;
end;

> From inside the popupmenu handler I can find through
> PopupMenu1.PopUpComponent which label I am on

The TPopupMenu.PopupComponent property is not filled in if you use the
OnContextPopup event to call Popup() manually. You have to let the TLabel
components display the TPopup for you in order for the PopupComponent
property to be meaningful.

> but not from the menuitem N1 click handler (which does the job
> or calls a RefreshData procedure).

Had you let the TLabel components display the TPopupMenu automatically, the
PopupComponent property should retain its value while the selected TMenuItem
is executing. However, since you are calling Popup() manually, you will
have to use an alternative approach. I suggest using the TPopupMenu's Tag
property for that, ie:

procedure TForm1.LabelContextPopup(Sender: TObject; const MousePos:
TPoint; var Handled: Boolean);
var
Label: TLabel;
P: TPoint;
begin
Label := Sender as TLabel;
P := Label.ClientToScreen(MousePos);
PopupMenu1.Tag := Integer(Label);
try
PopupMenu1.Popup(P.X, P.Y);
finally
PopupMenu1.Tag := 0;
end;
Handled := True;
end;

procedure TForm1.N1Click(Sender: TObject);
var
Label: TLabel;
begin
Label := TLabel(PopupMenu1.Tag);
if Label <> nil then
begin
// use Label as needed...
end;
end;


Gambit


diktyo

unread,
Jul 19, 2008, 2:13:51 AM7/19/08
to
Remy Lebeau (TeamB) wrote:

>
>
> Gambit

--

Thanks Remy,

It works either way. GREAT !


Petros

0 new messages