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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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_SETEXTENDEDLISTVIEWSTY LE = (LVM_FIRST + 54)
Public Const LVM_GETEXTENDEDLISTVIEWSTY LE = (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_GETEXTENDEDLISTVIEWSTY LE, 0, 0)
lStyle = lStyle Or LVS_EX_FULLROWSELECT
Call SendMessage(ListView1.hwnd , LVM_SETEXTENDEDLISTVIEWSTY LE, 0, Byval lStyle)back to top
I put the above in a function that looks like this:
Public Sub SetListViewToWholeRowSelec t(Byval ListViewhWnd As Long)
Dim lStyle as Long
lStyle = SendMessage(ListViewhWnd, LVM_GETEXTENDEDLISTVIEWSTY LE, 0, 0)
lStyle = lStyle Or LVS_EX_FULLROWSELECT
Call SendMessage(ListViewhWnd, LVM_SETEXTENDEDLISTVIEWSTY LE, 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!
Public Const LVM_FIRST = &H1000
Public Const LVM_SETEXTENDEDLISTVIEWSTY
Public Const LVM_GETEXTENDEDLISTVIEWSTY
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
lStyle = lStyle Or LVS_EX_FULLROWSELECT
Call SendMessage(ListView1.hwnd
I put the above in a function that looks like this:
Public Sub SetListViewToWholeRowSelec
Dim lStyle as Long
lStyle = SendMessage(ListViewhWnd, LVM_GETEXTENDEDLISTVIEWSTY
lStyle = lStyle Or LVS_EX_FULLROWSELECT
Call SendMessage(ListViewhWnd, LVM_SETEXTENDEDLISTVIEWSTY
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
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.
ASKER
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 SetListViewToWholeRowSelec t(Me.lstvi ew.hwnd)
When I open the form, It's saying the following variable is not defined:
LVS_EX_FULLROWSELECT
Call SetListViewToWholeRowSelec
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
Private Const LVS_EX_FULLROWSELECT = &H20
Private Sub ListView1_SelectedIndexCha
Try
Dim a As ListViewItem = ListView1.Items(ListView1.
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