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.
Visual Basic Classic

Avatar of undefined
Last Comment
tward

8/22/2022 - Mon
SOLUTION
ZeonFlash

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
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
tward

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
fuerteventura

It is version 5. Where do I need to put the code you have posted? Many thanks for all replies
tward

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!
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
tward

To Convert it, you can do the following:

1.  Open your form in a text editor.
2.  Replace ComctlLib.ListView to MSComctlLib.ListView
tward

..IF/When converting make sure  you add "Microsoft Windows Common Controls 6.0" under components.
ASKER
fuerteventura

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
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
tward

Sorry, forgot that declaration:

Private Const LVS_EX_FULLROWSELECT = &H20