C# → Текстовый редактор

Пример реализации простого текстового редактора на C#.

notepad
Листинг:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.txtSize.KeyPress += new KeyPressEventHandler(txtSize_KeyPress);
            this.txtSize.Validating += new CancelEventHandler(txtSize_Validating);
            this.rtfText.LinkClicked += new LinkClickedEventHandler(rtfText_LinkClicked);
        }

        void rtfText_LinkClicked(object sender, LinkClickedEventArgs e)
        {
            // открыть браузер (по умолчанию) и перейти по ссылке
            System.Diagnostics.Process.Start(e.LinkText); 
        }

        private void txtSize_Validating(object sender, CancelEventArgs e)
        {
            TextBox txt = (TextBox)sender;

            ApplyTextSize(txt.Text);
            this.rtfText.Focus();
        }

        private void txtSize_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 13)
            {
                e.Handled = true;
            }
            else if (e.KeyChar == 13)
            {
                TextBox txt = (TextBox)sender;
                if (txt.Text.Length > 0)
                    ApplyTextSize(txt.Text);
                e.Handled = true;
                this.rtfText.Focus();
            }
        }

        private void ApplyTextSize(string text)
        {
            float newSize = Convert.ToSingle(text);
            FontFamily currentFontFamily;
            Font newFont;

            currentFontFamily = this.rtfText.SelectionFont.FontFamily;
            newFont = new Font(currentFontFamily, newSize);

            this.rtfText.SelectionFont = newFont;
        }

        private void btnBold_Click(object sender, EventArgs e)
        {
            Font oldFont, newFont;

            oldFont = this.rtfText.SelectionFont;
            if (oldFont.Bold == true)
                // если уже жирный то убираем выделение
                newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Bold);
            else newFont = new Font(oldFont, oldFont.Style | FontStyle.Bold); // выделяем жирным

            this.rtfText.SelectionFont = newFont;
            this.rtfText.Focus();
        }

        private void btnItalic_Click(object sender, EventArgs e)
        {
            Font oldFont, newFont;

            oldFont = this.rtfText.SelectionFont;
            if (oldFont.Italic == true)
                newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Italic);
            else newFont = new Font(oldFont, oldFont.Style | FontStyle.Italic); 

            this.rtfText.SelectionFont = newFont;
            this.rtfText.Focus();
        }

        private void btnUnderline_Click(object sender, EventArgs e)
        {
            Font oldFont, newFont;

            oldFont = this.rtfText.SelectionFont;
            if (oldFont.Underline == true)
                newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Underline);
            else newFont = new Font(oldFont, oldFont.Style | FontStyle.Underline); 

            this.rtfText.SelectionFont = newFont;
            this.rtfText.Focus();
        }

        private void btnCenter_Click(object sender, EventArgs e)
        {
            if (this.rtfText.SelectionAlignment == HorizontalAlignment.Center)
                this.rtfText.SelectionAlignment = HorizontalAlignment.Left;
            else this.rtfText.SelectionAlignment = HorizontalAlignment.Center;
            this.rtfText.Focus();
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            try
            {
                this.rtfText.LoadFile("C:\example.txt");
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                this.rtfText.SaveFile("C:\example.txt");
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }
    }
}