Click to See Complete Forum and Search --> : ListView: Selected row
gknierim
February 22nd, 2000, 08:25 AM
I have a row selected in my listview. How can I reference the items in the selected row so that I may populate another form with those values? Basically I'm trying to select a row for editing, and when they click on the Edit button display another form with the selected record displayed. I'm guessing I can just populate the fields on the 2nd form with 'formname.txtbox.text' or something similar? Any suggestions would be great!!
Thanks,
Greg
Crazy D
February 22nd, 2000, 08:30 AM
A listview has the selecteditem property, that's a pointer to the selected item. Need to check for Is Nothing though, to see if it's selected.
Then you can fill the new form with form2.text1.text = lv.selecteditem.subitems(1) etc.
Crazy D :-)
"One ring rules them all"
Chris Eastwood
February 22nd, 2000, 08:36 AM
This code should help you out :
private Sub Form_Load()
'
' This assumes that your listview has 3 columns
' - and that it's in report mode
'
Dim li as ListItem
Dim lCount as Long
'
' Populate the listview with some stuff
'
for lCount = 1 to 255
set li = ListView1.ListItems.Add(, "K" & lCount, "Row" & lCount)
li.SubItems(1) = "Col1, Row" & lCount
li.SubItems(2) = "Col2, Row" & lCount
next
'
End Sub
'
' Now for the editing when you click the 'edit' button
'
private Sub cmdEdit_Click()
'
Dim li as ListItem
'
' Check to see if an item is selected
'
set li = ListView1.SelectedItem
'
' If not, then just quit from this routine
'
If li is nothing then
Exit Sub
End If
'
' Create a new 'editing form' - form2 in this
' case
'
Dim fFrm as Form2
'
set fFrm = new Form2
'
Load fFrm
'
' This isn't the preferred way of doing it - I'd
' recommend creating property procedures in the
' 'editing' form, so you can check the values
' before they are applied (but it will do for an
' example
'
fFrm.Text1.Text = li.Text
fFrm.Text2.Text = li.SubItems(1)
fFrm.Text3.Text = li.SubItems(2)
'
fFrm.Show vbModal, me
'
' Unload the form as we are finished with it
'
Unload fFrm
'
' Don't forget to set it to nothing
'
set fFrm = nothing
'
End Sub
Chris Eastwood
CodeGuru - the website for developers
http://codeguru.developer.com/vb
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.