Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1961
  • Last Modified:

Panel as popup menu

I want to add to my application a panel with some components on it and I want this panel have a popup menu behaviour (when a mouse button is pressed over any component except my panel its visible property will be changed to false). how can I know when the user clicks a components (and the component name) without writing the OnClick events for every component.

is possible to add components to a popup menu?
0
ronnix
Asked:
ronnix
  • 4
  • 3
  • 3
  • +1
1 Solution
 
mokuleCommented:
Try
procedure TForm1.Panel1Exit(Sender: TObject);
begin
Panel1.Visible := False;
end;
0
 
shaneholmesCommented:
Not sure if this is what you are looking for or not:

for this example, i added a TPopupmenu, and assigned each of the components (on the panel) PopupMenu property to this popup menu.

Then I added two Popup menu items

Hide
Show All

And their events

procedure TForm1.Hide1Click(Sender: TObject);
begin
if TPopupMenu(TMenuItem(Sender).GetParentComponent).PopupComponent is TControl then
  if TControl(TPopupMenu(TMenuItem(Sender).GetParentComponent).PopupComponent).Visible = True then
   TControl(TPopupMenu(TMenuItem(Sender).GetParentComponent).PopupComponent).Visible:= False
  else
   TControl(TPopupMenu(TMenuItem(Sender).GetParentComponent).PopupComponent).Visible:= True;
end;

procedure TForm1.ShowAll1Click(Sender: TObject);
var
 I: Integer;
begin
 for I:= 0 to Panel1.ControlCount - 1 do
  if  not Panel1.Controls[I].Visible then
   Panel1.Controls[I].Visible:= True;
end;


Shane
0
 
ronnixAuthor Commented:
Sorry, but this is not what I'm looking for. It's my fault because I didn't formulated my question corectly.
Because I don't know if it is possible to insert some components into a popupmenu (such as a listbox and some buttons) I put a panel on my form with some components, this panel "being" my popupmenu. How can I know if the user clicks any component on the screen except my panel and its components so that I can hide it. I want to do something like the list in Delphi that popups and you can select any property or method of a component or something else. It is visible while you select any item in the list but when you click any area on the screen it disappears.

Hope you understand what I mean
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
shaneholmesCommented:
Like this maybe

TForm1 = class(TForm)
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
    procedure SelectItem(Sender: Tobject);
  public
    { Public declarations }
  end;



procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
 ListBox1: TListBox;
begin
 if ssRight in Shift then
 begin
   ListBox1:= TListBox.Create(Self);
   ListBox1.Parent:= Self;
   ListBox1.Left:= X;
   ListBox1.Top:= Y;
   ListBox1.Items.Add('Item1');
   ListBox1.OnClick:= SelectItem;
 end;
end;

procedure TForm1.SelectItem(Sender: Tobject);
begin
 // do something with selection
 // TListBox(Sender).Items[TListBox(Sender).ItemIndex]
  TListBox(Sender).Free;
end;

Shane
0
 
mokuleCommented:

So do it like this

procedure TForm1.FormClick(Sender: TObject);
begin
Panel1.Visible := False;
end;
0
 
shaneholmesCommented:
Or you could create a Borderless form with a Listbox on it,

then do this

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
 if ssRight in Shift then
 begin
  if MyForm.ShowModal = mrOK then
  begin
   // do something with selection
   //MyListBox.Items[MyListBox.ItemIndex]
  end;
 end;
end;


Shane
0
 
ronnixAuthor Commented:
:)
I see I was not competely understood.
You say I must write the OnMouseDown event for the form, but what if I click another object on the form? It will not call the main form onmousedown event. And then I must write the onmousedown code for every component on the form, so that when I click it my panel disappear (like a popup menu). So I repeat my question. Is it posible to know when a user clicks a component without writing the onclick event for every object on my form( because all surface of my form is covered with components and I cannot write the onmousedown event for my form bacause it would be vainly?

So, when I press a certain button my panel visible property will become true and when I click everywhere on the desktop except the area ocuppied by my panel its visible property will be changed to false.
0
 
mokuleCommented:

Did You try code from my the very first post?
0
 
mokuleCommented:
I think that this code fulfill all Your requirements.

procedure TForm1.FormCreate(Sender: TObject);
begin
  Application.OnDeactivate := FormDeactivate;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Panel1.Visible := True;
  ActiveControl := Panel1;
end;

procedure TForm1.FormDeactivate(Sender: TObject);
begin
  Panel1.Visible := False;
end;

procedure TForm1.Panel1Exit(Sender: TObject);
begin
  Panel1.Visible := False;
end;

procedure TForm1.FormClick(Sender: TObject);
begin
  Panel1.Visible := False;
end;

0
 
Melih SARICAIT ManagerCommented:
yes there is a way .. no 2 ways :p


Prepare a windowProc for ur application Handle only WM_MouseButton event and do wot ever u want...

Or

Add a Procedure like

  private
   procedure onMyMouseDown(msg : Tmsg) ; message WM_MBUTTONDOWN;

in ur form object

  procedure tForm1.onMyMouseDown(msg : Tmsg) ;
   begin
     // do wot u want

   end;

get Details about  WM_Mbuttondown from the link below

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/mouseinput/mouseinputreference/mouseinputmessages/wm_mbuttondown.asp


0
 
ronnixAuthor Commented:
for non zero
In your code I must declare the procedure like this

procedure OnMouseDown(VAR Msg:Tmsg); message WM_MBUTTONDOWN;
else I get this error: "Invalid message parameter list"

And for this code my application does nothing.
I replaced WM_MButtonDown with WM_LButtonDown and The onmousedown proc is called only when I click the form not when I click other object on it.
I think mokule's answer is the best(just my oppinion), but thanks to all for your answers
0
 
Melih SARICAIT ManagerCommented:
yw
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 4
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now