Listviews & Submenus

G

Guest

Listviews in access have been the highlight of my year; they beat the pants off the normal list box! I've got a quite a complex listview at the moment, of which there is a lot of conditional formatting, sorting, filtering etc going on.

One thing I would like to add is popup sub menus. To do this, I have handled the Right click event with the following:

Private Sub axListTests_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Long, ByVal y As Long)

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' Sub menu controls for Right click
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Dim lvxObj As ListView
Set lvxObj = axListTests.Object
Dim lstItem As ListItem
Set lstItem = lvxObj.HitTest(x, y)

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' Test for an Item, then show submenu
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If lstItem Is Nothing Then
'Sub menu for white space
MsgBox "Sub menu for Add New Item"
Else
Select Case Button
Case 1
'left button clicked no action required (handled by DoubleClick)
Case 2
'right button clicked
'Sub menu for Selected item
MsgBox "Submenu Goes here for item " & lstItem
lvxObj.HoverSelection
lvxObj.View
End Select
End If

End Sub

As you can see, I can produce a message box depending on what item was right-clicked, or if no item was right-clicked.

The next stage is to produce a popup sub menu... the type you see in Windows Explorer that when you right-click a file you get a small menu with Open, Rename, Cut, Copy blah blah blah.. Well I wanna do the same, but with my own custom commands:

- Open Item
- Delete Item

So the two questions are:

1) How can I create submenus
2) how can I handle click events on submenu items?

TIA...
 
N

Neil

Hello Simon,

You create 'pop up' menus by creating a toolbar
(view->toolbars->customise... then click new). After giving the toolbar a
name, click properties and then select popup from the type combobox. This
toolbar is now in the shortcut menus toolbar. If you find this item and then
tick it, you get a toolbar with all the pop up menus in your application.
The one you have just entered will be under custom. From here, you add
buttons etc as required until you have the menu that you want to appear when
the right mouse button is pressed.

Once you have the toolbar, to add code to a button, i create a new module
which holds all the code for the buttons. You need to create a procedure
(this has to be a public function to work!) for each custom button. Once you
have wrote the procedure, you go back to you custom toolbar and click on the
properties for the button. In the OnAction box, enter the procedure that you
want to run. For many access controls, there is a property called 'Shortcut
Menu Bar' (found in the Other tab) - your menu should be in the dropdown
box. However for your listview, you will probably have to have some code to
show the menu (this is easy so dont worry :)).

Example:

You have created a new popup toolbar named Test. It has a button on there to
display a messagebox to say hello. The procedure for this is as follows:

Public Function SayHello()

MsgBox "Hello!"

End Function

you would place =SayHello() into the required buttons OnAction box to get
the procedure to run when the button is clicked.

To get the menu to appear in the first place when the right mouse button is
clicked, your example could look something like:

Select Case Button
Case 1
'left button clicked no action required (handled by DoubleClick)
Case 2
'right button clicked
' Show test menu
Application.CommandBars("Test").ShowPopup
End Select

HTH,

Neil.

SimonP said:
Listviews in access have been the highlight of my year; they beat the
pants off the normal list box! I've got a quite a complex listview at the
moment, of which there is a lot of conditional formatting, sorting,
filtering etc going on.
One thing I would like to add is popup sub menus. To do this, I have
handled the Right click event with the following:
Private Sub axListTests_MouseUp(ByVal Button As Integer, ByVal Shift As
Integer, ByVal x As Long, ByVal y As Long)
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' Sub menu controls for Right click
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Dim lvxObj As ListView
Set lvxObj = axListTests.Object
Dim lstItem As ListItem
Set lstItem = lvxObj.HitTest(x, y)

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' Test for an Item, then show submenu
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If lstItem Is Nothing Then
'Sub menu for white space
MsgBox "Sub menu for Add New Item"
Else
Select Case Button
Case 1
'left button clicked no action required (handled by DoubleClick)
Case 2
'right button clicked
'Sub menu for Selected item
MsgBox "Submenu Goes here for item " & lstItem
lvxObj.HoverSelection
lvxObj.View
End Select
End If

End Sub

As you can see, I can produce a message box depending on what item was
right-clicked, or if no item was right-clicked.
The next stage is to produce a popup sub menu... the type you see in
Windows Explorer that when you right-click a file you get a small menu with
Open, Rename, Cut, Copy blah blah blah.. Well I wanna do the same, but with
my own custom commands:
 
N

Neil

Glad it works.

I'm not laughing... ...honest... :)

Neil.

SimonP said:
Ah ha! Perfect..!

I'd been pi$$ing around with creating forms, mouse postions, commandbars
and g-d knows what else all frickin day... the solution was so simple.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top