CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2000
    Posts
    264

    ListView: Selected row

    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


  2. #2
    Join Date
    Apr 1999
    Location
    Rotterdam, Netherlands
    Posts
    278

    Re: ListView: Selected row

    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"

  3. #3
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: ListView: Selected row

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured