Hello,

I need some help getting started with saving all the items in a listview with a savefiledailog. The items in the list look like this IP , port. The IP and port are in different columns. The IP is in the first column and the IP is in the 2nd.

Dani AI

Generated

The error comes from indexing the SelectedItems collection with a loop bound based on Items.Count. SelectedItems contains only selected rows, so using the Items count will walk past the SelectedItems range and cause the "index" exception. correctly suggested iterating the Items collection (or iterating SelectedItems if the intent is to export only selected rows). Also always check that a ListViewItem has at least two subitems before accessing index 1.

Example (VB.NET, robust: checks subitems, builds lines, writes file with UTF-8):

Dim sb As New System.Text.StringBuilder()

For Each itm As ListViewItem In ListView1.Items
    Dim ip As String = itm.Text
    Dim port As String = If(itm.SubItems.Count > 1, itm.SubItems(1).Text, String.Empty)
    sb.AppendLine(ip & ":" & port)
Next

Using sfd As New SaveFileDialog()
    sfd.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
    If sfd.ShowDialog() = DialogResult.OK Then
        System.IO.File.WriteAllText(sfd.FileName, sb.ToString(), System.Text.Encoding.UTF8)
    End If
End Using

Notes and cautions: ensure the ListView is in Details mode so subitems are populated as expected; guard SubItems.Count to avoid index errors; if IPv6 addresses are possible, avoid a raw ":" separator (use brackets like "[::1]:80" or a different delimiter); use StringBuilder for performance on many items; if the ListView uses VirtualMode export from the backing data instead of Items. posted the original question and requested the code; this snippet aims to be a safe, straightforward way to save every item to a text file.

Recommended Answers

All 5 Replies

Post your code here and someone will help you.

for (int i = 0; i < listView1.Items.Count; i++)
            {
                write += listView1.SelectedItems[i].Text + ":" + listView1.SelectedItems[i].SubItems[1].Text;
                write += Environment.NewLine;
            }
            Stream myStream; 
            SaveFileDialog saveFileDialog1 = new SaveFileDialog(); 
            saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; 
            if (saveFileDialog1.ShowDialog() == DialogResult.OK) 
            { 
                if ((myStream = saveFileDialog1.OpenFile()) != null) 
                { 
                    StreamWriter wText = new StreamWriter(myStream); 
                    wText.Write(write); myStream.Close(); 
                }
            }

When I start the code I get an error saying
InvalidArgument=Value of '1' is not valid for 'index'.
Parameter name: index

Pointing to write += listView1.SelectedItems.Text + ":" + listView1.SelectedItems.SubItems[1].Text;

You said you want to save all items from a listView, not SelectedItem. So why are you using Selecteditem property?

What you hav to do it to loop through all the rows and get the values from all the columns (while in a particualar row), like this:

//...
  string text = null;
  for (int i = 0; i < listView1.Items.Count; i++)
       text += listView1.Items[i].Text + ": " + listView1.Items[i].SubItems[1].Text + Environment.NewLine;    
//...

This should do the trick
Mitja

Oh, I didn't notice that thanks.

You are welcome :)
bye,bye
Mitja

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.