菜单事件的响应(RichTextBox字体加粗、斜体、下划线)

范例说明

本范例是在前一例的基础上创建各个菜单项的响应事件,使用户单击菜单项时能执行相应的操作,程序运行效果如图:

image

image

image

关键步骤

    1. 创建一个新的Windows应用程序.
    2. 把MenuStript和RichTextBox控件拖放到设计界面上,并设置相应菜单项和Name属性.
    3. 把RichTextBox的Dock属性修改为”Fill”.
    4. 双击各个菜单项,会出现代码界面,添加相应的功能代码.
using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.IO;



namespace Demo1_3

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }



        /// <summary>

        /// 打开文件菜单事件

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void tsmiOpenFile_Click(object sender, EventArgs e)

        {

            try

            {

                OpenFileDialog ofdOpenFile = new OpenFileDialog();

                //对话框标题

                ofdOpenFile.Title = "打开文件";

                //设置打开文件类型

                ofdOpenFile.Filter = "TextDocument(*.rtf)|*.rtf";

                if (ofdOpenFile.ShowDialog() == DialogResult.OK)

                {

                    //文本框显示打开文件的内容

                    this.rtbShow.LoadFile(ofdOpenFile.FileName, RichTextBoxStreamType.PlainText);

                    this.Text = ofdOpenFile.FileName;

                }

            }

            catch (IOException exp)

            {

                MessageBox.Show(exp.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }

        }



        /// <summary>

        /// 保存文件菜单事件

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void tsmiSaveFile_Click(object sender, EventArgs e)

        {

            try

            {

                SaveFileDialog sfdSaveFile = new SaveFileDialog();

                sfdSaveFile.Title = "保存文件";

                sfdSaveFile.Filter = "TextDocument(*.rtf)|*.rtf";

                if (sfdSaveFile.ShowDialog() == DialogResult.OK)

                {

                    this.rtbShow.SaveFile(sfdSaveFile.FileName, RichTextBoxStreamType.PlainText);

                }

            }

            catch (IOException exp)

            {

                MessageBox.Show(exp.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }

        }



        /// <summary>

        /// 文字加粗菜单事件

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void tsmiBold_Click(object sender, EventArgs e)

        {

            //获取当前选中文字的格式(字体、字号、字形等)

            Font oldFont = this.rtbShow.SelectionFont;

            Font newFont;

            //判断当前选中文字是否已加粗

            if (oldFont.Bold)

            {

                //如果选中文字已加粗则取消加粗

                newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Bold);

            }

            else

            {

                //如果选中文字未加粗则直接加粗

                newFont = new Font(oldFont, oldFont.Style | FontStyle.Bold);

            }

            this.rtbShow.SelectionFont = newFont;     //设置选中文字格式为新的格式

        }



        /// <summary>

        /// 文字斜体菜单事件

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void tsmiItalic_Click(object sender, EventArgs e)

        {

            Font oldFont = this.rtbShow.SelectionFont;

            Font newFont;

            if (oldFont.Italic)

            {

                newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Italic);

            }

            else

            {

                newFont = new Font(oldFont, oldFont.Style | FontStyle.Italic);

            }

            this.rtbShow.SelectionFont = newFont;

        }



        /// <summary>

        /// 文字下划线菜单事件

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void tsmiUnderline_Click(object sender, EventArgs e)

        {

            Font oldFont = this.rtbShow.SelectionFont;

            Font newFont;

            if (oldFont.Underline)

            {

                newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Underline);

            }

            else

            {

                newFont = new Font(oldFont, oldFont.Style | FontStyle.Underline);

            }

            this.rtbShow.SelectionFont = newFont;

        }

    }

}

你可能感兴趣的:(text)