Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Right Mouse Button Queries 2

Status
Not open for further replies.

Trojan

Programmer
Nov 26, 1999
5
GB
I would like to be able to create custom Right Mouse Button menus but cannot find any info on this subject... Can anyone help? Also is it possible to select an item from a Listbox using the Right Button to enable the custom menu to apply to it?
 
What-ho,

right, if you want to define a right click menu, the first thing you have to do is define a new menu for the relative form, using the Menu Editor Tool. This isn't the definitive way of doing this, but I reckon it's the easiest. The Menu Editor icon can be found on the IDE toolbar, between the Add Form and Open Project icons. This'll open up a window where you can define your new menu. A typical menu will look something like this in the Editor Tool:

File
...New
...Delete
...-
...Save
...-
...Close

The indentations are set with indentation buttons in the Editor Tool and imply that various items are sub-items of others. Each of these menu items has various properties associated with it, as shown in the upper part of the Editor Tool Window. The two most important properties (the ones you can't just leave as they are) are Caption and Name. Caption defines what will be displayed in the menu, whereas Name defines what the subs behind the elements of your menu are going to be called. In our hypothetical menu above, lets say the names are:

mnuFile
...mnuFileNew
...mnuFileDelete
...mnuFileSeperatorOne
...mnuFileSave
...mnuFileSeperatorTwo
...mnuFileClose

mnuFile will be set as not visible in the Editor Tool, so that only the menu options appear in the menu when it is invoked.

So, these'll be your subs which will define what each function'll do; ie:

private sub mnuFileNew_Click()
blah blah blah
end sub

They'll be click event subs, the same as, say, command buttons, as you want the functionality to be invoked when you click that menu item.

Now comes the easy bit...getting the menu to be invoked on a right mouse click. The way to do this is to use the MouseDown event for whatever control it is you want the right mouse button to create a menu over. For example, if you want the menu to be available over, say, a text box control, you'd use the Text1_MouseDown event. The mouse down event returns four parameters, which button was pressed, whether shift was being held down and the X and Y coordinates of the click event. So, to get your menu to be shown, you'd do something like this:

Private Sub Text1_MouseDown(Button as Integer, Shift as Integer, x as Single, y as Single)

If Button = vbRightButton then
PopupMenu mnuFile
End If

End Sub

So, now your menu's on display and waiting for you to click one of its options, at which point it'll go off to the appropriate Sub. PopupMenu is a standard VB method which, you guessed it, Pops Up Menus (as defined in the Menu Editor Tool).


All this stuff's in MSDN if you can get hold of it, so have a look in there if you get stuck.


Hope that helps,

Cheerio,

Paul
 
Thanks very much NFI - I appreciate your help. Now, how about selecting Listbox items with the right button?
 
Some people are just never satisfied, are they?

Oh well...I tried to select items from a listbox using the right mouse button, namely through the mousedown event again, but there seems to be no way to tell the listbox to make the item under the mouse the selected item...in fact, listboxes don't seem to have any concept of selected items, which is most likely to do with the items in a listbox not being discrete objects...which makes things tricky.

So, at the risk of being berated for this, I'm going to say that it's very unlikely that you can select items from a listbox using the right mouse button.

But, helpful chap that I am, here's a solution:

From the microsoft windows common controls 6.0 SP3 tab, select the ListView control and put it in place of yourListBox control. ListViews are fully object oriented and are really just a visual collection of sorts, where each item in the list is an object in its own right, which gives you stupendous amounts of flexibility, plus, by default, left or right clicking on an item will make that item the SelectedItem of the ListView control. So, do something like this:

Private Sub Form_Load()
Dim itm As MSComctlLib.ListItem

For a = 1 To 9
Set itm = ListView1.ListItems.Add(a, , "Item " & a)
Next

End Sub

Private Sub ListView1_Click()

Me.Text1.Text = Me.ListView1.SelectedItem

End Sub


In this little test app, I've got a listview control called ListView1 and a textbox control called (go on, guess) Text1. As you can see, as each item in the listview control is an object (a list item object), you have to set an object up and then add it to the listview control by using the controls Add method. In the stuff above, ten items will be added, called "item 1" to "item 9". Then, on the click event of the listview control, I've just set the textbox control to show the SelectedItem (in fact, the SelectedItem is, in itself, a list item and has all the properties and methods of a list item...it just happens that the default property for a list item is text, so effectively, what I'm doing here is going:

me.text1.text = selecteditem.text

but you don't have to know that if you're not interested).

So there you go.

Shun not the ListView control, for, verily, it is the best thing since processed cheese.
Here endeth my posting.


Paul :)
 
Excellent Paul. You have now completely satisfied my thirst for knowledge with regard to the right mouse button. I should add though that I have many other questions of a non-right-mouse-button-nature as well but I think you deserve a break after all your recent efforts on my behalf.
Cheers For Now, Ian.
 
'here's a way to right click a list box item

Option Explicit
Private Const LB_GETTOPINDEX = &H18E

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Sub Form_Load()
Dim i As Integer
For i = 1 To 10
List1.AddItem i
Next
End Sub

Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim iRowHeight As Integer
Dim iTop As Integer
Dim iInsert As Integer
If Button = 2 Then
iTop = SendMessage(List1.hwnd, LB_GETTOPINDEX, 0&, 0&)
iRowHeight = TextHeight("x")
iInsert = Y \ iRowHeight
List1.ListIndex = iInsert + iTop
End If
End Sub
 
Here is the code to select an item in a Listbox using right button of mouse. There is no need to use API.


Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbRightButton Then
List1.ListIndex = Y \ TextHeight("X")
End If
End Sub
 
Thanks Guys. I'll try these ideas out now.
Ian.

* Complete with new handle - apparently my previous handle had the misfortune to share its name with some sort of a computer virus. Upon the discovery of this fact I felt it was only right and proper to disassociate myself from that name henceforth! ;)
 
Can I just say that the code Mohmehran has posted here is a fantastically clever piece of code - I cut and pasted that thinking, "Hah! This'll *never* work, surely!"...certainly ate *my* words!

Excellent brain you have there, well done :)


Paul
 
It *is* cool <grin>

One thought occurs to me:

If you have some strange font size in the ListBox would it still work? Is there some way to get the actual textheight of and entry in the ListBox?

Don't think I'm being picky Mohmehran, your bit of code is excellent and I'm going to use it all over the place.
 
TextHeight is a property of Form, Printer and Picture objects. So when you place the ListBox on the form, the TextHeight will represent the height of text according to the Font property of the form. If you want to use another font other than of the form, you can use a Picture object and then set the Font property to what you want, then use the Picture a as a container and put the ListBox in it. (And set the Border of the picture off) This way you can use every font that you want to use.

I hope it helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top