简单文本编辑器
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#调用COM组件
- 一步一步跟我学易语言之第二个易程序菜单设计
- 过滤淘宝网弹出窗口
- c#中虚函数的相关使用方法
- 三个不同文件停顿二秒弹出不同窗口的批处理
- 浅析C# web访问mysql数据库-整理归纳总结
- C#实现的阴历阳历互相转化类实例
- C# 的关键字详细介绍
- c# 获得局域网主机列表实例
- 关于C# Math 处理奇进偶不进的实现代码
- c# 随机函数的使用详解
- c#之圆形无标题栏椭圆窗体的实现详解
- C#页面之间跳转功能的小结
- 浅析C# 基础语法的使用
- C#中将字符串转换为整型的三种解决方法总结
- 深入c# GDI+简单绘图的具体操作步骤(一)
- C#中英文混合字符串截取函数
- C# 面向对象的基本原则
- 使用C#开源文件实时监控工具Tail&TailUI介绍
- C# 正则判断一个数字的格式是否有逗号的代码