Click here to Skip to main content
15,508,644 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to import whole lines from text file into checked listbox as items.

To be accurate every line contains Name, Surname, and number values separated with spaces.
I would like to import those lines into the checked listbox as items and work with their values later (read them etc..).

Can someone help me or maybe give tip on differend "technique" how to do that?

My little try on importing which failed and do nothing:

private void Setup_Load(object sender, EventArgs e)
    string path = @"Data/text.txt";
    using (StreamReader stRead = new StreamReader(path))
        while (!stRead.EndOfStream)

Updated 20-Aug-12 14:49pm
Sergey Alexandrovich Kryukov 20-Aug-12 19:33pm    
What's the problem?
Who knows what exactly you need? You would need to specify it formally. How some parts of text are mapped to the items? What the checked or unchecked status of each check box should be mapped to? I guess if you give yourself answers to those questions, you will be able to solve the problem by yourself. If not, what is the issues?
LosEagle 20-Aug-12 19:50pm    
Sorry for that. Updated the whole thing.

1 solution

This is very easy. Look at my solution. Here I am trying to read a text file with countries listed and then add each line to the CheckedListBox.

You cannot use
Items.Add()//[This is wrong]

The correct way is ,
Items.Insert(<index>, <string>)</string></index>
with proper parameters.

Check my code below.

private void button1_Click(object sender, EventArgs e)

           int counter = 0;
           string line;

           // Read the file and display it line by line.
           System.IO.StreamReader file = new System.IO.StreamReader(@"C:\Users\srikaran\Desktop\Countries.txt");

           //Now read the file line by line
           while ((line = file.ReadLine()) != null)
               //add the line to CheckedListBox, you need to pass the parameters "index" & "string"
               ChkListBox1.Items.Insert(counter, line);

               //increase the index

           //close the file

By default the items are not checked.

Try this code and let me know if you have any issues getting this output.

Good luck.
Share this answer
LosEagle 20-Aug-12 19:56pm    
Thanks it all makes sense for me. However it loads nothing for some reason :(. Still thanks for answer. Didn't programmed for long time so I will just make sure everything is fine.
Swinkaran 20-Aug-12 20:14pm    
That's cool. Try and let me know how you go with it. Also please click on "Accept Answer"(the green button) for my solution. Thanks.
LosEagle 20-Aug-12 20:22pm    
Thanks so much!! I've just had file named "text.txt" instead of just "text" and renamed and now it works!!
Swinkaran 20-Aug-12 20:25pm    
Great. Thanks for your feedback.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900