Solved

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

Posted on 2004-08-12
9
565 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 1

Author Comment

by:tdk_man
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:tdk_man
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now