[C#][SAMPLE][CODE][Control]RichTextBox以及相关常见属性

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

Form1.Designer.cs
  1 None.gif namespace  WA_RichTextBoxTest
  2 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
  3InBlock.gif    partial class Form1
  4ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
  5ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
  6InBlock.gif        /// 必需的设计器变量。
  7ExpandedSubBlockEnd.gif        /// </summary>

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

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

 20InBlock.gif            base.Dispose(disposing);
 21ExpandedSubBlockEnd.gif        }

 22InBlock.gif
 23ContractedSubBlock.gifExpandedSubBlockStart.gif        Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
 24InBlock.gif
 25ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 26InBlock.gif        /// 设计器支持所需的方法 - 不要
 27InBlock.gif        /// 使用代码编辑器修改此方法的内容。
 28ExpandedSubBlockEnd.gif        /// </summary>

 29InBlock.gif        private void InitializeComponent()
 30ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 31InBlock.gif            this.buttonBold = new System.Windows.Forms.Button();
 32InBlock.gif            this.buttonUnderline = new System.Windows.Forms.Button();
 33InBlock.gif            this.buttonItalic = new System.Windows.Forms.Button();
 34InBlock.gif            this.buttonCenter = new System.Windows.Forms.Button();
 35InBlock.gif            this.richTextBoxText = new System.Windows.Forms.RichTextBox();
 36InBlock.gif            this.textBoxSize = new System.Windows.Forms.TextBox();
 37InBlock.gif            this.labelSize = new System.Windows.Forms.Label();
 38InBlock.gif            this.buttonLoad = new System.Windows.Forms.Button();
 39InBlock.gif            this.buttonSave = new System.Windows.Forms.Button();
 40InBlock.gif            this.SuspendLayout();
 41InBlock.gif            // 
 42InBlock.gif            // buttonBold
 43InBlock.gif            // 
 44InBlock.gif            this.buttonBold.Anchor = System.Windows.Forms.AnchorStyles.Top;
 45InBlock.gif            this.buttonBold.Location = new System.Drawing.Point(1212);
 46InBlock.gif            this.buttonBold.Name = "buttonBold";
 47InBlock.gif            this.buttonBold.Size = new System.Drawing.Size(11623);
 48InBlock.gif            this.buttonBold.TabIndex = 0;
 49InBlock.gif            this.buttonBold.Text = "buttonBold";
 50InBlock.gif            this.buttonBold.UseVisualStyleBackColor = true;
 51InBlock.gif            this.buttonBold.Click += new System.EventHandler(this.buttonBold_Click);
 52InBlock.gif            // 
 53InBlock.gif            // buttonUnderline
 54InBlock.gif            // 
 55InBlock.gif            this.buttonUnderline.Anchor = System.Windows.Forms.AnchorStyles.Top;
 56InBlock.gif            this.buttonUnderline.Location = new System.Drawing.Point(17412);
 57InBlock.gif            this.buttonUnderline.Name = "buttonUnderline";
 58InBlock.gif            this.buttonUnderline.Size = new System.Drawing.Size(11623);
 59InBlock.gif            this.buttonUnderline.TabIndex = 1;
 60InBlock.gif            this.buttonUnderline.Text = "buttonUnderline";
 61InBlock.gif            this.buttonUnderline.UseVisualStyleBackColor = true;
 62InBlock.gif            this.buttonUnderline.Click += new System.EventHandler(this.buttonUnderline_Click);
 63InBlock.gif            // 
 64InBlock.gif            // buttonItalic
 65InBlock.gif            // 
 66InBlock.gif            this.buttonItalic.Anchor = System.Windows.Forms.AnchorStyles.Top;
 67InBlock.gif            this.buttonItalic.Location = new System.Drawing.Point(33612);
 68InBlock.gif            this.buttonItalic.Name = "buttonItalic";
 69InBlock.gif            this.buttonItalic.Size = new System.Drawing.Size(11623);
 70InBlock.gif            this.buttonItalic.TabIndex = 2;
 71InBlock.gif            this.buttonItalic.Text = "buttonItalic";
 72InBlock.gif            this.buttonItalic.UseVisualStyleBackColor = true;
 73InBlock.gif            this.buttonItalic.Click += new System.EventHandler(this.buttonItalic_Click);
 74InBlock.gif            // 
 75InBlock.gif            // buttonCenter
 76InBlock.gif            // 
 77InBlock.gif            this.buttonCenter.Anchor = System.Windows.Forms.AnchorStyles.Top;
 78InBlock.gif            this.buttonCenter.Location = new System.Drawing.Point(49812);
 79InBlock.gif            this.buttonCenter.Name = "buttonCenter";
 80InBlock.gif            this.buttonCenter.Size = new System.Drawing.Size(11623);
 81InBlock.gif            this.buttonCenter.TabIndex = 3;
 82InBlock.gif            this.buttonCenter.Text = "buttonCenter";
 83InBlock.gif            this.buttonCenter.UseVisualStyleBackColor = true;
 84InBlock.gif            this.buttonCenter.Click += new System.EventHandler(this.buttonCenter_Click);
 85InBlock.gif            // 
 86InBlock.gif            // richTextBoxText
 87InBlock.gif            // 
 88InBlock.gif            this.richTextBoxText.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
 89InBlock.gif                        | System.Windows.Forms.AnchorStyles.Left)
 90InBlock.gif                        | System.Windows.Forms.AnchorStyles.Right)));
 91InBlock.gif            this.richTextBoxText.Location = new System.Drawing.Point(1270);
 92InBlock.gif            this.richTextBoxText.Name = "richTextBoxText";
 93InBlock.gif            this.richTextBoxText.Size = new System.Drawing.Size(604185);
 94InBlock.gif            this.richTextBoxText.TabIndex = 4;
 95InBlock.gif            this.richTextBoxText.Text = "";
 96InBlock.gif            this.richTextBoxText.LinkClicked += new System.Windows.Forms.LinkClickedEventHandler(this.richTextBoxText_LinkClicked);
 97InBlock.gif            // 
 98InBlock.gif            // textBoxSize
 99InBlock.gif            // 
