Solved

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

Posted on 2004-08-12
9
610 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 100 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

820 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