您的位置:首页 > 编程语言 > C#

简单文本编辑器

2013-09-26 21:37 375 查看


功能:
对编辑器中选中的文本进行粗体、下划线、斜体、居中、文字大小、链接特殊处理、加载文件、保存为文件等操作

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Simple_Text_Editor
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void buttonBold_Click(object sender, EventArgs e)
{
Font oldFont;
Font newFont;
oldFont = this.richTextBoxText.SelectionFont;
if (oldFont.Bold)
newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Bold);
else
newFont = new Font(oldFont, oldFont.Style | FontStyle.Bold);
this.richTextBoxText.SelectionFont = newFont;
this.richTextBoxText.Focus();
}

private void buttonUnderline_Click(object sender, EventArgs e)
{
Font oldFont;
Font newFont;
oldFont = this.richTextBoxText.SelectionFont;
if (oldFont.Underline)
newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Underline);
else
newFont = new Font(oldFont, oldFont.Style | FontStyle.Underline);
this.richTextBoxText.SelectionFont = newFont;
this.richTextBoxText.Focus();
}

private void buttonItalic_Click(object sender, EventArgs e)
{
Font oldFont;
Font newFont;
oldFont = this.richTextBoxText.SelectionFont;
if (oldFont.Italic)
newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Italic);
else
newFont = new Font(oldFont, oldFont.Style | FontStyle.Italic);
this.richTextBoxText.SelectionFont = newFont;
this.richTextBoxText.Focus();
}

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

private void textBoxSize_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.richTextBoxText.Focus();
}
}

private void textBoxSize_Validated(object sender, EventArgs e)
{
TextBox txt = (TextBox)sender;
ApplyTextSize(txt.Text);
this.richTextBoxText.Focus();
}

private void ApplyTextSize(string textSize)
{
float newSize = Convert.ToSingle(textSize);
FontFamily currentFontFamily;
Font newFont;
currentFontFamily = this.richTextBoxText.SelectionFont.FontFamily;
newFont = new Font(currentFontFamily,newSize);
this.richTextBoxText.SelectionFont = newFont;
}

private void richTextBoxText_LinkClicked(object sender, LinkClickedEventArgs e)
{
System.Diagnostics.Process.Start(e.LinkText);
}

private void buttonLoad_Click(object sender, EventArgs e)
{
try
{
richTextBoxText.LoadFile("Test.rtf");
}
catch (System.IO.FileNotFoundException)
{
MessageBox.Show("No file to load yet!");
}
}

private void buttonSave_Click(object sender, EventArgs e)
{
try
{
richTextBoxText.SaveFile("Text.rtf");
}
catch (System.Exception err)
{
MessageBox.Show(err.Message);
}
}
}
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C# 编辑器 窗口