hiya.

I'm trying to read in a text file, then split each line and output it to a listbox. However its not that easy.

every 7 lines is about one subject so i need the listbox to write the data out like this.

line 1, line 2, line 3, line 4, line 5, line 6, line 7 \\then move onto the next subject. 

i can read in the file using streamreader

// Read the file.
        System.IO.StreamReader modules =
        new System.IO.StreamReader("C:/Documents and Settings/Paul/My Documents/uni/Reynolds10178862/C#Application/Modules/Modules/modules_v2.txt");

and i am using this while loop to split the file.

while ((line = modules.ReadLine()) != null)
            {

                counter++;
                String[] splittedint = line.Split('\t');

                String mcode = line.Split('\t')[0];

                reportbox.Items.Add(mcode);

            }

but this seems to write out the first symbol from each line, rather then each line. I was thinking maybe i need to assign each line to a string to do what is required but im not entirely sure.

can anyone help?

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Why split the lines? As I understand it you have to concatenate 7 lines add that to a listbox and then continue with the next 7 lines. Or am I wrong?

Thinking about yes you are corect. However how do i get the linbes to write out in the following format

line1, line2, line3, line4, line5, line6,line7
line8,line9...................................................etc

Does each line need to be assigned to a string?

please help someone please please help. i really need to get this done. please:'(

Is it something like this you want :

string mcode = string.Empty;
            int Itemcounter = 0;
            while ((line = modules.ReadLine()) != null)
            {
                mcode += line;
                Itemcounter++;
                if ( Itemcounter == 7 )//one line completed so start over again
                {
                    reportbox.Items.Add(mcode);
                    Itemcounter = 0;
                    mcode = string.Empty;
                }
            }

Is it something like this you want :

string mcode = string.Empty;
            int Itemcounter = 0;
            while ((line = modules.ReadLine()) != null)
            {
                mcode += line;
                Itemcounter++;
                if ( Itemcounter == 7 )//one line completed so start over again
                {
                    reportbox.Items.Add(mcode);
                    Itemcounter = 0;
                    mcode = string.Empty;
                }
            }

that worked thank you.

One other question if i may ask.

i have a form that has 6 textboxes and a dropdown box, i want the drop down box box to be linked to module code. the text file is set out in the following order;

module code
module status
title
leader
info
no enrolled
no permitted

in my form i have;

module code - drop down here
module status - Text box here
module title - Text box here
module leader - Text box here
No of students enrolled - Text box here
No of students permitted - Text box here
Additional info - Text box here

I need it so that when you select a module code, then the other 6 lines appear in the appropriate box, so module status in the module status textbox etc

after i have done that i need it so that you can edit the information in the textboxes and then save it back to the module_v2.txt file.

Hello, can you post what you have so far? Any tries in code, which 'don't want to work proper'.

I can populate the textbox by putting in every number from 0 to 7.

but this means doing something like this;

// i read in the file using streamreader and called it modules

while ((line = modules.ReadLine()) != null)
{
    if (counter == 0 || counter == 7 //etc)
       { 
         output = line;
          mcodecombo.Items.Add(output);
       }
}

Doing the if function as above means that you have millions of || counter ==
which looks nasty can you think of a better way?

i then need to make it so that when you click the item in the combobo it populates the other text boxes with the appropriate lines as stated in a previous comment. i have absolutly no idea where to start with that.

For millions different cases for counter value - I'd better used switch.
The second thing, that I've wanted to point to is this:

output = line;
mcodecombo.Items.Add(output);

Are you sure, that you need to re-assign the value of line variable to the output and then add it to the mcodecombo? Maybe it's better to do this:

mcodecombo.Items.Add(line);

for this:

i then need to make it so that when you click the item in the combobo it populates the other text boxes with the appropriate lines

you should check the ComboBox Events and find the most suitable event for your need. And then write the code for it.

For millions different cases for counter value - I'd better used switch.
The second thing, that I've wanted to point to is this:

output = line;
mcodecombo.Items.Add(output);

Are you sure, that you need to re-assign the value of line variable to the output and then add it to the mcodecombo? Maybe it's better to do this:

mcodecombo.Items.Add(line);

for this:

you should check the ComboBox Events and find the most suitable event for your need. And then write the code for it.

I have changed what i was doing slightly to make life easier (i hope)

i have changed all the textboxes in my form to comboboxes so you can choose a line to edit.

then when you click my save button i need it to take the information from any boxes that have changed. and change the appropriate line in the .txt file. i am really new to C# and this program is really hard.

im not expecting anything solid as answer just need to be given a kickstart as to where to start.

i have populated each combobox with the appropriate line's i.e. the mcodecombo box has all the module codes from the .txt file in it.

then when you click my save button i need it to take the information from any boxes that have changed. and change the appropriate line in the .txt file. i am really new to C# and this program is really hard.

Well, let's imagine the algorithm of this:
1. Memorize the initial state of your combo-boxes and after pressing the button - check if it changed.
2. If it changed - go to file, find the line and change the values there ...

For finding the certain line in file, you should iterate through your file and check the lines, if they are fit to your condition.

Then if the line, that you're going to change and new line has same length, then you can rewrite that line, by seeking and setting the certain position of where to write.

But if not ... In that case - you should, e.g. memorize the data after the line, that you gonna change, rewrite that line and then after that line, write the data, which you memorized before.
Is that what you're looking for?

Well, let's imagine the algorithm of this:
1. Memorize the initial state of your combo-boxes and after pressing the button - check if it changed.
2. If it changed - go to file, find the line and change the values there ...

For finding the certain line in file, you should iterate through your file and check the lines, if they are fit to your condition.

Then if the line, that you're going to change and new line has same length, then you can rewrite that line, by seeking and setting the certain position of where to write.

But if not ... In that case - you should, e.g. memorize the data after the line, that you gonna change, rewrite that line and then after that line, write the data, which you memorized before.
Is that what you're looking for?

Yes that sounds about right. i have no idea where to start with it all though.

Hello, foxypj.
Please, try to put there some effort ... your tries, suggestions and such.
The main thing in my help is that I'm trying to push you in right direction (or show the possible ways to go) to get your solution. I can't push you if I have no idea: are you moving in a right way or my explanation leads you in wrong direction .. or maybe you're standing on the same place and waiting for ready-made solution :P

This article has been dead for over six months. Start a new discussion instead.