Click here to Skip to main content
15,498,506 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:

C#
private void Setup_Load(object sender, EventArgs e)
{
    string path = @"Data/text.txt";
    using (StreamReader stRead = new StreamReader(path))
    {
        while (!stRead.EndOfStream)
        {
            checkedListBox1.Items.Add(Convert.ToString(stRead.ReadLine()));
        }
}


Thanks!
Posted
Updated 20-Aug-12 14:49pm
v2
Comments
Sergey Alexandrovich Kryukov 20-Aug-12 19:33pm     CRLF
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? --SA
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
C#
Items.Add()//[This is wrong]

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

Check my code below.

C#
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
               counter++;
           }

           //close the file
           file.Close();
       }


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
 
Comments
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