Assign Listbox values in vector

Asked

Viewed 646 times

0

By playing the items one by one within an array, position 0 with 0 and so on, it works correctly. But the code has a malfunction because ,in the first iteration is normal in the second of the error:

Invalidargument=Value of '1' is not Valid for 'index'. It is certain that position 1, is not index, but I no longer want to select the index from the list.

for (j = 0; j <= notas_operacao; j++)   <== quantidade de notas no listbox
            {
                foreach (var item in listBox_Nfe.Items) <==== corre listBox
                {
                    notas[j] = listBox_Nfe.Items[j].ToString(); <==deveria jogar list item atual em posição de vetor em j
                    MessageBox.Show(notas[j]); => debug
                }
            }
  • Only with narrow stretch it becomes difficult to evaluate.

  • It is simply this part of my application that is failing, this time when I try to take all the items in a listbox and play in a vector string

  • 1

    http://answall.com/help/mcve

  • 1

    If you make a foreach of each element of listBox, pq not allocate in notas the variable item of foreach? Actually you don’t even need the external, just the foreach just, just put a variable j outside and j++ within the foreach

  • That’s exactly what it was.

1 answer

2

Is this what you want:

foreach (var item in listBox_Nfe.Items) <==== corre listBox
{
    int j = 0;
    notas[j] = item.ToString();
    MessageBox.Show(notas[j]);
    j++;
}

The information you give isn’t much, so I could be wrong.

  • That’s it, just with a small adjustment notes[j] = listBox_Nfe.Items[j]. Tostring(); thank you very much

  • The variable of the foreach item should equal listBox_Nfe.Items[j]

Browser other questions tagged

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