Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-08-12
9
Medium Priority
?
693 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

721 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