Simple Text Editor in C#

Step 1: Firstly we're going to create a new Win Form Application project(I'll call it Cioban Mini Text Editor).



Step 2: To our form we will add three buttons(one for saving the file,one for reading the file,and the other for font),an openfile dialog,a savefile dialog,a font dialog, and rich textbox.Also,we'll ad a new namespace(System.IO).



Step 3: The design it's done.Let's start coding.

In my case:

button1 = open button;
button2 = save button;
button3 = the font button

This is the code

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.IO;  
  10. namespace Cioban_Mini_Text_Editor   
  11. {  
  12.     public partial class Form1: Form  
  13.     {  
  14.         public Form1()  
  15.         {  
  16.             InitializeComponent();  
  17.         }  
  18.         private void button1_Click(object sender, EventArgs e)   
  19.         {  
  20.             openFileDialog1.ShowDialog(); //Shows the dialog   
  21.             if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK && openFileDialog1.FileName.Contains(".txt")) //Checks if it's all ok and if the file name contains .txt  
  22.             {  
  23.                 string open = File.ReadAllText(openFileDialog1.FileName); //Reads the text from file  
  24.                 richTextBox1.Text = open; //Shows the reded text in the textbox  
  25.             }   
  26.             else //If something goes wrong...  
  27.             {  
  28.                 MessageBox.Show("The file you've chosen is not a text file");  
  29.             }  
  30.         }  
  31.   
  32.         private void button2_Click(object sender, EventArgs e)   
  33.         {  
  34.             saveFileDialog1.ShowDialog(); //Opens the Show File Dialog  
  35.             if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) //Check if it's all ok  
  36.             {  
  37.                 string name = saveFileDialog1.FileName + ".txt"//Just to make sure the extension is .txt  
  38.                 File.WriteAllText(name, richTextBox1.Text); //Writes the text to the file and saves it               
  39.             }  
  40.         }  
  41.         private void button3_Click(object sender, EventArgs e)  
  42.         {  
  43.             fontDialog1.ShowDialog(); //Shows the font dialog   
  44.             if (fontDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) {  
  45.                 richTextBox1.Font = fontDialog1.Font; //Sets the font to the one selected in the dialog  
  46.             }  
  47.         }  
  48.     }  
  49. }  
Enjoy

Thanks guys.
Next Recommended Reading Saving C# List Into Text File