Short VB .Net question, please help!

JonTheBaller

Golden Member
Dec 2, 2002
1,916
0
0
In VB .Net, I made a ListView object with two columns of data (let's say item & price). How do I update a specific index in the ListView? For example, what would my code look like if I wanted to update the price of the 2nd listing in ListView1? Thanks.
 

joohang

Lifer
Oct 22, 2000
12,340
1
0
I could've told you the answer if you asked me 6 months ago. I'll just bump for ya since I am now a complete dummy in .NET.
 

calpha

Golden Member
Mar 7, 2001
1,287
0
0
Not the best example mind you....but it works....lots of other tests need to be added, but I just coded it up for you quickly:

Add One listview (two columns), two text boxes, one command button and paste this code Leave the names default:

What you're looking for is the listview.selecteditemindices which returns a System.Windows.Forms.ListView.SelectedIndexCollection

[edit] damn vb.net ide [/edit]

Add Two Class Vars:
'class variable for storing list view's current selected item index
Dim myListIndex As System.Windows.Forms.ListView.SelectedIndexCollection
'class vairable for storing list view's current selected item
Dim itmx As System.Windows.Forms.ListViewItem


Add the following to the form_load event:
Dim myx As System.Windows.Forms.ListViewItem
Dim i As Integer = 0
Dim j As Integer = 10
For i = 0 To j
myx = Me.ListView1.Items.Add("Decription # " & i)
myx.SubItems.Add("Price = $" & i * 2 & ".00")

Next
'change button's format
Me.Button1.Text = "Update The Stuff"
Me.TextBox1.Text = ""
Me.TextBox2.Text = ""

add the following to the listview1_click event

'get the selected indexes from the listview
myListIndex = Me.ListView1.SelectedIndices
'single select list box.....all we care is the count of selected items is > 1
If myListIndex.Count > 0 Then
'get the first one in the global listindex var
itmx = Me.ListView1.Items(myListIndex(0))
'set the text boxes to the current values of the selected item
Me.TextBox1.Text = itmx.SubItems(0).Text
Me.TextBox2.Text = itmx.SubItems(1).Text
End If

add the following to the button1_click event
If (Me.TextBox1.Text <> vbNullString) And (Me.TextBox2.Text <> vbNullString) Then
If Not itmx Is Nothing Then
itmx.SubItems(0).Text = Me.TextBox1.Text
itmx.SubItems(1).Text = Me.TextBox2.Text
End If
End If