100InBlock.gif            this.textBoxSize.Anchor = System.Windows.Forms.AnchorStyles.Top;
101InBlock.gif            this.textBoxSize.Location = new System.Drawing.Point(28841);
102InBlock.gif            this.textBoxSize.Name = "textBoxSize";
103InBlock.gif            this.textBoxSize.Size = new System.Drawing.Size(14821);
104InBlock.gif            this.textBoxSize.TabIndex = 5;
105InBlock.gif            this.textBoxSize.Text = "10";
106InBlock.gif            this.textBoxSize.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
107InBlock.gif            this.textBoxSize.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBoxSize_KeyPress);
108InBlock.gif            this.textBoxSize.Validating += new System.ComponentModel.CancelEventHandler(this.textBoxSize_Validating);
109InBlock.gif            // 
110InBlock.gif            // labelSize
111InBlock.gif            // 
112InBlock.gif            this.labelSize.Anchor = System.Windows.Forms.AnchorStyles.Top;
113InBlock.gif            this.labelSize.AutoSize = true;
114InBlock.gif            this.labelSize.Location = new System.Drawing.Point(20344);
115InBlock.gif            this.labelSize.Name = "labelSize";
116InBlock.gif            this.labelSize.Size = new System.Drawing.Size(5912);
117InBlock.gif            this.labelSize.TabIndex = 6;
118InBlock.gif            this.labelSize.Text = "labelSize";
119InBlock.gif            // 
120InBlock.gif            // buttonLoad
121InBlock.gif            // 
122InBlock.gif            this.buttonLoad.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
123InBlock.gif            this.buttonLoad.Location = new System.Drawing.Point(174266);
124InBlock.gif            this.buttonLoad.Name = "buttonLoad";
125InBlock.gif            this.buttonLoad.Size = new System.Drawing.Size(11623);
126InBlock.gif            this.buttonLoad.TabIndex = 7;
127InBlock.gif            this.buttonLoad.Text = "buttonLoad";
128InBlock.gif            this.buttonLoad.UseVisualStyleBackColor = true;
129InBlock.gif            this.buttonLoad.Click += new System.EventHandler(this.buttonLoad_Click);
130InBlock.gif            // 
131InBlock.gif            // buttonSave
132InBlock.gif            // 
133InBlock.gif            this.buttonSave.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
134InBlock.gif            this.buttonSave.Location = new System.Drawing.Point(336266);
135InBlock.gif            this.buttonSave.Name = "buttonSave";
136InBlock.gif            this.buttonSave.Size = new System.Drawing.Size(11623);
137InBlock.gif            this.buttonSave.TabIndex = 8;
138InBlock.gif            this.buttonSave.Text = "buttonSave";
139InBlock.gif            this.buttonSave.UseVisualStyleBackColor = true;
140InBlock.gif            this.buttonSave.Click += new System.EventHandler(this.buttonSave_Click);
141InBlock.gif            // 
142InBlock.gif            // Form1
143InBlock.gif            // 
144InBlock.gif            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
145InBlock.gif            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
146InBlock.gif            this.ClientSize = new System.Drawing.Size(628295);
147InBlock.gif            this.Controls.Add(this.buttonSave);
148InBlock.gif            this.Controls.Add(this.buttonLoad);
149InBlock.gif            this.Controls.Add(this.labelSize);
150InBlock.gif            this.Controls.Add(this.textBoxSize);
151InBlock.gif            this.Controls.Add(this.richTextBoxText);
152InBlock.gif            this.Controls.Add(this.buttonCenter);
153InBlock.gif            this.Controls.Add(this.buttonItalic);
154InBlock.gif            this.Controls.Add(this.buttonUnderline);
155InBlock.gif            this.Controls.Add(this.buttonBold);
156InBlock.gif            this.MinimumSize = new System.Drawing.Size(636329);
157InBlock.gif            this.Name = "Form1";
158InBlock.gif            this.Text = "Form1";
159InBlock.gif            this.DragDrop += new System.Windows.Forms.DragEventHandler(this.Form1_DragDrop);
160InBlock.gif            this.DragEnter += new System.Windows.Forms.DragEventHandler(this.Form1_DragEnter);
161InBlock.gif            this.ResumeLayout(false);
162InBlock.gif            this.PerformLayout();
163InBlock.gif
164ExpandedSubBlockEnd.gif        }

