Page 1 of 1

RTF Document Editor

Posted: Wed Mar 02, 2011 5:15 am
by CodenStuff
Hello,

I was just going through my project folder and found this document editor I was working on some time ago. Its only basic but may help you if you want to make a text editor.

This is the same as my other tutorial in VB.Net and I have just converted it for any C# users who may want it.

Image

Features include:
Save & Load as *.rtf file (saves font/sizes/colour etc rather than just plain text)
Change text colour
Change text font/size
Change font style


Here is how to make this little application. First you will need to add the following controls to your form:

MenuStrip - add 3 fields into this control "Save"..."Load"...and "Exit"
14 Buttons
1 RichtextBox


Button 1 - 5 = Used to select font style
Buttons 6 - 14 = Used to select colours
Button 15 = Choose font

OK once you have done that and arranged the controls as you want them you need to stretch out the richtextbox so it fits most of the form, make it nice and big.

Then you can simply copy and paste this code over. Its mostly button code so its easy to follow, each one changes Font/Color/Style:
Code: Select all
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;

namespace DocPad
{
	public partial class Form1
	{

		internal Form1()
		{
			InitializeComponent();
		}
		private void Button11_Click(object sender, System.EventArgs e)
		{
			RichTextBox1.SelectionColor = Color.LightSlateGray;
		}

		private void Button1_Click(object sender, System.EventArgs e)
		{
			RichTextBox1.SelectionFont = new Font(this.RichTextBox1.SelectionFont, FontStyle.Bold);
		}

		private void Button2_Click(object sender, System.EventArgs e)
		{
			RichTextBox1.SelectionFont = new Font(this.RichTextBox1.SelectionFont, FontStyle.Regular);
		}

		private void Button3_Click(object sender, System.EventArgs e)
		{
			RichTextBox1.SelectionFont = new Font(this.RichTextBox1.SelectionFont, FontStyle.Italic);
		}

		private void Button4_Click(object sender, System.EventArgs e)
		{
			RichTextBox1.SelectionFont = new Font(this.RichTextBox1.SelectionFont, FontStyle.Strikeout);
		}

		private void Button5_Click(object sender, System.EventArgs e)
		{
			RichTextBox1.SelectionFont = new Font(this.RichTextBox1.SelectionFont, FontStyle.Underline);
		}

		private void Button6_Click(object sender, System.EventArgs e)
		{
			RichTextBox1.SelectionColor = Color.Red;
		}

		private void Button7_Click(object sender, System.EventArgs e)
		{
			RichTextBox1.SelectionColor = Color.Blue;
		}

		private void Button8_Click(object sender, System.EventArgs e)
		{
			RichTextBox1.SelectionColor = Color.Purple;
		}

		private void Button9_Click(object sender, System.EventArgs e)
		{
			RichTextBox1.SelectionColor = Color.Black;
		}

		private void Button10_Click(object sender, System.EventArgs e)
		{
			RichTextBox1.SelectionColor = Color.Pink;
		}

		private void Button12_Click(object sender, System.EventArgs e)
		{
			RichTextBox1.SelectionColor = Color.Yellow;
		}

		private void Button13_Click(object sender, System.EventArgs e)
		{
			RichTextBox1.SelectionColor = Color.Green;
		}

		private void Button14_Click(object sender, System.EventArgs e)
		{
			RichTextBox1.SelectionColor = Color.Orange;
		}
		private void SaveToolStripMenuItem_Click(object sender, System.EventArgs e)
		{
			SaveFileDialog savefiledialog1 = new SaveFileDialog();
			savefiledialog1.Title = "Save File";
			savefiledialog1.FileName = "*.rtf";
			savefiledialog1.Filter = "RTF |*.rtf";
			if (savefiledialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				RichTextBox1.SaveFile(savefiledialog1.FileName, RichTextBoxStreamType.RichText);
			}
		}

		private void LoadToolStripMenuItem_Click(object sender, System.EventArgs e)
		{
			OpenFileDialog openfiledialog1 = new OpenFileDialog();
			openfiledialog1.Title = "Save File";
			openfiledialog1.FileName = "*.rtf";
			openfiledialog1.Filter = "RTF |*.rtf";
			if (openfiledialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				RichTextBox1.LoadFile(openfiledialog1.FileName, RichTextBoxStreamType.RichText);
			}
		}

		private void ExitToolStripMenuItem_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void Button15_Click(object sender, System.EventArgs e)
		{
			FontDialog fontdialog1 = new FontDialog();
			if (fontdialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				RichTextBox1.SelectionFont = fontdialog1.Font;
			}
		}

        private void Form1_Load(object sender, EventArgs e)
        {

        }
	}

} //end of root namespace

If everything went ok you should now be able to run and use your basic text editor :D .

Source:
DocPadC.zip
Happy coding cooll;

Re: RTF Document Editor

Posted: Wed Mar 02, 2011 5:34 am
by zachman61
are you beginning to venture from VB?
nice and i guarantee this will help me learn it

Re: RTF Document Editor

Posted: Wed Mar 02, 2011 6:21 am
by Usman55
Hmmm... Wordpad in C#? I remember when you posted this. This was what made me to make my own editor. Do it in C++ lol.

Re: RTF Document Editor

Posted: Sat Mar 05, 2011 11:11 pm
by GoodGuy17
zachman61 wrote:
are you beginning to venture from VB?
nice and i guarantee this will help me learn it
I think he just used a converter, but he may very well have actually did the whole conversion process himself.

Re: RTF Document Editor

Posted: Sat Mar 05, 2011 11:14 pm
by Zulf
C# is easier than VB.NET.

Re: RTF Document Editor

Posted: Sun Mar 06, 2011 8:10 am
by Kieken72
Could u release it also in Vb.Net?

Re: RTF Document Editor

Posted: Sun Mar 06, 2011 8:19 am
by GoodGuy17
It is in VB, check the VB section for a topic titled as this is.