Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dim myintlistNew As New List(Of Integer)


Private Sub ListView2_Click(sender As Object, e As EventArgs) Handles ListView2.Click

        Dim identifier1 As Integer = 0
        If CB_Stock_Update_Id.Items.Count = 10 Then
            myintlistNew.Clear()
        End If
            If myintlistNew.Count > 0 Then
                For y As Integer = 0 To myintlistNew.Count - 1
                    If myintlistNew(y) = CInt(ListView2.SelectedItems.Item(0).SubItems(0).Text) Then
                        identifier1 = 1
                    End If
                Next
                If identifier1 = 0 Then
                    myintlistNew.Add(CInt(ListView2.SelectedItems.Item(0).SubItems(0).Text))
                End If
            Else
                myintlistNew.Add(CInt(ListView2.SelectedItems.Item(0).SubItems(0).Text))
            End If
            CB_Stock_Update_Id.Items.Clear()
            For z As Integer = 0 To myintlist.Count - 1
                CB_Stock_Update_Id.Items.Add(myintlistNew(z))
            Next
            Panel_Update_Stocks.Enabled = True


    End Sub


What I have tried:

I am already using this code in listview1 and send information in combobox without duplication. But in listview 2 it catches error
Posted
Updated 9-May-19 7:54am
Comments
Richard Deeming 9-May-19 11:05am    
Most likely cause is that ListView2 doesn't have any selected items.

Without your data, we can't fix your problem - and we have no access to your data!

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. A quick Google for "Visual Studio" and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Quote:
Vb.net: index was out of range must be nonnegative and less than the size of the collection parameter index

First of all, when you ask for help, it is a nice idea to tell where is the error. Otherwise, we can only guess.
The error is exactly what says the message.
Somewhere, you have an array or collection and you try to access a position that does not exist. The best move is to use the debugger and inspect variables to see where it goes wrong.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Visual Basic / Visual Studio Video Tutorial - Basic Debugging - YouTube[^]
Visual Basic .NET programming for Beginners - Breakpoints and Debugging Tools[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900