Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Forms WinForm
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)
        {
            checkedListBox1.Items.Add(Convert.ToString(stRead.ReadLine()));
        }
}
 
Thanks!
Posted 20-Aug-12 14:04pm
Edited 20-Aug-12 14:49pm
v2
Comments
Sergey Alexandrovich Kryukov at 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?
--SA
LosEagle at 20-Aug-12 19:50pm
   
Sorry for that. Updated the whole thing.

1 solution

Rate this: bad
good
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
                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.
  Permalink  
Comments
LosEagle at 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 at 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 at 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 at 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
0 Sergey Alexandrovich Kryukov 279
1 OriginalGriff 190
2 DamithSL 185
3 Peter Leow 105
4 Kornfeld Eliyahu Peter 105
0 OriginalGriff 7,355
1 DamithSL 5,254
2 Sergey Alexandrovich Kryukov 4,942
3 Maciej Los 4,906
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web02 | 2.8.141223.1 | Last Updated 20 Aug 2012
Copyright © CodeProject, 1999-2014
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