Results 1 to 5 of 5

Thread: [RESOLVED] ListView error while adding new item

  1. #1

    Thread Starter
    Lively Member chipp's Avatar
    Join Date
    May 2012
    Posts
    78

    Resolved [RESOLVED] ListView error while adding new item

    ca somebody tell me what's wrong with my code?

    here's the code and forms design:

    Name:  form1 design.jpg
Views: 120
Size:  37.8 KB
    Code:
    Imports System.Threading
    
    Public Class Form1
    
        Dim txtqty_first As Boolean = False
        Public i As Integer
    
        Private Sub form1_load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            cbProduct.Items.Clear()
            txtQty.Text = 0
            txtPrice.Text = 0
            txtDisc.Text = 0
            txtVAT.Text = 10
            i = 0
        End Sub
        Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
            Dim j As Integer
            prog_bar.Maximum = 25
    
            For j = 1 To prog_bar.Maximum
                prog_bar.Value = j
                Thread.Sleep(150)
            Next
    
            i = i + 1
            Me.Hide()
            Form2.Show()
            prog_bar.Value = 0
        End Sub
    
        Private Sub form1_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Click
            If txtQty.Text = "" Then
                txtQty.Text = 0
            End If
        End Sub
    
        Private Sub txtQty_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtQty.Click
            If txtQty.Text = 0 Then
                txtQty.Text = ""
            End If
        End Sub
    
        Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
            txtQty.Text = 0
            txtPrice.Text = 0
            txtDisc.Text = 0
            txtVAT.Text = 10
        End Sub
    
        Public Sub txtqty_and_txtdisc_keyup(ByVal sender As System.Object, ByVal e As Windows.Forms.KeyEventArgs) Handles txtQty.KeyUp, txtDisc.KeyUp
    
        End Sub
    End Class
    Name:  form2 design.jpg
Views: 126
Size:  34.1 KB
    Code:
    Public Class Form2
        Private Sub form2_load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            With Form1
                lvProductSummary.Items.Add("No. " & .i)
                If .i = 1 Then
                    lvProductSummary.Items(0).SubItems.Add(My.Computer.Info.OSPlatform)
                Else
                    lvProductSummary.Items(1).SubItems.Add(My.Computer.Info.OSVersion)
                End If
            End With
        End Sub
    
        Private Sub form2_formclosed(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.FormClosed
            Form1.Show()
        End Sub
    End Class
    here's the error:
    Name:  runtime error.jpg
Views: 122
Size:  34.6 KB

    i seems like my 2nd listview initialization didn't work. can somebody tell me why?
    Last edited by chipp; Mar 15th, 2015 at 01:33 PM.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,394

    Re: ListView error while adding new item

    The error message tells you that lvProductSummary.Items(1) doesn't exist

  3. #3

    Thread Starter
    Lively Member chipp's Avatar
    Join Date
    May 2012
    Posts
    78

    Re: ListView error while adding new item

    Quote Originally Posted by .paul. View Post
    The error message tells you that lvProductSummary.Items(1) doesn't exist
    that's why i said it looks like that my 2nd initialization doesn't executed, bcoz the code:

    lvProductSummary.Items.Add ("No. " & i)

    should be executed every time the form loads, i.e. if i run it twice, it should add another row (2nd row) in the listview.

    am i right?

  4. #4
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,282

    Re: ListView error while adding new item

    When you close Form2, it is disposed of. The next call to Form2.Show spawns another, different default instance of Form2, with an empty ListView. You add a single Item to it, but then try to add a sub-item to a second, non existent Item.

  5. #5

    Thread Starter
    Lively Member chipp's Avatar
    Join Date
    May 2012
    Posts
    78

    Re: ListView error while adding new item

    thank you giving me revelation

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