Click here to Skip to main content
13,052,956 members (72,176 online)
Rate this:
Please Sign up or sign in to 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)

Posted 20-Aug-12 13:04pm
Updated 20-Aug-12 13: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

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 20 Aug 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100