zoukankan      html  css  js  c++  java
  • [C#][SAMPLE][CODE][Control]RichTextBox以及相关常见属性

    [示例出处]:本示例来自《C#入门经典》第三版中文版,P355-P360
    [示例涉及]:
    1、RichTextBox以及相关常见属性
    [示例代码]:2文件(其余默认)

    Form1.Designer.cs
      1namespace WA_RichTextBoxTest
      2{
      3    partial class Form1
      4    {
      5        /// <summary>
      6        /// 必需的设计器变量。
      7        /// </summary>

      8        private System.ComponentModel.IContainer components = null;
      9
     10        /// <summary>
     11        /// 清理所有正在使用的资源。
     12        /// </summary>
     13        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>

     14        protected override void Dispose(bool disposing)
     15        {
     16            if (disposing && (components != null))
     17            {
     18                components.Dispose();
     19            }

     20            base.Dispose(disposing);
     21        }

     22
     23        Windows 窗体设计器生成的代码
    167
    168        private System.Windows.Forms.Button buttonBold;
    169        private System.Windows.Forms.Button buttonUnderline;
    170        private System.Windows.Forms.Button buttonItalic;
    171        private System.Windows.Forms.Button buttonCenter;
    172        private System.Windows.Forms.RichTextBox richTextBoxText;
    173        private System.Windows.Forms.TextBox textBoxSize;
    174        private System.Windows.Forms.Label labelSize;
    175        private System.Windows.Forms.Button buttonLoad;
    176        private System.Windows.Forms.Button buttonSave;
    177    }

    178}

    179
    180

    Form1.cs
      1using System;
      2using System.Collections.Generic;
      3using System.ComponentModel;
      4using System.Data;
      5using System.Drawing;
      6using System.Text;
      7using System.Windows.Forms;
      8
      9namespace WA_RichTextBoxTest
     10{
     11    public partial class Form1 : Form
     12    {
     13        public Form1()
     14        {
     15            InitializeComponent();
     16        }

     17
     18        private void buttonBold_Click(object sender, EventArgs e)
     19        {
     20            Font oldFont;
     21            Font newFont;
     22
     23            //获取选定字的字体
     24            oldFont = this.richTextBoxText.SelectionFont;
     25
     26            //如果字体是粗体,我们就将它的粗体格式移除
     27            if (oldFont.Bold)
     28                newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Bold);
     29            else
     30                newFont = new Font(oldFont, oldFont.Style | FontStyle.Bold);
     31
     32            //插入新字体并对RichTextBoxText设置输入焦点
     33            this.richTextBoxText.SelectionFont = newFont;
     34            this.richTextBoxText.Focus();
     35        }

     36
     37        private void buttonUnderline_Click(object sender, EventArgs e)
     38        {
     39            Font oldFont;
     40            Font newFont;
     41
     42            //获取选定字的字体
     43            oldFont = this.richTextBoxText.SelectionFont;
     44
     45            //如果有下划线,我们就将它的下划线格式移除
     46            if (oldFont.Underline)
     47                newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Underline);
     48            else
     49                newFont = new Font(oldFont, oldFont.Style | FontStyle.Underline);
     50
     51            //插入新字体并对RichTextBoxText设置输入焦点
     52            this.richTextBoxText.SelectionFont = newFont;
     53            this.richTextBoxText.Focus();
     54        }

     55
     56        private void buttonItalic_Click(object sender, EventArgs e)
     57        {
     58            Font oldFont;
     59            Font newFont;
     60
     61            //获取选定字的字体
     62            oldFont = this.richTextBoxText.SelectionFont;
     63
     64            //如果字体是斜体,我们就将它的斜体格式移除
     65            if (oldFont.Italic)
     66                newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Italic);
     67            else
     68                newFont = new Font(oldFont, oldFont.Style | FontStyle.Italic);
     69
     70            //插入新字体并对RichTextBoxText设置输入焦点
     71            this.richTextBoxText.SelectionFont = newFont;
     72            this.richTextBoxText.Focus();
     73        }

     74
     75        private void buttonCenter_Click(object sender, EventArgs e)
     76        {
     77            if (this.richTextBoxText.SelectionAlignment == HorizontalAlignment.Center)
     78                this.richTextBoxText.SelectionAlignment = HorizontalAlignment.Left;
     79            else
     80                this.richTextBoxText.SelectionAlignment = HorizontalAlignment.Center;
     81
     82            this.richTextBoxText.Focus();
     83        }

     84
     85        private void textBoxSize_KeyPress(object sender, KeyPressEventArgs e)
     86        {
     87            //移除所有的非数字,退格,回车字符
     88            if ((e.KeyChar < 48 || e.KeyChar > 57&& e.KeyChar != 8 && e.KeyChar != 13)
     89                e.Handled = true;
     90            else if (e.KeyChar == 13)
     91            
     92                //如果用户键入回车就设置字号
     93                TextBox txt = (TextBox)sender;
     94
     95                if (txt.Text.Length > 0)
     96                    ApplyTextSize(txt.Text);
     97                e.Handled = true;
     98                this.richTextBoxText.Focus();
     99            }

    100        }

    101
    102        private void textBoxSize_Validating(object sender, CancelEventArgs e)
    103        {
    104            TextBox txt = (TextBox)sender;
    105            ApplyTextSize(txt.Text);
    106            this.richTextBoxText.Focus();
    107        }

    108
    109        private void ApplyTextSize(string textSize)
    110        
    111            //因为我们将需要一个浮点型,所以我们转换text到float
    112            float newSize = Convert.ToSingle(textSize);
    113            FontFamily currentFontFamily;
    114            Font newFont;
    115
    116            //创建一个新的字体其他的family属性相同,除了新的尺寸
    117            currentFontFamily = this.richTextBoxText.SelectionFont.FontFamily;
    118            newFont = new Font(currentFontFamily, newSize);
    119
    120            //设置选择文字的字号
    121            this.richTextBoxText.SelectionFont = newFont;
    122        }

    123
    124        private void richTextBoxText_LinkClicked(object sender, LinkClickedEventArgs e)
    125        {
    126            System.Diagnostics.Process.Start(e.LinkText);
    127        }

    128
    129        private void buttonLoad_Click(object sender, EventArgs e)
    130        {
    131            //将文件载入到RichTextBox中
    132            try
    133            {
    134                richTextBoxText.LoadFile("Test.rtf");
    135            }

    136            catch (System.IO.FileNotFoundException)
    137            {
    138                MessageBox.Show("文件不存在!");
    139            }

    140        }

    141
    142        private void buttonSave_Click(object sender, EventArgs e)
    143        {
    144            //保存文件
    145            try
    146            {
    147                richTextBoxText.SaveFile("Test.rtf");
    148            }

    149            catch (System.Exception err)
    150            {
    151                MessageBox.Show(err.Message);
    152            }

    153        }

    154
    155        private void Form1_DragDrop(object sender, DragEventArgs e)
    156        {
    157            buttonSave_Click(sender, e);
    158        }

    159
    160        private void Form1_DragEnter(object sender, DragEventArgs e)
    161        {
    162            this.Focus();
    163        }

    164    }

    165}

    [示例说明]:
    1、开发语言:C#
    2、开发环境:Visual Studio.Net 2005 Team suite
    3、开发模板:C#.net项目->Windows应用程序
  • 相关阅读:
    108. Convert Sorted Array to Binary Search Tree
    107. Binary Tree Level Order Traversal II
    106. Construct Binary Tree from Inorder and Postorder Traversal
    105. Construct Binary Tree from Preorder and Inorder Traversal
    104. Maximum Depth of Binary Tree
    103. Binary Tree Zigzag Level Order Traversal
    102. Binary Tree Level Order Traversal
    系统和进程相关信息
    文件I/0缓冲
    系统编程概念(文件系统mount等函数的使用)
  • 原文地址:https://www.cnblogs.com/volnet/p/575599.html
Copyright © 2011-2022 走看看