My teacher said that there was a bug in the code where if you run the program and didn't select an item then click a button which you selected an item in the listview and a message box appears, there will be an error where it will say ''InvalidArgument=Value of '0' is not valid for 'index'.
Parameter name: index''. Also, how can I not display a blank text from a blank textbox in the listview while a message box shows and tells that there is an error in not putting any texts in the textbox?

NOTE: This is a Windows Forms Application program and I am programming it in the Visual Basic 2010 Express version.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    ProductListView.View = View.Details
    ProductListView.GridLines = True
    ProductListView.FullRowSelect = True

    ProductListView.Columns.Add("Product Name", 100)
    ProductListView.Columns.Add("Price", 70)
    ProductListView.Columns.Add("Quantity", 70)

    Dim prdt As String() = New String(3) {}
    Dim slctprdt As ListViewItem
    prdt(0) = "Product 1"
    prdt(1) = "100"
    prdt(2) = "10"
    slctprdt = New ListViewItem(prdt)
    ProductListView.Items.Add(slctprdt)

    prdt(0) = "Product 2"
    prdt(1) = "200"
    prdt(2) = "20"
    slctprdt = New ListViewItem(prdt)
    ProductListView.Items.Add(slctprdt)

End Sub

Private Sub SlctBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SlctBtn.Click

        Dim prdtName As String
        Dim price As String
        Dim qtty As String

        prdtName = ProductListView.SelectedItems.Item(0).SubItems(0).Text
        price = ProductListView.SelectedItems.Item(0).SubItems(1).Text
        qtty = ProductListView.SelectedItems.Item(0).SubItems(2).Text

        MsgBox(prdtName & " " & price & " " & qtty)

End Sub

Private Sub AddPrdtBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddPrdtBtn.Click

    Dim prdt As String() = New String(3) {}
    Dim slctprdt As ListViewItem
    prdt(0) = PrNTBox.Text
    prdt(1) = PTBox.Text
    prdt(2) = QTBox.Text
    slctprdt = New ListViewItem(prdt)
    ProductListView.Items.Add(slctprdt)

End Sub

Check ProductListView.SelectedItems.Count. If it's zero, exit your procedure.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.