RTF Document Editor

All tutorials created in C# to be posted in here.
7 posts Page 1 of 1
Contributors
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4392
Joined: Tue Aug 04, 2009 1:47 am

RTF Document Editor
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;
You do not have the required permissions to view the files attached to this post.
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
User avatar
zachman61
VIP - Donator
VIP - Donator
Posts: 1892
Joined: Wed Dec 16, 2009 9:56 pm

Re: RTF Document Editor
zachman61
are you beginning to venture from VB?
nice and i guarantee this will help me learn it
Nailing my feet to the floor is easier than using my own muscles to balance, but you don't see me doing that :)
User avatar
Usman55
VIP - Site Partner
VIP - Site Partner
Posts: 2821
Joined: Thu Dec 24, 2009 7:52 pm

Re: RTF Document Editor
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.
Image
GoodGuy17
Coding God
Coding God
Posts: 1610
Joined: Mon Sep 07, 2009 12:25 am

Re: RTF Document Editor
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.
User avatar
Zulf
Serious Programmer
Serious Programmer
Posts: 441
Joined: Fri Jun 11, 2010 7:46 am

Re: RTF Document Editor
Zulf
C# is easier than VB.NET.
Image
User avatar
Kieken72
VIP - Donator
VIP - Donator
Posts: 231
Joined: Sun May 02, 2010 10:38 am

Re: RTF Document Editor
Kieken72
Could u release it also in Vb.Net?
GoodGuy17
Coding God
Coding God
Posts: 1610
Joined: Mon Sep 07, 2009 12:25 am

Re: RTF Document Editor
GoodGuy17
It is in VB, check the VB section for a topic titled as this is.
7 posts Page 1 of 1
Return to “C-Sharp Tutorials”