Solved

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

Posted on 2004-08-12
9
652 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 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
Industry Leaders: 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

Industry Leaders: 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!

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month11 days, 5 hours left to enroll

628 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