165InBlock.gif
166ExpandedSubBlockEnd.gif        #endregion

167InBlock.gif
168InBlock.gif        private System.Windows.Forms.Button buttonBold;
169InBlock.gif        private System.Windows.Forms.Button buttonUnderline;
170InBlock.gif        private System.Windows.Forms.Button buttonItalic;
171InBlock.gif        private System.Windows.Forms.Button buttonCenter;
172InBlock.gif        private System.Windows.Forms.RichTextBox richTextBoxText;
173InBlock.gif        private System.Windows.Forms.TextBox textBoxSize;
174InBlock.gif        private System.Windows.Forms.Label labelSize;
175InBlock.gif        private System.Windows.Forms.Button buttonLoad;
176InBlock.gif        private System.Windows.Forms.Button buttonSave;
177ExpandedSubBlockEnd.gif    }

178ExpandedBlockEnd.gif}

179 None.gif
180 None.gif

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

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

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

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

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

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

100ExpandedSubBlockEnd.gif        }

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

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

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

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

136InBlock.gif            catch (System.IO.FileNotFoundException)
137ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
138InBlock.gif                MessageBox.Show("文件不存在!");
139ExpandedSubBlockEnd.gif            }

140ExpandedSubBlockEnd.gif        }

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

149InBlock.gif            catch (System.Exception err)
150ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
151InBlock.gif                MessageBox.Show(err.Message);
152ExpandedSubBlockEnd.gif            }

153ExpandedSubBlockEnd.gif        }

154InBlock.gif
155InBlock.gif        private void Form1_DragDrop(object sender, DragEventArgs e)
156ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
157InBlock.gif            buttonSave_Click(sender, e);
158ExpandedSubBlockEnd.gif        }

159InBlock.gif
160InBlock.gif        private void Form1_DragEnter(object sender, DragEventArgs e)
161ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
162InBlock.gif            this.Focus();
163ExpandedSubBlockEnd.gif        }

164ExpandedSubBlockEnd.gif    }

165ExpandedBlockEnd.gif}

[示例说明]:
1、开发语言:C#
2、开发环境:Visual Studio.Net 2005 Team suite
3、开发模板:C#.net项目->Windows应用程序

转载于:https://www.cnblogs.com/volnet/archive/2006/11/28/575599.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值