Problem seleting a row in a listview

HalErp

Member
Joined
Feb 15, 2006
Messages
9
Location
El Dorado Hills Ca
Programming Experience
1-3
I am using a listview control I manage to populated the subitems with data from a data table. It all looks good. The problem is that when I try to select a row from the list it does not work all that well. If I click on the first column item it is highlighted but none of the others items in the row are highlighted. Also if I click on any other part of the row (subitems) nothing happens i. e. it does not highlight anything.

I think that I have not set a property correctly but I can not find it.

Question what do I need to do to be able to click on any part of the row to select the row?

Hal:-(
 
Listview properties MultiSelect and FullRowSelect should be self-explanatory.
 
Still having a problem

I have three columns in my ListView box when I open the form noting is selected on the ListView. If I click on one of the rows the first time the proper value is displayed by the MessageBox. And the time that I clicked on is highlighted so far so good.

If I made a mistake in my selection and try again I get and error stating that "0" is not an valid index for the item. If I leave the form and come back I can get another selection it works fine the first time but does not work the second time.

I have tried all kinds of things but can not get it to work. Help!


Private Sub ListViewPositions_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListViewPositions.SelectedIndexChanged
Dim ItemDesc As String
ItemDesc = ListViewPositions.SelectedItems(0).SubItems(2).ToString
MessageBox.Show(ItemDesc,
"199")
End Sub
 
Check if SelectedItems.Count is greater that zero before acccessing the collection. Also you would probably want to use the Text property of the subitem instead of the type.ToString() function method.

VB.NET:
Private Sub ListViewPositions_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles ListViewPositions.SelectedIndexChanged
 If ListViewPositions.SelectedItems.Count > 0 Then
  Dim ItemDesc As String
  ItemDesc = ListViewPositions.SelectedItems(0).SubItems(1).Text
 End If
End Sub
 
John That kind of worked Here is the code that I am using:

Private Sub ListViewPositions_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListViewPositions.SelectedIndexChanged
Dim ItemDesc As String
MessageBox.Show(ListViewPositions.SelectedItems.Count.ToString, "199") '&&& Test Only
If ListViewPositions.SelectedItems.Count > 0 Then
ItemDesc = ListViewPositions.SelectedItems(0).SubItems(2).Text
MessageBox.Show(ItemDesc,
"201") '&&& Test Only
Else
ItemDesc = ListViewPositions.SelectedItems(0).SubItems(2).Text
MessageBox.Show(ItemDesc,
"205") '&&& Test Only
End If
End Sub

When I click on the first line in the listview 1 199 is displayed by the msgbox
then CEO 201 Which is right

If I think I made a mistake and should have clicked on line 4 and do
The Next msgbox displays 0 199
then it crashes

If I close the subform and reopen it thinks works fine again the first time. I have been stuck on this for two days.

:(
 
Probelem solved

Well I do not quite understand this but it solved the problem

I added an OK button to my form so my code looks like this:

Private Sub ListViewPositions_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListViewPositions.SelectedIndexChanged

' Sets the position in the listview box and makes the selection

End Sub

In a second sub I got the value that I wanted after the last selection

Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click

MessageBox.Show(Me.ListViewPositions.SelectedItems(0).SubItems(2).Text, "234") '&&& Test Only this line of code displayed the value that I wanted from the last selection and closed the form

Me.Close()
End Sub

This post is also attached to the root thread the reader is advised to read through the thread as it give an explanation of what I was trying to do.
 
Back
Top