Home  |  Index  |  Dotnet4all Snippets  |  Submit resources
About  |  Mail us  
Dotnet4all Logo
Listview and context menu question (Saturday, August 28, 2004)
 

Found the following interesting discussion in the Newsgroups:

Listview and context menu question
by:Sameh Ahmed

Hello there
i have a context menu linmked to a listview control.
I want the context mnu to appear ONLY when i right click on an item and not
any place in listview.
any ideas?
Thanks in advance.
PS: i want the same with treeviews
Regards
Sameh


 Reply:
by:Brian Henry

 I do it by checking the selecteditems.count property of the listview.. but I
am sure there are better ways of doing it

if selecteditems.count > 0
' show menu
end if

or if you have multi select on and want right click on only one selected

if selected items.count = 1
' show menu
end if

just an idea


 Reply:
by:Sameh Ahmed

 Thanks for ur time
this is what i use
but the context menu appears even if u right click on a different item
i don't want it to appear when u select an item then move in an empty space


 Reply:
by:sawhar@hotmail-dot-com.no-spam.invalid (carmen)

 Sameh,

You can include an If ....Else statement to check whether there is any
selected item in your list view. If no item is selected, then set the
visible property of your context menu item(s) to false.

Carmen


 Reply:
by:sawhar@hotmail-dot-com.no-spam.invalid (carmen)

 example:

Private Sub ListView1_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ListView1.SelectedIndexChanged
ControlContextMnu()
End Sub

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
ControlContextMnu()
End Sub

Private Sub ControlContextMnu()
Dim ctmtem As MenuItem
If Me.ListView1.SelectedItems.Count = 0 Then
For Each mnuitem In ContextMenu1.MenuItems
mnuitem.Visible = False
Next
Else
For Each mnuitem In ContextMenu1.MenuItems
mnuitem.Visible = True
Next
End If
End Sub



Posted by Xander Zelders



 
Previous Posts
    - How to use GetFiles
    - Custom treeview question
    - Error Using Webbrowser Control on Form
    - Regular Expressions
    - forms Control collections
    - Jump Menu (DropDown Menu) in ASP.net.
    - Shutdown / Restart
    - Printing in .NET
    - VB.Net Output
    - How to load an object's property which is an array...

Archives
    - 08/01/2004 - 08/08/2004
    - 08/08/2004 - 08/15/2004
    - 08/15/2004 - 08/22/2004
    - 08/22/2004 - 08/29/2004
    - 08/29/2004 - 09/05/2004
    - 09/05/2004 - 09/12/2004
    - 09/12/2004 - 09/19/2004
    - 09/19/2004 - 09/26/2004
    - 09/26/2004 - 10/03/2004
    - 10/03/2004 - 10/10/2004
    - 01/02/2005 - 01/09/2005
    - 01/09/2005 - 01/16/2005
    - 01/30/2005 - 02/06/2005
    - 01/01/2006 - 01/08/2006


Disclaimer & Terms of Use  | DotNet4All.Com concept & © 2004 - 2007 by  Zelders²  - Holland