C# GUI Text Editor

Exercise: Write a GUI program to create a simple Text Editor. Your editor has the following basic functions:

-A user can choose a file  to open

-A user can save the text to the existing file.

-A user can save the text in a new file name.

-A user can play with styles of the text(font name, size, color, etc.)


Csharp exercises:create a simple text editor

Solution: To run the code solution for this exercise, you will need to create a project(Window Form Applications) in Visual Studio. The controls/components to be placed on your form are five Buttons, one RichTextBox, one OpenFileDialog, one FontDialog, one ColorDialog, and one SaveFileDialog.

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.IO;


namespace WindowsFormsApplication2


    public partial class Form1 : Form


        string fn = "" ;//file name variable


        public Form1()





        private void Form1_Load(object sender, EventArgs e)


            //set default font to the text

            richTextBox1.Font = new Font("Arial", 12, FontStyle.Regular); 



        private void button1_Click(object sender, EventArgs e)




               //Choose file to open

                openFileDialog1.Filter = "Text files(*.txt)|*.txt|All files(*.*)|*.*";

                openFileDialog1.Multiselect = false;



                fn = openFileDialog1.FileName;


                richTextBox1.Text = readText(fn);



            catch (FileNotFoundException ex) { }



        private string readText(string fn)


          //read file content and place it in the RichTextBox

            string content=null ;

            if (fn !=null )


                FileStream fs = new FileStream(fn, FileMode.Open, FileAccess.Read);

                StreamReader fr = new StreamReader(fs);


                content = fr.ReadToEnd();



            return content;



        private void button2_Click(object sender, EventArgs e)


          //open font dialog box


            richTextBox1.SelectionFont  = fontDialog1.Font;

            richTextBox1.SelectionColor = fontDialog1.Color;



        private void button3_Click(object sender, EventArgs e)


          //Open color dialog box


            richTextBox1.SelectionColor = colorDialog1.Color ;



        private void button4_Click(object sender, EventArgs e)


          //save to existing file


            richTextBox1.SaveFile(fn,RichTextBoxStreamType.PlainText );



        private void button5_Click(object sender, EventArgs e)


            saveFileDialog1.Filter = "Text files(*.txt)|*.txt|All files(*.*)|*.*";


            if (saveFileDialog1.FileName != "")

                richTextBox1.SaveFile(saveFileDialog1.FileName,RichTextBoxStreamType.PlainText );



        private void Form1_Resize(object sender, EventArgs e)


            //resize the controls when the form resizes

            richTextBox1.Width =this.Width-60 ;

            richTextBox1.Height = this.Height-100;






This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.