Click to See Complete Forum and Search --> : ListView - how can I select item?


Innocent
November 14th, 2002, 09:16 AM
OK, I'm almost embarrassed to ask, but...

VB.NET - I have a listview and wants to select an item in list from code.

Athley
November 14th, 2002, 09:53 AM
Hmmmm, strange behaviour here....

lvReport.Items.Item(0).Selected = True

.... does select the item in code but not visually, so you can the do something like this.....

MsgBox(lvReport.SelectedItems.Item(0).Text)

....to return the "not visibly" seleted item......

The help specificly says: You can use this property to determine if an item is selected or to select an item at run time.

The item sure is not highlighted though

I'll look into the visibility thing and get back if I find anything.

/Leyan

Athley
November 14th, 2002, 10:19 AM
rrrrrr. :mad:

I'm stupid sometimes..... you got to make sure the listview has focus to make it highligthed.....

lvTest.Focus()
lvTest.Item(0).Selected = True

Worked for me...

/Leyan

Innocent
November 16th, 2002, 06:27 AM
Thanks!

alcOrtholite
November 16th, 2002, 02:48 PM
When I want to select an item in a listview, because invariably the control will lose focus, I colour the selected line. I use this code shown below

If Me.lvw.Items.Count > 0 Then

'Make all of the rows and items in the listview black
For i = 0 To Me.lvw.Items.Count - 1
Me.lvw.Items(i).ForeColor = System.Drawing.Color.Black
For j = 1 To 7

Me.lvw.Items(i).SubItems(j).ForeColor = system.Drawing.Color.Black

Next
Next

'Change the selected row colour to red

Me.lvwPatientDetails.SelectedItems.Item(0).ForeColor = System.Drawing.Color.Red

For j = 1 To 7
Me.lvwPatientDetails.Items(0).SubItems(j).ForeColor = System.Drawing.Color.Red

Next


Obviously in this example I have 8 Columns in the listview.

I hope this helps.