Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
1.18/5 (3 votes)
See more:
I need to print a file to a listbox in groups of 5. Its a combination of 3 letters and i need the listbox to read;
"Line 001: ABC DEF GHI JKL MNO
Line 002: PQR STU VWX YZA"

except replacing the letters with the combinations in the file obviously and since the file can be switched the number of lines is not definite and needs to count up continuously but always hold 3 digits. Please help. Thanks

So far i have something like this but idk if its correct

C#
public partial class frmDNA : Form
   {
       int x = 1;
       String Path;
       StreamReader DNAFile = new StreamReader("/documents/visual studio 2013/Projects/DNA/DNA.txt");
       int Index;
       int Lines = File.ReadLines("/documents/visual studio 2013/Projects/DNA/DNA.txt").Count();
       public frmDNA()
       {
           InitializeComponent();
       }
      private void btnPrint_Click(object sender, EventArgs e)
       {
           String DNA = "Line " + x.ToString("000") + " = ";
           string[] DNAarray = new string[Lines];
           int Begin, end;
           Begin = 0;
           end = 5;
           while (Index < DNAarray.Length && !DNAFile.EndOfStream)
           {
               DNAarray[Index] = DNAFile.ReadLine();
               while (Begin < end)
               {
                   DNA  += DNAFile.ReadLine() + " ";
                   Begin++;
                   x++;
               }
               end += 5;
               Index++;
               lstOut.Items.Add(DNA);
               Begin += 5;
           }



       }

       private void frmDNA_Load(object sender, EventArgs e)
       {

       }
   }


But
C#
lstOut.Items.Add(DNA);
is showing up as wrong saying DNA is unassigned.
Posted
Updated 30-Apr-15 12:21pm
v5
Comments
gggustafson 30-Apr-15 0:03am    
Do you mean "read a file to a ListBox"?
Member 11561872 30-Apr-15 0:03am    
aren't they the same thing?
gggustafson 30-Apr-15 0:19am    
I don't know what "print a file to a ListBox" means. Files are read or written. ListBoxes have items added or deleted. If you are trying to read the contents of a file into a ListBox, then please say so. And supply some code that you have tried (supply code by using the Improve Question button.
Member 11561872 30-Apr-15 0:31am    
Then I mean read. I need to take lines of a text file and output them to a listbox. I can't do actual code atm as I'm away from the computer but I have the file opened and the data put into a simple string array. But I don't know how to make it so each line reads specific text then has a specific number of groups after.
gggustafson 30-Apr-15 9:19am    
See my solution below

1 solution


First, Google "c# read line of text file". Refer to the MSDN example.



I prefer to use a using statement rather than opening and closing a file. Replace the body of the example's while statement with whatever editing you need to perform on line and then add line to the ListBox.



The following example assumes that System.IO and System.Text appear in a using directive. Replace c:\\gustafson\\test.txt with the name of your text file


C#
public void read_file_into_listbox ( )
    {
    StringBuilder   concatenated = new StringBuilder ( );
    int             item_count = 0;
    string          line;
    int             line_count = 0;

    list_box.BeginUpdate ( );
    using ( StreamReader file =
                         new StreamReader (
                            "c:\\gustafson\\test.txt" ) )
        {
        concatenated.Append ( "Line " );
        while ( ( line = file.ReadLine ( ) ) != null )
            {
            item_count++;
            if ( item_count == 1 )
                {
                line_count++;
                concatenated.AppendFormat (
                    "{0:d3}: {1} ",
                    line_count,
                    line.ToUpper ( ).Trim ( ) );
                }
            else if ( item_count == 5 )
                {
                concatenated.AppendFormat (
                    "{0}",
                    line.ToUpper ( ).Trim ( ) );
                list_box.Items.Add (
                    concatenated.ToString ( ) );
                concatenated.Length = 0;
                concatenated.Append ( "Line " );
                item_count = 0;
                }
            else
                {
                concatenated.AppendFormat (
                    "{0} ",
                    line.ToUpper ( ).Trim ( ) );
                }
            }
        if ( item_count > 0 )
            {
            list_box.Items.Add ( concatenated.ToString ( ) );
            }
        }
    list_box.EndUpdate ( );
    }
}


Although string concatenation can occur by using the + operator, more efficient string manipulation can be had by using StringBuilder.



When this code executes, the edited lines of the file will appear in the listbox.



Hope that helps.

 
Share this answer
 
v2
Comments
Member 11561872 30-Apr-15 15:37pm    
but the problem is the file is organized as such:
ABC
DEF
GHI
JKL
MNO
PQR
STU
VWX
YZA

And can be in different formats in other files the user may try. I need the output to be
Line 001: ABC DEF GHI JKL MNO and so on
gggustafson 30-Apr-15 15:50pm    
Read five lines, concatenating them into, say, concatenated_lines. Then, after five lines have been read, add concatenated_lines to the ListBox.
Member 11561872 30-Apr-15 16:05pm    
ok. How would i do that? I've actually been trying while loops. My current code has this:

public partial class frmDNA : Form
{
int x = 1;
String Path;
StreamReader DNAFile = new StreamReader("/documents/visual studio 2013/Projects/DNA/DNA.txt");
int Index;
int Lines = File.ReadLines("/documents/visual studio 2013/Projects/DNA/DNA.txt").Count();
public frmDNA()
{
InitializeComponent();
}

private void btnPrint_Click(object sender, EventArgs e)
{
String DNA;
string[ ] DNAarray = new string[Lines];
int Begin, end;
Begin = 0;
end = 4;
while (Index < DNAarray.Length && !DNAFile.EndOfStream)
{
DNAarray[Index] = DNAFile.ReadToEnd();
while (Begin < end)
{
DNA = "Lines " + x + " = " + DNAarray[Begin];
Begin++;
}
end += 5;
x++;
Index++;
lstOut.Items.Add(DNA);
}



}
}

But the line lstOut.Items.Add(DNA); says DNA is unassigned.
gggustafson 30-Apr-15 23:53pm    
I revised my solution. The ListBox displays the text as you wanted it.
gggustafson 3-May-15 1:25am    
If the solution was useful, please make it your solution and mark it. Thanks.

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