RichTextBox控件

2024-04-03 22:38
文章标签 控件 richtextbox

本文主要是介绍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);elsenewFont = 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);elsenewFont = 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);elsenewFont = 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;elsethis.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();}#endregionprivate 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;}
}

这篇关于RichTextBox控件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/874168

相关文章

lvgl8.3.6 控件垂直布局 label控件在image控件的下方显示

在使用 LVGL 8.3.6 创建一个垂直布局,其中 label 控件位于 image 控件下方,你可以使用 lv_obj_set_flex_flow 来设置布局为垂直,并确保 label 控件在 image 控件后添加。这里是如何步骤性地实现它的一个基本示例: 创建父容器:首先创建一个容器对象,该对象将作为布局的基础。设置容器为垂直布局:使用 lv_obj_set_flex_flow 设置容器

小程序button控件上下边框的显示和隐藏

问题 想使用button自带的loading图标功能,但又不需要button显示边框线 button控件有一条淡灰色的边框,在控件上了样式 border:none; 无法让button边框隐藏 代码如下: <button class="btn">.btn{border:none; /*一般使用这个就是可以去掉边框了*/} 解决方案 发现button控件有一个伪元素(::after

MFC中Spin Control控件使用,同时数据在Edit Control中显示

实现mfc spin control 上下滚动,只需捕捉spin control 的 UDN_DELTAPOD 消息,如下:  OnDeltaposSpin1(NMHDR *pNMHDR, LRESULT *pResult) {  LPNMUPDOWN pNMUpDown = reinterpret_cast(pNMHDR);  // TODO: 在此添加控件通知处理程序代码    if

MFC 控件重绘(2) NM_CUSTOMDRAW, WM_DRAWITEM, 虚函数DrawItem

控件重绘有三种方法: 1 设定界面属性 2 利用Windows的消息机制,通过Windows消息映射(Message Mapping)和反映射(Message Reflecting),在合适的时机修改控件的状态和行为。此方式涉及NM_CUSTOMDRAW和WM_DRAWITEM 3 利用虚函数机制,重载虚函数。即DrawItem虚函数。 对于NM_CUSTOMDRAW,某些支持此消息的控件

C# 通过拖控件移动窗体

目录 引言一、通过控件事件移动窗体1、创建窗体界面2、添加控件事件3、添加代码 二、通过windowsAPI移动窗体1、 构建窗体和添加事件2、代码展示 引言 在C#Form窗体设计中,如果我们不需要使用默认边框设计自己个性化的窗体(FromBorderStyle=none时),这时候你会发现拖动窗体的功能就没有了,这里需要自己构建方法让用户可以拖动整个窗体,这里我们使用前辈的

Qt-常用控件(3)-多元素控件、容器类控件和布局管理器

1. 多元素控件 Qt 中提供的多元素控件有: QListWidgetQListViewQTableWidgetQTableViewQTreeWidgetQTreeView xxWidget 和 xxView 之间的区别,以 QTableWidget 和 QTableView 为例. QTableView 是基于 MVC 设计的控件.QTableView 自身不持有数据,使用 QTab

ASP.NET手动触发页面验证控件事件

开发环境:.NET Framework 3.5.1 sp1 参考文章: http://www.codeproject.com/KB/aspnet/JavascriptValidation.aspx http://msdn.microsoft.com/zh-cn/library/aa479045.aspx http://www.cnblogs.com/minsentinel/archive/

兔子--计算listview的高度,解决listview与scrollview控件冲突

/** * 计算ListView的高度 * * @param listView */ public void setListViewHeightBasedOnChildren(ListView listView) { // 获取ListView对应的Adapter OrderGoodsAdapter listAdapter = (OrderGoodsAdapter) listView.getAda

QT:QWidget 控件属性的介绍

控件属性介绍 🌴enabled 状态属性🌴geometry 几何属性示例一:改变控件尺寸示例二:更变控件位置window frame 的影响 🌴windowTitle 窗口标题🌴windowIcon 窗口图标🌴 qrc机制🌴windowOpacity 窗口透明度🌴cursor 修改鼠标显示样式示例一:通过编辑区直接修鼠标显示样式示例二:通过代码的方式设置鼠标在控件的显示效果示例

Android 自定义View控件,实现跟随手指触摸移动的小球

Android UI组件是通过继承View类,然后绘制内容,比如ImageView,TextView等组件都是继承View类。 当Android系统提供的组件功能不能满足需求时,可以通过继承View类重写一个或多个方法,派生自定义的组件,View类常用重写方法: 1.构造器:View子类最基本的重写方法 protected voidonDraw(Canvas canvas) public