Editable ListView

RampagePS

Newcomer
Joined
Aug 25, 2004
Messages
17
Hey, I have done a search in the forums and have not found a solution, so I thought I would ask and see if anyone knew what i needed to do to solve my problem. My problem is I have a listview with items and subitems and I want the user to be able to right click a selected listview item and hit edit contact which I have on the context menu and it load up another form I have and it will put all the the items from the selected listview item into a bunch of different text boxes.

Rampage<PS>
 
What specifically is the problem? Or are you looking for a quick how-to? If the latter is the case, On the MouseDown (or MouseUp, I don't know which would be better) event, check which button was clicked (e.Button). If it was the right button, and there is an item selected (ListView.SelectedItems.Count = 1), pop your context menu up, and if they click "Edit Contact" populate your contact form with the subitems of your listview, show it, when they click OK, modify the selected item using the contents of the contact form.
 
yeah when they click edit contact i want the selected items to go to a bunch of textboxes on another form, how would i get the selected items to the text boxes?
 
This is one way to do it: Add a new constructor (Sub New) to your second form, which accepts a listview item. In the constructor call MyClass.New() to perform the normal initialization. Then set each textbox's .Text property to the appropriate subitem (ListViewItem.SubItems(index).Text [I think]). When the user clicks okay, set all the subitems to the modified textbox's text, and your changes should be reflected in the original listview.
 
RampagePS said:
sry to say this but i still don't understand what you are saying

Here. Modify this code as necessary and add it to your edit contact form. When you instantiate the form, rather than using the standared constructor [New frmEditContact], pass in the selected ListViewItem [New frmEditContact(lstContacts.SelectedItems(0))].

Visual Basic:
    'Assuming that your list view items have the following information: Name, Address, Phone Number
    Dim _Contact As ListViewItem
    Public Sub New(ByVal ContactInfo As ListViewItem)
        MyClass.New() 'Initialize
'
        _Contact = ContactInfo 'Retain a reference to the listview item
        'Get the info from the listviewitem and copy it to the textboxes
        txtName.Text = ContactInfo.SubItems(0).Text
        txtAdress.Text = ContactInfo.SubItems(1).Text
        txtPhoneNumber.Text = ContactInfo.SubItems(2).Text 'This should be validatad
'
        'This form should be shown using ShowDialog
    End Sub
'
    Private Sub btnOK_Click(ByVal Sender As Object, ByVal e As EventArgs) Handles btnOK.Click
        'Store the info back into the listview item
        _Contact.SubItems(0).Text = txtName.Text
        _Contact.SubItems(1).Text = txtAdress.Text
        _Contact.SubItems(2).Text = txtPhoneNumber.Text
        'Close the form
        Me.Close()
    End Sub

The form will be displayed with all the contact info, and when the user clicks the okay button (in the code above I named it btnOK) the info will be stored back to the ListViewItem.
 
Back
Top