Random Line of Text Selector in C#

This tutorial will teach you how to create a program that selects a line of text from a text file using C#. So, now let's start this tutorial! 1. Let's start with creating a Windows Form Applicationin C# for this tutorial by following the following steps in Microsoft Visual Studio 2010: Go to File, click New Project, and choose Windows Application. 2. Next, add a button named Button1 to have the random line selector. ou must design your interface like this: design 3. For Button1, have this code below to have the random line selector.
  1.         private void Button2_Click(object sender, EventArgs e)
  2.         {
  3.             //display a new OpenFileDialog to allow the user to select a .txt Text file for the input of the tool.
  4.             using (OpenFileDialog fo = new OpenFileDialog())
  5.             {
  6.  
  7.              //filter to only text file
  8.                 fo.Filter = "Text Files | *.txt";
  9.                 fo.RestoreDirectory = true;
  10.              //have only one line to select
  11.                 fo.Multiselect = false;
  12.               //show the dialog
  13.                 fo.ShowDialog();
  14.                // if filename is not null
  15.                 if (fo.FileName != null)
  16.                 {
  17.                   //create a new list of string named 'lines' and read the entire text file to the list
  18.                     List<string> lines = new List<string>();
  19.                     using (StreamReader sr = new StreamReader(fo.FileName))
  20.                     {
  21.                         while (sr.Peek() != -1)
  22.                         {
  23.                             lines.Add(sr.ReadLine());
  24.                         }
  25.                     }
  26.                     //Finally we get a random number using our globally available 'random' variable and output the line at the index of that random line
  27.                     int number = System.Convert.ToInt32(random.Next(lines.Count));
  28.                     MessageBox.Show(lines[number]);
  29.                 }
  30.             }
Output: output For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:donbermoy@engineer.com Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment