Dear all

i need help with listview . i have 5 columns and i have 4 menustrips. i want something like below

    If ListView1.SelectedItems(0).SubItems(0).Text = "Advance" Then
        t2.Enabled = False
        t3.Enabled = False
        t4.Enabled = False

    ElseIf ListView1.SelectedItems(0).SubItems(0).Text = "Access" Then
        t1.Enabled = False
        t3.Enabled = False
        t4.Enabled = False

    ElseIf ListView1.SelectedItems(0).SubItems(0).Text = "EN\DN" Then

        t1.Enabled = False
        t2.Enabled = False
        t4.Enabled = False

    ElseIf ListView1.SelectedItems(0).SubItems(0).Text = "Drive" Then
        t1.Enabled = False
        t2.Enabled = False
        t3.Enabled = False
    Else

        t1.Enabled = False
        t2.Enabled = False
        t3.Enabled = False
        t4.Enabled = False

    End If

t1 t2 t3 t4 are the submenu mains of menustrips

mouse right click must read the first column of listview and open the menu as per it. suppose

1st colums is advance so right click must only open the menu of advance........if the 2nd row is for access so when i click right of mouse if must open access menu..

pplz help and sorry for bad english.

Recommended Answers

All 15 Replies

What is the current problem? Where are you having a problem?

the problem is when i do right click it only deduct the ADVANCE and open its right click menu .. but i want that code deduct others and open thr menu

Ok. I've done this code for you.

 Try
 If ListView1.SelectedItems(0).SubItems(0).Text = "Advance" Then
 t1.Enabled = True
 t2.Enabled = False
 t3.Enabled = False
 t4.Enabled = False
 Else
 If ListView1.SelectedItems(0).SubItems(0).Text = "Access" Then
 t1.Enabled = False
 t2.Enabled = True
 t3.Enabled = False
 t4.Enabled = False
 Else
 If ListView1.SelectedItems(0).SubItems(0).Text = "EN/DN" Then
 t1.Enabled = False
 t2.Enabled = False
 t3.Enabled = True
 t4.Enabled = False
 Else
 If ListView1.SelectedItems(0).SubItems(0).Text = "Drive" Then
 t1.Enabled = False
 t2.Enabled = False
 t3.Enabled = False
 t4.Enabled = True
 End If
 End If
 End If
 End If
 Catch ex As Exception
 ' Just ignore this exception
 End Try

Hope this will answer your question.

From my opinion, use ListView HitTest() method to get the information at the position on which item you downed your mouse or outside the label area..

thanks Mr.M but its not working :(

i copy paste this code in my form_load event but not working

No you must double click the ListView it self then past it there it must be in

 Private Sub ListView1_SelectedIndexChange(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
 ' Past the code here
 End Sub

You need to double click the ListView to get the event change handler because you are dealing with the changing of events as you are checking which item is selected.

@ Mr.M
Why do you put Try Catch in the code but want to ignore any errors?

Because when you first click the item in listView it will work but when you try to click another item it will throw an exception that Argument out of range exception wasn't handled so to get ride of this just put Try and ignore this exception it will work perfect. Try it out.

This is to enable you to make selections without there closing application to be able to make another selection. This is for showing the OP what he wanted to obtain as the result so professionally he will have to handle that exception according to his guide or plan but I only ignored it to get results straight away.

I didn't know of any way to deal with it so I choose to ignoring it because it doesn't affect the system and or required results.

mouse right click must read the first column of listview and open the menu as per it. suppose

1st colums is advance so right click must only open the menu of advance........if the 2nd row is for access so when i click right of mouse if must open access menu..

How far I am right that you are saying about the ContextMenu, which always appears on Right Click of the mouse.

As I posted before, you must use the HitTest() to get your desired result and it always perfoms under Listview object's MouseDown or MouseUp events.
I am describing the codes which may be give you a way of thinking.

Private Sub ListView1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDown
        'Declaring variable to get  the location 
        'where user try to hit the mouse pointer
        'in the Listview control
        Dim lstTestInfo As ListViewHitTestInfo = ListView1.HitTest(e.Location.X, e.Location.Y)

        'Disabling all menuItems
        T1.Enabled = False
        T2.Enabled = False
        T3.Enabled = False
        T4.Enabled = False

        'Trying to match the location where user hits the right mouse button.
        'If he hits it on Label or Image then do the job else disable all menuItems
        If e.Button = Windows.Forms.MouseButtons.Right Then
            If (lstTestInfo.Location = ListViewHitTestLocations.Label) Or _
                (lstTestInfo.Location = ListViewHitTestLocations.Image) Then

                Select Case lstTestInfo.Item.Text
                    Case "Advance"
                        T1.Enabled = True
                    Case "Access"
                        T2.Enabled = True
                    Case "En/Dn"
                        T3.Enabled = True
                    Case "Drive"
                        T4.Enabled = True
                End Select
            End If
        End If

    End Sub

hi...i try both codes but it only showing advance menu others are not enabled .... it only working for advance

Use ItemSelectionChanged event in lieu of SelectedIndexChanged event to select item through keyboard or mouse. ItemSelectionChanged event works fine.

 Private Sub ListView1_ItemSelectionChanged(sender As Object, e As System.Windows.Forms.ListViewItemSelectionChangedEventArgs) Handles ListView1.ItemSelectionChanged


        If ListView1.SelectedItems.Count = 1 Then
            '..............
            '..................
            ' Do your jobs here
            'you can use here Mr.M's codes also
            'if you like.
            '...........
            '............
        end If
End Sub     

no shark1 it not working...it only showing me 1st menu for advance. other showing disable.

Disable all events of listview except MouseDown event and run. Please check,What result you are getting.

Before anyone can help you make sure you have the listbox control set up to the condition you need, otherwise all the help is useless:
listview multiSelect = false,
view = details,
FullRow select = true,
HideSelected = False.
After you set all this up you might want to start off with this code snippet as it shows the index of the row you have clicked and returns the the item in the first column:

 Private Sub ListView1_MouseUp(sender As Object, e As MouseEventArgs) Handles ListView1.MouseUp
        If e.Button = Windows.Forms.MouseButtons.Right Then
            Dim index As Integer = 0
            Dim str As String = ""
            index = ListView1.FocusedItem.Index
            str = ListView1.FocusedItem.SubItems(0).Text
            MsgBox("Index   " & index.ToString & " Item " & str)
        End If
    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.