Index Error in listbox

Asked

Viewed 68 times

1

My application has the following starting code:

BuscaDiretorioPastaOriginal("Diretorio") // isso seria uma função **recursiva** de diretorios
BuscaDiretorioPastaPath("Diretorio") // isso seria uma função **recursiva** de diretorios

This function lists in two listBox, up to there all blz, but dirty the error in this function

Private Sub AnalisarNovoArquivo()

 CTN.LSTNewAqui.Items.Add(CTN.LSTOG.SelectedItem.ToString)  
 CTN.LSTOG.Items.Remove(CTN.LSTOG.SelectedItem.ToString)

 CTN.LSTNewAqui.SelectedIndex = 0 
 ListaArray(0) = CTN.LSTNewAqui.SelectedItem.ToString  'Adiciona ao Index 0 na lista de array

end sub 

This part of the function states that the value is not valid for selectedIndex, the reason would be from the top there where it has CTN.Items.Remove(CTN.LSTOG.SelectedItem.ToString).

OBS: function works correctly if used a Try, but how it returns this error?

Function that accuses error:

    Try
        For ItemsI = 0 To CTN.LSTOG.Items.Count - 1
            CTN.LSTOG.SelectedIndex = ItemsI
            CTN.LSTPath.SelectedIndex = ItemsI

            If CTN.LSTOG.SelectedItem.ToString <> CTN.LSTPath.SelectedItem.ToString Then
                Notificar("Existem novos arquivos a serem adicionados ao projeto.")
                AnalisarNovoArquivo()
                IniciarBuscaDeNovoArquivo()
                SplitCaminho()
                rem  VerificarDiretorios()
                CopiarNovoArquivo()
            End If
        Next
    Catch ex As Exception


    End Try
  • 1

    What error has appeared?

  • 2

    It doesn’t work properly if you use one Try, you hide the mistake with it. Never Use Catch without doing anything inside it. And just do something useful, something that solves the problem or gives information that helps solve the problem. Otherwise it only hurts. And almost always capture a Exception is another mistake because there are rare cases that we can do something useful with such a generic exception. To learn about exceptions start (but don’t stop at this post, follow the links) reading this answer: http://answall.com/questions/30124/

  • Like I understand but or less I read a lot the MSDN but I left it pure for the simple fact that I put something in the application to, already leaves pure the application runs normally, without any error. Error was Value ex:31 is not valid for Selectedindex

1 answer

2


Private Sub AnalisarNovoArquivo()

  CTN.LSTNewAqui.Items.Add(CTN.LSTOG.SelectedItem.ToString)  
  CTN.LSTOG.Items.RemoveAt(CInt(CTN.LSTOG.SelectedIndex))

  CTN.LSTNewAqui.SelectedIndex = 0 
  ListaArray.Items.Insert(0, CTN.LSTOG.SelectedItem.ToString)

end sub 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.