Sunday, May 3, 2020

C# Tutorial-Reading Lines from Notepad

I have four controls txtLRN(textbox), Save(button), txtName(textbox), and btnreadtxt(button).
I want to mass produce barcodes by putting automated texts in my textboxes. I can do this by
using the btnreadtxt button. The code below is for the command button name btnreadtxt.

private void btnreadtxt_Click(object sender, EventArgs e)
        {
            try {
             
                using (StreamReader sr = new StreamReader("d:/Demo.txt")) // StreamReader is
                   // inherited from TextReader that provides method to read a character,
                   //block, line, or all content.
                  {
                      string line;   //variable type string, variable name line
                     string result;   //variable type string, variable name result
                    while ((line = sr.ReadLine()) != null)  // continue reads if the value is not null,
                                                          //and stop to the current line if null value is present.
                    {
                        string[] tempArray = line.Split(' ');  // reads blank character as another
                                                                            //index.
                     
                        result = line.Substring(0, line.IndexOf(","));  //The Substring method is used
                                                                           //to grab characters
                                                                            //starts from zero based index.
                                // Line.IndexOf(",")- Grabs number of characters starting                                                                                //from zero-based index of the first occurrence of comma.

                         Console.WriteLine(result); //Writes the specified string value, followed by
                                           //the current line terminator, to the standard output stream.

                        txtLRN.Text = tempArray[0]; //locate the array at zero index of my strings.
                        txtLRN.Text = txtLRN.Text.Substring(0, (txtLRN.TextLength - 1)); 
                           //equals to 12 characters comprises of 12 array length with 0-11 indices.

                        txtName.Text = tempArray[1] + " " + tempArray[2] + " " + tempArray[3]
                       + " " +tempArray[4];
                         
// We need to arrange our strings as example 119973120275,  ARONG AARON JAMES
// LAGARAS in notepad with zero index 119973120275, first index(ARONG) second 
//index(AARON) third index(JAMES) fourth index(LAGARAS) else sr.ReadLine())
// returns null.

                        Save.PerformClick();
                                     //Save button generates barcode as program underlies in it.
                     
                    }
                  }
            }
            catch (Exception ee)   // The catch keyword indicates the catching of an exception
            {
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(ee.Message);   //To print a message to the console, we use
                                                                   // the WriteLine()method of the Console class.
            }
            Console.Read();  // Console. Read() Method is used to read the next character
                                      // from the standard input stream.
      }
    }
}
From notepad from d:/Demo.txt.
119973130050, AMISTAD JERICK AMORES Y
119979120026, AMORO KYLE RYAN BASCON
119982120016, ANDALES CLARK KHEN PONCE
119973120275, ARONG AARON JAMES LAGARAS
119975120614, BARGAMENTO CRIS RESTY BERNARDO

Click this YouTube video link for more understanding.

No comments:

Post a Comment