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)

Recommended Answers

All 9 Replies

Try

For Each line As String In ListBox1.Items
    Caminho.WriteLine(line)
Next

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.

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.

I've already tried the code I suggested and it writes the lines of text to the file as requested.

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

I was not going through the listbox

I don't know what you mean by that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.