Good evening,

   I'm having problems when trying to save a listbox in a .txt file, follow the code;

IO.Directory.CreateDirectory("C:\Test")
         Dim Caminho As New IO.StreamWriter("C:\Test\Arquin.txt")
         Dim i As Integer
         For i = 0 To ListBox1.Items.Count - 1
                 Caminho.WriteLine(ListBox1.Items.Item(i))
         Next
         Caminho.Close()

The following error occurs:

'Item' is not a member of 'System.Array'

(I am Brazilian, maybe my English is half wrong because I used the google translator)

Edited 1 Year Ago by Reverend Jim: Corrected code formatting (we use markdown syntax)

Created the file, but when I see has nothing recorded in the file

If there are string values the code line should be similar to listBox.Items[i].ToString() Try that.

A ListBox, by definition, contains text. You don't have to convert to String. Try replacing

Caminho.WriteLine(line)

with

Debug.WriteLine(line)

or put a breakpoint at that line and check the values. But don't forget to add

Caminho.Close()

after the loop.

Edited 1 Year Ago by Reverend Jim

It did not work ...

I think it is not working because I was wearing a theme in vb.net to change the appearance

Instead of Caminho.WriteLine(ListBox1.Items.Item(i)) use Caminho.WriteLine(ListBox1.Items(i)) which can return you the value of the lisboxitem at the position of i.

Did tests and found that the problem is that I was not going through the listbox, maybe it's because my listbox is of topics that I put in vb.net

This article has been dead for over six months. Start a new discussion instead.