Results 1 to 5 of 5

Thread: How to change/update selected listview item from another form vb.net

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    2

    How to change/update selected listview item from another form vb.net

    I was doing removing items from another form for listbox and it was working good.

    Now I want to do with listview but I couldn't do it here is my listbox codes.. And I am using classes



    Code:
    Dim WithEvents myedit As New Form2
    Dim WithEvents mynew As New Form2
    
    Private Sub btnModify_Click(sender As Object, e As EventArgs)
        If ListBox1.SelectedIndex <> -1 Then
            Dim std As New studentinf
            std = studentinf.studentinformation(ListBox1.SelectedIndex)
            myedit.txtnumber.Text = std.number
            myedit.txtname.Text = std.namesurname
            myedit.txtquiz.Text = std.quiz
            myedit.txtmidterm.Text = std.midterm
            myedit.txtfinal.Text = std.final
            myedit.ShowDialog()
        Else
            MsgBox("Select to modify")
        End If
    End Sub
    
    Sub DoModify() Handles myedit.Done
        Dim std As studentinf
    
        std = studentinf.studentinformation(ListBox1.SelectedIndex)
        std.number = myedit.txtnumber.Text
        std.namesurname = myedit.txtname.Text
        std.quiz = myedit.txtquiz.Text
        std.midterm = myedit.txtmidterm.Text
        std.final = myedit.txtfinal.Text
    
        ListBox1.Items(ListBox1.SelectedIndex) = std.number & vbTab & vbTab & std.namesurname & vbTab & vbTab &
           std.quiz & vbTab & vbTab & std.midterm & vbTab & vbTab & std.final
    End Sub

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: How to change/update selected listview item from another form vb.net

    So what's the question? Are you still trying to do this with a ListView? If so, then what problem are you running into? Also, this doesn't look so much like removing items from a listbox as adding them TO the listbox.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    2

    Re: How to change/update selected listview item from another form vb.net

    Quote Originally Posted by Shaggy Hiker View Post
    So what's the question? Are you still trying to do this with a ListView? If so, then what problem are you running into? Also, this doesn't look so much like removing items from a listbox as adding them TO the listbox.
    Yeah sorry I was so stressed while I was writing this, I have been trying this since 5 hours. The codes for changing selected items from listbox. I wrote similar codes for listview but it didn't work

    Code:
    Private Sub btnModify_Click(sender As Object, e As EventArgs) Handles btnModify.Click
            If ListView1.FocusedItem.Index < 0 Then
                Dim std As studentinf
                std = studentinf.studentinformation(ListView1.FocusedItem.Index)
                myedit.txtnumber.Text = std.number
                myedit.txtname.Text = std.namesurname
                myedit.txtdepartment.Text = std.department
                myedit.txtquiz.Text = std.quiz
                myedit.txtmidterm.Text = std.midterm
                myedit.txtfinal.Text = std.final
                myedit.lblavg.Text = std.avg
    
                myedit.ShowDialog()
    
            End If
        End Sub
    
        Sub modify() Handles myedit.Done
            Dim std As studentinf
            std = studentinf.studentinformation(ListView1.FocusedItem.Index)
    
            std.number = myedit.txtnumber.Text
            std.namesurname = myedit.txtname.Text
            std.department = myedit.txtdepartment.Text
            std.quiz = myedit.txtquiz.Text
            std.midterm = myedit.txtmidterm.Text
            std.final = myedit.txtfinal.Text
            std.avg = myedit.lblavg.Text
    
            Dim i As New ListViewItem
            i = ListView1.Items.Add(std.number)
            i.SubItems.Add(std.namesurname)
            i.SubItems.Add(std.department)
            i.SubItems.Add(std.quiz)
            i.SubItems.Add(std.midterm)
            i.SubItems.Add(std.final)
            i.SubItems.Add(std.avg)
    
        End Sub

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: How to change/update selected listview item from another form vb.net

    I don't do much with ListView, so there are likely some variations that I don't know, but I don't believe that you are adding the right thing. The ListView.Items.Add method takes an object, which means you can give it ANYTHING, but I believe that only one thing makes any sense, which is a ListViewItem. Therefore, the code would look like this:
    Code:
     Dim i As New ListViewItem(std.number.ToString)
     ListView1.Items.Add(i)
    You were adding the number and expecting to get back a ListViewItem. You should be adding the ListViewItem. The return from Add would be in the index of the item in the ListView, but you don't care about that.

    I will also note that I'm not sure about the constructor for the ListViewItem. I passed in the std.number in that example, since that seemed most similar to what you showed, but that may not be correct. The number could also be added as just another subitem to the ListViewItem rather than being passed in the constructor. That may be more a matter of how it displays than anything else.
    My usual boring signature: Nothing

  5. #5
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: How to change/update selected listview item from another form vb.net

    Hi
    perhaps the Datagridview is a better choice ?

    here a few ways to work with the Listview
    a) get Items to Textboxes
    b) send them back to the Listview
    c) add new Item

    Code:
      Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
            'get Listview Data to Textbox
            For Each lvItem As ListViewItem In ListView1.SelectedItems
                TextBox1.Text = lvItem.SubItems(0).Text
                TextBox2.Text = lvItem.SubItems(1).Text
                TextBox3.Text = lvItem.SubItems(2).Text
            Next
    
              
        End Sub
    
        Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
            'put Textbox Data back to Listview
            For Each lvItem As ListViewItem In ListView1.SelectedItems
                lvItem.SubItems(0).Text = TextBox1.Text
                lvItem.SubItems(1).Text = TextBox2.Text
                lvItem.SubItems(2).Text = TextBox3.Text
            Next
        End Sub
        Public Sub lvwAddItem(ByVal lvw As ListView, ByVal ParamArray Text() As String)
            'How to use lvwAddItem
            'setup Column Headers
            'With ListView1
            '    .View = View.Details
            '    .LabelEdit = False
            '    .HideSelection = False
            '    .GridLines = True
            '    .FullRowSelect = True
            '    .Columns.Add("Column1", 120)
            '    .Columns.Add("Column2", 120)
            '    .Columns.Add("Column3", 120)
            'End With
            'Then Call
            'lvwAddItem(ListView1,"Text1","Text2,"Text3","Text4")
            With lvw.Items
                .Add(New ListViewItem(Text))
            End With
        End Sub
    
    
        Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
            lvwAddItem(ListView1, TextBox1.Text, _
                                     TextBox2.Text, _
                                        TextBox3.Text)
        End Sub
    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

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