user911072
user911072

Reputation: 13

C# listbox split to another 2 listboxes

Problem: I have 1 ListBox which loads a text file contain:

What I want to do is, once I've loaded the text file into the list box, I want to have the 'ip' to go into a different listbox and the 'port' into a different listbox. This is first time working on a project like this.

Upvotes: 1

Views: 977

Answers (2)

A-Dubb
A-Dubb

Reputation: 1709

// if you wanted to do it with LINQ. 
// of course you're loading all lines 
// into memory at once here of which 
// you'd have to do regardless
var text = File.ReadAllLines("TestFile.txt");
var ipsAndPorts = text.Select(l => l.Split(':')).ToList();

ipsAndPorts.ForEach(ipAndPort =>
{
    lstBoxIp.Items.Add(ipAndPort[0]);
    lstBoxPort.Items.Add(ipAndPort[1]);
});

Upvotes: 1

Orn Kristjansson
Orn Kristjansson

Reputation: 3485

Something like

        using (StreamReader sr = new StreamReader("TestFile.txt"))
        {
            String line;
            // Read and display lines from the file until the end of
            // the file is reached.
            while ((line = sr.ReadLine()) != null)
            {
               string[] ipandport = line.split(":");
               lstBoxIp.Items.Add( ipandport[0] );
               lstBoxPort.Items.Add( ipandport[1] );
            }
        }

Upvotes: 0

Related Questions