C# Tutorial – Read Text File Line by Line in Windows Forms and Visual Studio

Hi, in this tutorial we will make a fun application using C# and Windows Form in Visual Studio. This is a One Liner Joke App, in this app we will load a external joke file which has a 100 jokes separated by line and we will load that file int to the program and then display one line of text at a time to the display. We will use various components from visual studio to include in the windows form app such as List Box, Labels, Images, Timer and Button. You can scroll down and select the jokes you want to read also this application will have a feature to automatically select the items from the list box at a interval from the timer. We will also demonstrate how to make import custom fonts inside of windows form and display the information on a label using it. Hopefully you will have learned something new using this tutorial.

Lesson Objective –

  • Create a On Line Joke Reading App
  • Load external text file
  • Read lines from the text file line by line
  • Display the lines using labels
  • Load external font file and use it to display the texts with it
  • Load lines inside of a list box
  • Use a timer to go through the list box of jokes and display them on the screen
  • Create the application using labels, buttons, list box and timer

Video Tutorial –

 

Download Text File Background and Custom Font for the app here

 

Source Code

 

using System.Drawing.Text;

namespace Read_One_Line_Jokes_App_Windows_Form_MOO_ICT
{
    public partial class Form1 : Form
    {
        int lineNum = -1;
        PrivateFontCollection newFont = new PrivateFontCollection();
        bool playing = false;


        public Form1()
        {
            InitializeComponent();

            newFont.AddFontFile("Rainbow Paper.ttf");
            lblJoke.Font = new Font(newFont.Families[0],24, FontStyle.Bold);

        }

        private void TimerEvent(object sender, EventArgs e)
        {

            if (playing == true && lineNum < listBox1.Items.Count -1)
            {
                lineNum++;
                listBox1.SetSelected(lineNum, true); 
            }
            else
            {
                lineNum = -1;
                playing = false;
                timer.Stop();
                btnStart.Text = "Start";
            }




        }

        private void LoadFile(object sender, EventArgs e)
        {
            // open text file

            OpenFileDialog openFile = new OpenFileDialog();
            openFile.InitialDirectory = @"C:\txt";
            openFile.Title = "Browse Text Files Only";
            openFile.Filter = "Text Files Only (*.txt) | *.txt";
            openFile.DefaultExt = "txt";

            if (openFile.ShowDialog() == DialogResult.OK)
            {
                listBox1.Items.Clear();

                // load the lines to the list box
                var fileLocation = File.ReadAllLines(openFile.FileName);
                List lines = new List(fileLocation);

                for (int i = 0; i < lines.Count; i++) 
                { 
                   listBox1.Items.Add(lines[i]); 
                } 
             } 
          } 



          private void StartTimer(object sender, EventArgs e) { if (playing == false) { if (listBox1.Items.Count > 0)
                {
                    playing = true;
                    timer.Start();
                    btnStart.Text = "Stop";
                }
                else
                {
                    MessageBox.Show("Select a text file first! ", "MOO Says: ");
                }
            }
            else
            {
                timer.Stop();
                btnStart.Text = "Start";
                playing = false;
            }




        }

        private void ChangeJokeListBox(object sender, EventArgs e)
        {
            lblJoke.Text = listBox1.SelectedItem.ToString();
        }
    }
}






Comments are closed.