Wednesday, February 17, 2010

Building a simple text editor using WinForms

I’ve been playing around with WinForms recently, and decided to try and put together a simple text editor. It’s pretty straightforward to do so here’s how!

Setting Up

First of all, open up and create a new WinForms project in Visual Studio. When it’s all ready to go, you’ll see a designer view and some controls in the toolbox to the left. Drag over two Buttons, a TextField, a Label and a RichTextField. These can all be found in the “All Windows Forms” section of the Toolbox.

form design

It’s always a good idea to give each of the components a name so right-click and choose “Properties”. The Properties window should show on the right and you can change the name of the control at the top and the text that appears on the control at the bottom of this window. Now that the Form has been designed, it’s time to start programming.

Creating a new text file

If you right-click on the “Save to File” and click on “View Code”, you’ll be taken to the Form1.cs class that sits behind the form. The highlighted method will be a click event for that button. Before we put the code on the click event, add the following to the using statement which allows us to read and write files:

using System.IO;




In the click event for the Save to File button, paste the following code:



String input = richTextBox.Text;
String newName = newFileName.Text;

try
{
StreamWriter sw = new StreamWriter("Path to the file"+newName+".txt", true, Encoding.ASCII);
sw.WriteLine(input);
sw.Close();
}

catch (Exception ex)
{
Console.WriteLine("Exception: " + ex.Message);
}


This code begins by specifying string variables used to take input from the TextBox and RichTextBox. Then inside the try block, it opens the StreamWriter and writes the path to wherever you would like to save the file and encodes it. After the text from the RichTextBox is written to the file, the StreamWriter is closed.  Finally, a catch block is used to detect any errors while writing to the file.



If you Build and Run now, you’ll be able to type and save a text file! Next thing is to read information from a file we just created.





Reading from a text file



First thing we need to do here is to open up the click event for the “Read From File” button (again right-click and choose “View Code”). Copy the following code into the method:




String line;
try
{
StreamReader sr = new StreamReader("Path to the text filet");
line = sr.ReadLine();
while (line != null)
{
richTextBox.Text = line;
line = sr.ReadLine();
}
sr.Close();
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex.Message);
}



This code starts by creating a string variable used to hold whatever text is taken from the file. Next we create a try block (again so we make sure any errors are dealt with properly) where a StreamReader object is created. The while loop is fairly straightforward; it reads the text from the file and puts it into the RichTextBox before reading in the next line. The StreamReader is then closed and finally the catch block takes care of any errors we might have.



If you run the program now you should be able to create a new text file and read back from it. A good way to extend this project further is to have a drop down list containing all of the created text files so the user can open one from there.



One thing to note is when you’re specifying the path to your text file, you’ll have to use double backslashes (\\) which acts as a reserved character in C#.



As always you can leave a message if you have any problems or issues.

No comments:

Post a Comment