Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Applying A Popupmenu Item To A Specific Listbox Item Without Left Clicking

Posted on 2004-08-12
9
Medium Priority
?
719 Views
Last Modified: 2008-03-10
I have a listbox and have created a pop-up menu which appears when you right-click on the listbox.

I want to be able to right click on an entry in the listbox and then select say Delete from the pop-up menu to delete that listbox entry. But, right clicking doesn't highlight the listbox item so ItemIndex isn't set.

Is there any way to highlight the right-clicked on item before the menu pops up?

TDK_Man
0
Comment
Question by:tdk_man
  • 5
  • 3
9 Comments
 
LVL 7

Expert Comment

by:LRHGuy
ID: 11782975
You can put some code in the MouseUp event to detect the "right" click...

procedure TForm1.ListBox1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X,Y: integer);
var
  APoint: TPoint;
  Index: integer;
begin
  if Button = mbRight then  begin
    APoint.X := X;
    APoint.Y := Y;
    ListBox1.ItemIndex := ListBox1.ItemAtPos(APoint, True);
  end;
end;
0
 
LVL 7

Accepted Solution

by:
LRHGuy earned 300 total points
ID: 11783097
I don't like the previous one...this one works much better...


procedure TForm2.PopupMenu1Popup(Sender: TObject);
var
  APoint: TPoint;
  Index:integer;
begin
  APoint:=PopupMenu1.PopupPoint;
  APoint:=ListBox1.ScreenToClient(APoint);
  Index:=ListBox1.ItemAtPos(APoint,True);
  if Index>=0 then begin
    ListBox1.ItemIndex:=Index;
  end;
end;
0
 
LVL 7

Expert Comment

by:LRHGuy
ID: 11783127
What that does is check the popup point of the popupmenu against the list, and determine which item, if any, the mouse was over. It then selects that item. You could always set the ItemIndex to Index if you have empty space in the list and you want to show that nothing is selected. The what it is now, it leaves itemindex alone unless the mouse is over an item.

Enjoy!
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:tdk_man
ID: 11783625
When I use the code, I get the error Undeclared identifier: 'PopupPoint'.

It aoppears in the Properties for the component in the help files, so I don't understand why.

TDK_Man
0
 
LVL 1

Author Comment

by:tdk_man
ID: 11783682
Just tried the first piece of code too and that pops up the menu *before* the menu item is highlighted.

TDK_Man
0
 
LVL 7

Expert Comment

by:LRHGuy
ID: 11784809
Like I said, the first one I didn't like..it works OK but not with an atached popup menu.

If PopupPoint is listed in the help, but is undeclared...ugh...I don't know. I used Delphi 7 with no problem.
0
 
LVL 1

Author Comment

by:tdk_man
ID: 11785316
Having played around with both pieces of code, neither will actually highlight the item in the Listbox with the right mouse button unless one of the items is left-clicked first.

I'm using Delphi 5 - maybe it's different to V7.

Maybe someone can confirm that

APoint:=PopupMenu1.PopupPoint;

...doesn't work in Delphi 5 please?

TDK_Man
0
 
LVL 7

Expert Comment

by:LRHGuy
ID: 11785595
The second one works fine, and doesn't need any left click. In D7 at least, it works.

I don't know if PopupPoint is a property in D5, not having it...hopefully someone with D5 can tell us!

0
 
LVL 2

Expert Comment

by:beermequik
ID: 11788322
I did the following in Delphi 4...

Set the AutoPopUp property to False;

MouseUpEvent...

procedure TForm1.ListBox1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  APoint: TPoint;
  Index: integer;
begin
  if Button = mbRight then  begin
    APoint.X := X;
    APoint.Y := Y;
    ListBox1.ItemIndex := ListBox1.ItemAtPos(APoint, True);
  end;
  PopupMenu1.Popup(form1.Left + listbox1.left +  APoint.X, form1.top + listbox1.top +APoint.Y);
end;
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question