Click here to Skip to main content
15,909,651 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
VB
Dim intItemExists As Integer = -1
        Dim strTransferItem As String = ""
        strTransferItem = ListBox1.Items(ListBox1.SelectedIndex).ToString
        If ListBox2.FindStringExact(strTransferItem) = -1 Then
            ListBox2.Items.Add(strTransferItem)
        Else

            'Item found and already exists in the target listbox            
            MessageBox.Show(String.Format("Item ""{0}"" is already in the target list box", strTransferItem))
            LCOUNT2.Text = ListBox1.Items.Count


This is my coding to move items from listbox1 to another . now the problem is if i dont select an item from the lbox, then the aplication stops running
Posted
Updated 10-Dec-11 18:27pm
v2
Comments
jus coding 11-Dec-11 0:30am    
so can u js tell me how can i over come with this error.

It's very simple you just check ListBox Item is Selected or Not before executing your code.Try this code :
VB
If ListBox1.SelectedItems.Count > 0 Then
   'Your Code goes here
End If

Or
VB
If ListBox1.SelectedItems.Count < 1 Then Exit Sub

I hope it will help you. :)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-Dec-11 0:48am    
This is a different variant, will also work, my 5.
--SA
Manoj K Bhoir 11-Dec-11 0:54am    
Thank You SAKryukov! :)
Abhinav S 11-Dec-11 1:32am    
My 5!
Manoj K Bhoir 11-Dec-11 1:46am    
Thank You Abhinav!
Monjurul Habib 11-Dec-11 1:45am    
5!
VB
If ListBox2.FindStringExact(strTransferItem) = -1 Then
    ListBox2.Items.Add(strTransferItem)
Else

This code will not work when you don't select anything.

Change your condition to handle the case when no item is selected.
 
Share this answer
 
Comments
Manoj K Bhoir 11-Dec-11 0:43am    
I agree!My 5 :)
Abhinav S 11-Dec-11 1:32am    
Thanks Manoj.
Sergey Alexandrovich Kryukov 11-Dec-11 0:47am    
Three answers at the same time. My 5.
--SA
Abhinav S 11-Dec-11 1:32am    
Thank you SA.
Monjurul Habib 11-Dec-11 1:46am    
5!
First of all, this is not nice to just say "stops working". Are you a software developer or not? If you are not, this is off-topic. If you are, provide a comprehensive exception information. Did you even run it under debugger? If you don't do it, you can relax and stop software development.

The solution can be found in your question. Look at ListBox1.Items(ListBox1.SelectedIndex). You explicitly assume here that some item is selected. If this is not so, selected index is negative, and Items property throws out-of-range exception. You can check up that a selected index if non-negative before this line and, for example, exit the method if it is negative.

—SA
 
Share this answer
 
Comments
Manoj K Bhoir 11-Dec-11 0:55am    
My 5! Because i agree with above explaination.
Sergey Alexandrovich Kryukov 11-Dec-11 1:12am    
Thank you, Manoj.
--SA
Abhinav S 11-Dec-11 1:32am    
My 5!
Sergey Alexandrovich Kryukov 11-Dec-11 1:34am    
Thank you, Abhinav.
--SA
Monjurul Habib 11-Dec-11 1:46am    
5!

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