Link to home
Start Free TrialLog in
Avatar of fuerteventura
fuerteventura

asked on

Select whole line on listview

I have a listview in my form. It has five columns. You can only highlight a line when clicking on the first column, and then only the selected line in the first column is selected. Is it possible to program it so that you can click on any column, and it will highlight the whole line? Cheers.
SOLUTION
Avatar of ZeonFlash
ZeonFlash

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Howard Cantrell
Here is asample example of how you can click on a colunm and show the subitems (cells)

 Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
        Try
            Dim a As ListViewItem = ListView1.Items(ListView1.SelectedIndices(0))
            Dim s As String = ""

            Dim i As Integer
            For i = 0 To 6 ' this loop simply puts together a string (s) to contain each subitem
                s &= i.ToString & ": " & a.SubItems(i).Text & vbNewLine
            Next
            MsgBox(s)
            ' ListView1.Refresh()
        Catch ex As Exception
            '  MsgBox(ex.ToString)
        End Try
    End Sub
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of fuerteventura
fuerteventura

ASKER

It is version 5. Where do I need to put the code you have posted? Many thanks for all replies
These would need to go in a global module, or change Public to Private and you can put them in a form:

Public Const LVM_FIRST = &H1000
Public Const LVM_SETEXTENDEDLISTVIEWSTYLE = (LVM_FIRST + 54)
Public Const LVM_GETEXTENDEDLISTVIEWSTYLE = (LVM_FIRST + 55)

Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd as long, Byval wMsg as long, Byval wParam As Long, Byref lParam As Long) As Long

This stuff:

Dim lStyle as Long

lStyle = SendMessage(ListView1.hwnd, LVM_GETEXTENDEDLISTVIEWSTYLE, 0, 0)
lStyle = lStyle Or LVS_EX_FULLROWSELECT

Call SendMessage(ListView1.hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, Byval lStyle)back to top

I put the above in a function that looks like this:

Public Sub SetListViewToWholeRowSelect(Byval ListViewhWnd As Long)

Dim lStyle as Long

lStyle = SendMessage(ListViewhWnd, LVM_GETEXTENDEDLISTVIEWSTYLE, 0, 0)
lStyle = lStyle Or LVS_EX_FULLROWSELECT

Call SendMessage(ListViewhWnd, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, Byval lStyle)back to top

End Sub

Then just put a call to the Function in your Form_Load Event.

Another thing you can do is upgrade the control from 5 to 6, then you can just set the .FullRowSelect Property!
To Convert it, you can do the following:

1.  Open your form in a text editor.
2.  Replace ComctlLib.ListView to MSComctlLib.ListView
..IF/When converting make sure  you add "Microsoft Windows Common Controls 6.0" under components.
Tward - many thanks again for your replies. i've put the declarations into the from and changed public to private. I've created a sub in the form and called the function on form load using the following:

Call SetListViewToWholeRowSelect(Me.lstview.hwnd)
When I open the form, It's saying the following variable is not defined:
LVS_EX_FULLROWSELECT
Sorry, forgot that declaration:

Private Const LVS_EX_FULLROWSELECT = &H20