RichTextBox控件

using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        //文件名(包含绝对路径)。
        private string fileName;
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 选中的文本如果是粗体,就去除粗体设置,否则就设置粗体。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnBold_Click(object sender, EventArgs e)
        {
            Font oldFont;
            Font newFont;
            oldFont = this.RichTextBox1.SelectionFont;
            if (oldFont.Bold)
                newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Bold);
            else
                newFont = new Font(oldFont, oldFont.Style | FontStyle.Bold);
            this.RichTextBox1.SelectionFont = newFont;
            this.RichTextBox1.Focus();
        }

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

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

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

        private void TbxSize_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)
                    ApplayTextSize(txt.Text);
                e.Handled = true;
                //在处理事件属性后面有一条函数是有必要的。
                this.RichTextBox1.Focus();
            }
        }
        /// <summary>
        /// 在进行完验证后引发该事件。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TbxSize_Validated(object sender, EventArgs e)
        {
            TextBox txt = (TextBox)sender;
            ApplayTextSize(txt.Text);
            this.RichTextBox1.Focus();
        }
        /// <summary>
        /// 应用该文本输入字体大小。
        /// </summary>
        /// <param name="textSize">文本大小</param>
        private void ApplayTextSize(string textSize)
        {
            float newSize = Convert.ToSingle(textSize);
            FontFamily currentFontFamily = this.RichTextBox1.SelectionFont.FontFamily;
            Font newFont = new Font(currentFontFamily, newSize);
            this.RichTextBox1.SelectionFont = newFont;
            this.RichTextBox1.Focus();
        }
        /// <summary>
        /// 单击文本中的超链接时发生。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RichTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start(e.LinkText);
        }

        private void BtnLoad_Click(object sender, EventArgs e)
        {
            try
            {
                using (OpenFileDialog fileDialog = new OpenFileDialog())
                {
                    fileDialog.Filter = "富文本格式文件(*.rtf)|*.rtf|所有文件|*.*";
                    if (fileDialog.ShowDialog() == DialogResult.OK)
                    {
                        fileName = fileDialog.FileName;
                        using (Stream s = fileDialog.OpenFile())
                        {
                            RichTextBox1.LoadFile(s, RichTextBoxStreamType.RichText);
                        }
                    }
                }
            }
            catch (FileLoadException ex)
            {
                MessageBox.Show("格式不正确:" + ex.Message);
            }
            catch (FileNotFoundException ex)
            {
                MessageBox.Show("未找到文件:" + ex.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void BtnSave_Click(object sender, EventArgs e)
        {
            try
            {
                using (SaveFileDialog fileDialog = new SaveFileDialog())
                {
                    fileDialog.AddExtension = true;
                    fileDialog.Filter = "富文本文件|*.RTF";
                    if (!string.IsNullOrEmpty(fileName))
                        RichTextBox1.SaveFile(fileName);
                    else
                    {
                        if (fileDialog.ShowDialog() == DialogResult.OK)
                            RichTextBox1.SaveFile(fileDialog.FileName);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void BtnClear_Click(object sender, EventArgs e)
        {
            Invoke(new Action(() => { RichTextBox1.Clear(); }));
        }
    }
}

 

namespace WindowsFormsApp2
{
    partial class Form1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要修改
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.BtnBold = new System.Windows.Forms.Button();
            this.BtnUnderline = new System.Windows.Forms.Button();
            this.BtnItalic = new System.Windows.Forms.Button();
            this.BtnCenter = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.TbxSize = new System.Windows.Forms.TextBox();
            this.RichTextBox1 = new System.Windows.Forms.RichTextBox();
            this.BtnLoad = new System.Windows.Forms.Button();
            this.BtnSave = new System.Windows.Forms.Button();
            this.BtnClear = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // BtnBold
            // 
            this.BtnBold.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.BtnBold.Location = new System.Drawing.Point(62, 26);
            this.BtnBold.Name = "BtnBold";
            this.BtnBold.Size = new System.Drawing.Size(127, 27);
            this.BtnBold.TabIndex = 0;
            this.BtnBold.Text = "Bold";
            this.BtnBold.UseVisualStyleBackColor = true;
            this.BtnBold.Click += new System.EventHandler(this.BtnBold_Click);
            // 
            // BtnUnderline
            // 
            this.BtnUnderline.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.BtnUnderline.Location = new System.Drawing.Point(211, 26);
            this.BtnUnderline.Name = "BtnUnderline";
            this.BtnUnderline.Size = new System.Drawing.Size(127, 27);
            this.BtnUnderline.TabIndex = 0;
            this.BtnUnderline.Text = "Underline";
            this.BtnUnderline.UseVisualStyleBackColor = true;
            this.BtnUnderline.Click += new System.EventHandler(this.BtnUnderline_Click);
            // 
            // BtnItalic
            // 
            this.BtnItalic.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.BtnItalic.Location = new System.Drawing.Point(360, 26);
            this.BtnItalic.Name = "BtnItalic";
            this.BtnItalic.Size = new System.Drawing.Size(127, 27);
            this.BtnItalic.TabIndex = 0;
            this.BtnItalic.Text = "Italic";
            this.BtnItalic.UseVisualStyleBackColor = true;
            this.BtnItalic.Click += new System.EventHandler(this.BtnItalic_Click);
            // 
            // BtnCenter
            // 
            this.BtnCenter.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.BtnCenter.Location = new System.Drawing.Point(509, 26);
            this.BtnCenter.Name = "BtnCenter";
            this.BtnCenter.Size = new System.Drawing.Size(127, 27);
            this.BtnCenter.TabIndex = 0;
            this.BtnCenter.Text = "Center";
            this.BtnCenter.UseVisualStyleBackColor = true;
            this.BtnCenter.Click += new System.EventHandler(this.BtnCenter_Click);
            // 
            // label1
            // 
            this.label1.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(185, 76);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(87, 15);
            this.label1.TabIndex = 1;
            this.label1.Text = "LabelSize:";
            // 
            // TbxSize
            // 
            this.TbxSize.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.TbxSize.Location = new System.Drawing.Point(272, 73);
            this.TbxSize.Name = "TbxSize";
            this.TbxSize.Size = new System.Drawing.Size(207, 25);
            this.TbxSize.TabIndex = 2;
            this.TbxSize.Text = "10";
            this.TbxSize.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.TbxSize_KeyPress);
            this.TbxSize.Validated += new System.EventHandler(this.TbxSize_Validated);
            // 
            // RichTextBox1
            // 
            this.RichTextBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.RichTextBox1.Location = new System.Drawing.Point(36, 116);
            this.RichTextBox1.Name = "RichTextBox1";
            this.RichTextBox1.Size = new System.Drawing.Size(647, 174);
            this.RichTextBox1.TabIndex = 3;
            this.RichTextBox1.Text = "";
            this.RichTextBox1.LinkClicked += new System.Windows.Forms.LinkClickedEventHandler(this.RichTextBox1_LinkClicked);
            // 
            // BtnLoad
            // 
            this.BtnLoad.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
            this.BtnLoad.Location = new System.Drawing.Point(283, 326);
            this.BtnLoad.Name = "BtnLoad";
            this.BtnLoad.Size = new System.Drawing.Size(127, 27);
            this.BtnLoad.TabIndex = 0;
            this.BtnLoad.Text = "Load";
            this.BtnLoad.UseVisualStyleBackColor = true;
            this.BtnLoad.Click += new System.EventHandler(this.BtnLoad_Click);
            // 
            // BtnSave
            // 
            this.BtnSave.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
            this.BtnSave.Location = new System.Drawing.Point(457, 326);
            this.BtnSave.Name = "BtnSave";
            this.BtnSave.Size = new System.Drawing.Size(127, 27);
            this.BtnSave.TabIndex = 0;
            this.BtnSave.Text = "Save";
            this.BtnSave.UseVisualStyleBackColor = true;
            this.BtnSave.Click += new System.EventHandler(this.BtnSave_Click);
            // 
            // BtnClear
            // 
            this.BtnClear.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
            this.BtnClear.Location = new System.Drawing.Point(117, 326);
            this.BtnClear.Name = "BtnClear";
            this.BtnClear.Size = new System.Drawing.Size(127, 27);
            this.BtnClear.TabIndex = 0;
            this.BtnClear.Text = "Clear Text";
            this.BtnClear.UseVisualStyleBackColor = true;
            this.BtnClear.Click += new System.EventHandler(this.BtnClear_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(716, 409);
            this.Controls.Add(this.RichTextBox1);
            this.Controls.Add(this.TbxSize);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.BtnCenter);
            this.Controls.Add(this.BtnItalic);
            this.Controls.Add(this.BtnUnderline);
            this.Controls.Add(this.BtnClear);
            this.Controls.Add(this.BtnSave);
            this.Controls.Add(this.BtnLoad);
            this.Controls.Add(this.BtnBold);
            this.MinimumSize = new System.Drawing.Size(734, 456);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button BtnBold;
        private System.Windows.Forms.Button BtnUnderline;
        private System.Windows.Forms.Button BtnItalic;
        private System.Windows.Forms.Button BtnCenter;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox TbxSize;
        private System.Windows.Forms.RichTextBox RichTextBox1;
        private System.Windows.Forms.Button BtnLoad;
        private System.Windows.Forms.Button BtnSave;
        private System.Windows.Forms.Button BtnClear;
    }
}

发布了18 篇原创文章 · 获赞 4 · 访问量 1839

猜你喜欢

转载自blog.csdn.net/tiankongzhicheng441x/article/details/104631293
今日推荐