JohnDueDueDue
JohnDueDueDue

Reputation: 17

C# Load .txt file and show in listview

so what I'm trying to do is load a .txt file and once the .txt file is loaded it will show the contents from the .txt file in a listView.

Here is my load code.

        List<String> proxies = new List<string>();
    private void loadProxiesToolStripMenuItem_Click(object sender, EventArgs e)
    {
        loadProxies();
    }

    private void loadProxies()
    {
        this.Invoke(new MethodInvoker(delegate
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "TXT files|*.txt";
            ofd.Title = "Load Proxies";
            var dialogResult = ofd.ShowDialog();
            if (dialogResult == DialogResult.OK)
            {
                proxies = new List<string>();
                Parallel.ForEach(System.IO.File.ReadLines(ofd.FileName), (line, _, lineNumber) =>
                {
                    if (line.Contains(":"))
                    {
                        //loadedCombo.Add(line);
                        proxies.Add(line);
                    }
                    else
                    {
                        //MessageBox.Show("Hmm, thats not a combolist - please try again");
                    }
                });
            }

            txt_proxies.Text = "Proxies Loaded: " + proxies.Count.ToString();

        }));
    }

and I'm wanting it to show in the listView which is named "proxyView".

So what I'm trying to say it, I can get the .txt to load and it changes the count but it's not adding the contents from the .txt file into the listview.

Thanks much appreciated.

Upvotes: 1

Views: 2137

Answers (1)

Reza Aghaei
Reza Aghaei

Reputation: 125197

To add an item to a ListView you can use yourListView.Items.Add(text)

For example:

private void loadProxies()
{ 
    OpenFileDialog ofd = new OpenFileDialog();
    ofd.Filter = "TXT files|*.txt";
    ofd.Title = "Load Proxies";
    var dialogResult = ofd.ShowDialog();
    if (dialogResult == DialogResult.OK)
    {
        foreach (var line in System.IO.File.ReadLines(ofd.FileName))
        {
            if (line.Contains(":"))
                proxyView.Items.Add(line);
        }
    }
}

Upvotes: 3

Related Questions