richtextboxc#中怎样将richtextbox转换为流?

richtextbox  时间:2021-06-22  阅读:()

RichTextBox控件用法

Windows 窗体 RichTextBox 控件用于显示、输入和操作带有格式的文本。

  RichTextBox 控件除了执行 TextBox 控件的所有功能之外,它还可以显示字体、颜色和链接,从文件加载文本和嵌入的图像,撤消和重复编辑操作以及查找指定的字符。

  与字处理应用程序(如 Microsoft Word)类似,RichTextBox 通常用于提供文本操作和显示功能。

与 TextBox 控件一样,RichTextBox 控件也可以显示滚动条;但与 TextBox 控件不同的是,默认情况下,该控件将同时显示水平滚动条和垂直滚动条,并具有更多的滚动条设置。

     使用示例如下:   创建一个 RichTextBox 控件,该控件将 RTF 文件加载到控件中并搜索单词“Text”的第一个实例。

然后代码更改选定文本的字体样式、字体大小和字体颜色并将更改保存到原始文件。

在代码示例的最后,将该控件添加到其 Form 中。

  本示例要求1:将代码示例中创建的方法添加到 Form 类中并从窗体的构造函数调用此方法。

  本示例要求2:在 C 驱动器的根目录中创建一个包含单词“Text”的 RTF 文件。

  Demo   public void CreateMyRichTextBox()   {   RichTextBox richTextBox1 = new RichTextBox();   richTextBox1.Dock = DockStyle.Fill;   richTextBox1.LoadFile("C:\MyDocument.rtf");   richTextBox1.Find("Text", RichTextBoxFinds.MatchCase);   richTextBox1.SelectionFont = new Font("Verdana", 12, FontStyle.Bold);   richTextBox1.SelectionColor = Color.Red;   richTextBox1.SaveFile("C:\MyDocument.rtf", RichTextBoxStreamType.RichText);   this.Controls.Add(richTextBox1);   }   Demo   using System;   using System.Collections.Generic;   using System.ComponentModel;   using System.Data;   using System.Drawing;   using System.Text;   using System.Windows.Forms;   namespace Win_Test   {   public partial class RichTextBox_Test : Form   {   public RichTextBox_Test()   {   InitializeComponent();   }   Font oldFont;   Font newFont;   //richTextBox1 所选文字加粗   private void button1_Click(object sender, EventArgs e)   {   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();   }   //richTextBox1 所选文字加下划线   private void button2_Click(object sender, EventArgs e)   {   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();   }   //richTextBox1 所选文字为斜体   private void button3_Click(object sender, EventArgs e)   {   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();   }   //richTextBox1 所选文字居中   private void button4_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 textBox1_KeyPress(object sender, KeyPressEventArgs e)   {   //remove all characters that are not numbers,backspace and enter   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)   ApplyTextSize(txt.Text);   e.Handled = true;   this.richTextBox1.Focus();   }   }   //根据textBox1的值设置richTextBox1的字体   private void ApplyTextSize(string textSize)   {   float newSize = Convert.ToSingle(textSize);   FontFamily currentFontFamily;   Font newFont;   currentFontFamily = this.richTextBox1.SelectionFont.FontFamily;   newFont = new Font(currentFontFamily, newSize);   this.richTextBox1.SelectionFont = newFont;   }   //在textBox1控件验证时触发,设置richTextBox1的字体   private void textBox1_Validating(object sender, CancelEventArgs e)   {   TextBox txt = (TextBox)sender;   ApplyTextSize(txt.Text);   this.richTextBox1.Focus();   }   //让浏览器打开超链接地址   private void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)   {   System.Diagnostics.Process.Start(e.LinkText);   }   //richTextBox1 加载 Test.rtf 文件   private void button5_Click(object sender, EventArgs e)   {   try   {   richTextBox1.LoadFile("Test.rtf");   }   catch (System.IO.FileNotFoundException)   {   MessageBox.Show("No file to be load yet");   }   }   //richTextBox1 保存到 Test.rtf 文件   private void button6_Click(object sender, EventArgs e)   {   try   {   richTextBox1.SaveFile("Test.rtf");   }   catch (System.Exception err)   {   MessageBox.Show(err.Message);   }   }   }   }

RichTextBox与TextBox的区别是什么?

RichTextBox 控件不仅允许输入和编辑文本,同时还提供了标准 TextBox 控件未具有的、更高级的指定格式的许多功能。

  RichTextBox 提供了一些属性,对于本控件文本的任何部分,用这些属性都可以指定格式。

为了改变文本的格式,首先要选定它。

只有选定的文本才能赋予字符和段落格式。

使用这些属性,可把文本改为粗体或斜体,或改变其颜色,以及创建上标和下标。

通过设置左右缩进和悬挂式缩进,可调整段落的格式。

  RichTextBox 控件能以 rtf 格式和普通 ASCII 文本格式这两种形式打开和保存文件。

可以使用控件的方法(LoadFile 和 SaveFile)直接读写文件,或使用与 Visual Basic 文件输入/输出语句联结的、诸如 SelRTF 和 TextRTF 之类的控件属性打开和保存文件。

  发行注意:为了能在应用程序中使用 RichTextBox 控件,必须把Richtx32.ocx 文件添加到工程中。

因此,在应用程序发行时,Richtx32.ocx 文件就应安装在 Microsoft Windows 的 SYSTEM 目录内。

vb6.0 RichTextBox 的用法

RichTextBox的功能与用法大体上与TextBox(文本框)相似,但其功能更加强大,缺点是占用内存空间更多。

其最突出的优点是对字体的改变更灵活,多出了一些属性和方法。

这里简单说几个:改变字体:控件名.Font.Name = .... '比如:"宋体" (双引号必须有)改变字体大小:控件名.Font.Size = '这里填数字(单位:磅)改变字体颜色:控件名.SelColor = .... '例如绿色:RGB(0, 255 ,0) 另外一些功能楼上说的很详细,这里就不在解释说明了。

RichTextBox控件的事件

SelChange事件,DragDrop事件,DragOver事件,GotFocus事件,KeyDown,KeyUp事件,KeyPress事件,LostFocus事件,MouseDown, MouseUp事件,MouseMove事件,Validate事件,OLECompleteDrag事件(ActiveX控件),OLEDragDrop事件(ActiveX控件),OLEDragOver事件(ActiveX控件),OLEGiveFeedback事件(ActiveX控件),OLESetData事件(ActiveX控件),OLEStartDrag事件(ActiveX控件),Change事件(ActiveX控件),Click事件(ActiveX控件),DblClick事件(ActiveX控件),KeyDown,KeyUp事件(ActiveX控件),KeyPress事件(ActiveX控件)。

~~~~~~~~~ RichTextBox 事件 更新:2010 年 6 月(修正) RichTextBox类型公开以下成员。

事件 名称 说明 ContextMenuClosing 在控件上的任何上下文菜单关闭之前发生。

(继承自FrameworkElement。

) ContextMenuOpening 在控件上的任何上下文菜单打开时发生。

(继承自FrameworkElement。

) DataContextChanged 仅当此控件的数据上下文更改时才发生。

(继承自FrameworkElement。

) DragEnter 在输入系统报告将此控件作为拖动目标的基础拖动事件时发生。

(继承自UIElement。

) DragLeave 在输入系统报告将此控件作为拖动来源的基础拖动事件时发生。

(继承自UIElement。

) DragOver 在输入系统报告将此控件作为潜在放置目标的基础拖动事件时发生。

(继承自UIElement。

) Drop 在输入系统报告将此控件作为放置目标的基础放置事件时发生。

(继承自UIElement。

) FocusableChanged 当Focusable属性的值更改时发生。

(继承自UIElement。

) GiveFeedback 在输入系统报告涉及此控件的基础拖放事件时发生。

(继承自UIElement。

) GotFocus 在此控件获得逻辑焦点时发生。

(继承自UIElement。

) GotKeyboardFocus 在键盘焦点位于此控件上时发生。

(继承自UIElement。

) GotMouseCapture 在此控件捕获鼠标时发生。

(继承自UIElement。

) GotStylusCapture 在此控件捕获手写笔时发生。

(继承自UIElement。

) Initialized 初始化此FrameworkElement时发生。

此事件与IsInitialized属性的值从 false(或未定义)更改为 true 的情况相符。

(继承自FrameworkElement。

) IsEnabledChanged 在此控件的IsEnabled属性值更改时发生。

(继承自UIElement。

) IsHitTestVisibleChanged 在此控件的IsHitTestVisible依赖项属性值更改时发生。

(继承自UIElement。

) IsKeyboardFocusedChanged 在此控件的IsKeyboardFocused属性值更改时发生。

(继承自UIElement。

) IsKeyboardFocusWithinChanged 在此控件的IsKeyboardFocusWithinChanged属性值更改时发生。

(继承自UIElement。

) IsMouseCapturedChanged 在此控件的IsMouseCaptured属性值更改时发生。

(继承自UIElement。

) IsMouseCaptureWithinChanged 在此控件的IsMouseCaptureWithinProperty值更改时发生。

(继承自UIElement。

) IsMouseDirectlyOverChanged 在此控件的IsMouseDirectlyOver属性值更改时发生。

(继承自UIElement。

) IsStylusCapturedChanged 在此控件的IsStylusCaptured属性值更改时发生。

(继承自UIElement。

) IsStylusCaptureWithinChanged 在此控件的IsStylusCaptureWithin属性值更改时发生。

(继承自UIElement。

) IsStylusDirectlyOverChanged 在此控件的IsStylusDirectlyOver属性值更改时发生。

(继承自UIElement。

) IsVisibleChanged 在此控件的IsVisible属性值更改时发生。

(继承自UIElement。

) KeyDown 在焦点位于此控件上并且用户按下键时发生。

(继承自UIElement。

) KeyUp 在焦点位于此控件上并且用户释放键时发生。

(继承自UIElement。

) LayoutUpdated 在与当前Dispatcher关联的各种可视控件的布局更改时发生。

(继承自UIElement。

) Loaded 在控件已布局、已呈现且可用于交互时发生。

(继承自FrameworkElement。

) LostFocus 在此控件失去逻辑焦点时发生。

(继承自UIElement。

) LostKeyboardFocus 在键盘焦点不再位于此控件上时发生。

(继承自UIElement。

) LostMouseCapture 在此控件失去鼠标捕获时发生。

(继承自UIElement。

) LostStylusCapture 在此控件失去手写笔捕获时发生。

(继承自UIElement。

) MouseDoubleClick 当单击鼠标按钮两次或更多次时发生。

(继承自Control。

) MouseDown 在指针悬停于此控件上并且用户按下任意鼠标按钮时发生。

(继承自UIElement。

) MouseEnter 在鼠标指针进入此控件的边界时发生。

(继承自UIElement。

) MouseLeave 在鼠标指针离开此控件的边界时发生。

(继承自UIElement。

) MouseLeftButtonDown 在鼠标指针悬停于此控件上并且用户按下鼠标左键时发生。

(继承自UIElement。

) MouseLeftButtonUp 在鼠标指针悬停于此控件上并且用户释放鼠标左键时发生。

(继承自UIElement。

) MouseMove 在鼠标指针悬停于此控件上并且用户移动该鼠标指针时发生。

(继承自UIElement。

) MouseRightButtonDown 在鼠标指针悬停于此控件上并且用户按下鼠标右键时发生。

(继承自UIElement。

) MouseRightButtonUp 在鼠标指针悬停于此控件上并且用户释放鼠标右键时发生。

(继承自UIElement。

) MouseUp 在用户在此控件上释放任意鼠标按钮时发生。

(继承自UIElement。

) MouseWheel 在鼠标指针悬停于此控件上并且用户滚动鼠标滚轮时发生。

(继承自UIElement。

) PreviewDragEnter 在输入系统报告将此控件作为拖动目标的基础拖动事件时发生。

(继承自UIElement。

) PreviewDragLeave 在输入系统报告将此控件作为拖动来源的基础拖动事件时发生。

(继承自UIElement。

) PreviewDragOver 在输入系统报告将此控件作为潜在放置目标的基础拖动事件时发生。

(继承自UIElement。

) PreviewDrop 在输入系统报告将此控件作为放置目标的基础放置事件时发生。

(继承自UIElement。

) PreviewGiveFeedback 在开始拖放操作时发生。

(继承自UIElement。

) PreviewGotKeyboardFocus 在键盘焦点位于此控件上时发生。

(继承自UIElement。

) PreviewKeyDown 在焦点位于此控件上并且用户按下键时发生。

(继承自UIElement。

) PreviewKeyUp 在焦点位于此控件上并且用户释放键时发生。

(继承自UIElement。

) PreviewLostKeyboardFocus 在键盘焦点不再位于此控件上时发生。

(继承自UIElement。

) PreviewMouseDoubleClick 当用户单击鼠标按钮两次或更多次时发生。

(继承自Control。

) PreviewMouseDown 在指针悬停于此控件上并且用户按下任意鼠标按钮时发生。

(继承自UIElement。

) PreviewMouseLeftButtonDown 在鼠标指针悬停于此控件上并且用户按下鼠标左键时发生。

(继承自UIElement。

) PreviewMouseLeftButtonUp 在鼠标指针悬停于此控件上并且用户释放鼠标左键时发生。

(继承自UIElement。

) PreviewMouseMove 在鼠标指针悬停于此控件上并且用户移动该鼠标指针时发生。

(继承自UIElement。

) PreviewMouseRightButtonDown 在鼠标指针悬停于此控件上并且用户按下鼠标右键时发生。

(继承自UIElement。

) PreviewMouseRightButtonUp 在鼠标指针悬停于此控件上并且用户释放鼠标右键时发生。

(继承自UIElement。

) PreviewMouseUp 在鼠标指针悬停于此控件上并且用户释放任意鼠标按钮时发生。

(继承自UIElement。

) PreviewMouseWheel 在鼠标指针悬停于此控件上并且用户滚动鼠标滚轮时发生。

(继承自UIElement。

) PreviewQueryContinueDrag 在拖放操作过程中键盘或鼠标按钮的状态发生变化时发生。

(继承自UIElement。

) PreviewStylusButtonDown 在指针悬停于此控件上并且用户按下手写笔按钮时发生。

(继承自UIElement。

) PreviewStylusButtonUp 在指针悬停于此控件上并且用户释放手写笔按钮时发生。

(继承自UIElement。

) PreviewStylusDown 在手写笔悬停于此控件上并接触数字化仪时发生。

(继承自UIElement。

) PreviewStylusInAirMove 在手写笔掠过控件但并未实际接触数字化仪时发生。

(继承自UIElement。

) PreviewStylusInRange 在手写笔悬停于此控件上方并位于数字化仪的检测范围之内时发生。

(继承自UIElement。

) PreviewStylusMove 在手写笔掠过控件时发生。

若要引发此事件,在数字化仪检测到手写笔时必须移动手写笔;否则,将改为引发PreviewStylusInAirMove。

(继承自UIElement。

) PreviewStylusOutOfRange 当手写笔距离要检测的数字化仪过远时发生。

(继承自UIElement。

) PreviewStylusSystemGesture 在用户执行若干手写笔笔势之一时发生。

(继承自UIElement。

) PreviewStylusUp 在用户将悬停于此控件上的手写笔从数字化仪上移开时发生。

(继承自UIElement。

) PreviewTextInput 在此控件以与设备无关的方式获取文本时发生。

(继承自UIElement。

) QueryContinueDrag 在拖放操作过程中键盘或鼠标按钮的状态发生变化时发生。

(继承自UIElement。

) QueryCursor 在请求显示光标时发生。

每次鼠标指针移至新位置时都会在一个控件上引发此事件,这意味着光标对象可能需要根据其新位置进行更改。

(继承自UIElement。

) RequestBringIntoView 当在此控件上调用BringIntoView时发生。

(继承自FrameworkElement。

) SelectionChanged 在文本选定内容更改后发生。

(继承自TextBoxBase。

) SizeChanged 当此控件上的ActualHeight或ActualWidth属性的值发生更改时发生。

(继承自FrameworkElement。

) SourceUpdated 当此控件上的任何现有属性绑定的源值发生更改时发生。

(继承自FrameworkElement。

) StylusButtonDown 在指针悬停于此控件上并且用户按下手写笔按钮时发生。

(继承自UIElement。

) StylusButtonUp 在指针悬停于此控件上并且用户释放手写笔按钮时发生。

(继承自UIElement。

) StylusDown 在手写笔悬停于此控件上并接触数字化仪时发生。

(继承自UIElement。

) StylusEnter 在手写笔进入此控件的边界时发生。

(继承自UIElement。

) StylusInAirMove 在手写笔掠过控件但并未实际接触数字化仪时发生。

(继承自UIElement。

) StylusInRange 在手写笔悬停于此控件上方并位于数字化仪的检测范围之内时发生。

(继承自UIElement。

) StylusLeave 在手写笔离开此控件的边界时发生。

(继承自UIElement。

) StylusMove 在手写笔掠过此控件时发生。

若要引发此事件,手写笔位于数字化仪上时必须移动。

否则,将改为引发StylusInAirMove。

(继承自UIElement。

) StylusOutOfRange 在手写笔悬停于此控件上方并位于数字化仪的检测范围之外时发生。

(继承自UIElement。

) StylusSystemGesture 在用户执行若干手写笔笔势之一时发生。

(继承自UIElement。

) StylusUp 在用户将悬停于此控件上的手写笔从数字化仪上移开时发生。

(继承自UIElement。

) TargetUpdated 当此控件上的任何属性绑定的目标值发生更改时发生。

(继承自FrameworkElement。

) TextChanged 在文本控件中的内容更改时发生。

(继承自TextBoxBase。

) TextInput 在此控件以与设备无关的方式获取文本时发生。

(继承自UIElement。

) ToolTipClosing 在控件上的任何工具提示关闭之前发生。

(继承自FrameworkElement。

) ToolTipOpening 当控件上的任意工具提示打开时发生。

(继承自FrameworkElement。

) Unloaded 当从加载的控件的元素树中移除元素时发生。

(继承自FrameworkElement。

) 另请参见 参考 RichTextBox 类 System.Windows.Controls 命名空间

c#中怎样将richtextbox转换为流?

byte[] bytes = System.Text.Encoding.ASCII.GetBytes(This.richTextBox1.Text); 流转字符: byte[] bytes = new byte[255]; string str = System.Text.Encoding.ASCII.GetString(bytes,0,bytes.Length);

HTTPS加密协议端口默认是多少且是否支持更换端口访问

看到群里网友们在讨论由于不清楚的原因,有同学的网站无法访问。他的网站是没有用HTTPS的,直接访问他的HTTP是无法访问的,通过PING测试可以看到解析地址已经比较乱,应该是所谓的DNS污染。其中有网友提到采用HTTPS加密证书试试。因为HTTP和HTTPS走的不是一个端口,之前有网友这样测试过是可以缓解这样的问题。这样通过将网站绑定设置HTTPS之后,是可以打开的,看来网站的80端口出现问题,而...

搬瓦工VPS:高端线路,助力企业运营,10Gbps美国 cn2 gia,1Gbps香港cn2 gia,10Gbps日本软银

搬瓦工vps(bandwagonhost)现在面向中国大陆有3条顶级线路:美国 cn2 gia,香港 cn2 gia,日本软银(softbank)。详细带宽是:美国cn2 gia、日本软银,都是2.5Gbps~10Gbps带宽,香港 cn2 gia为1Gbps带宽,搬瓦工是目前为止,全球所有提供这三种带宽的VPS(云服务器)商家里面带宽最大的,成本最高的,没有第二家了! 官方网站:https...

虎跃云-物理机16H/32G/50M山东枣庄高防BGP服务器低至550元每月!

虎跃科技怎么样?虎跃科技(虎跃云)是一家成立于2017年的国内专业服务商,专业主营云服务器和独立服务器(物理机)高防机房有着高端华为T级清洗能力,目前产品地区有:山东,江苏,浙江等多地区云服务器和独立服务器,今天虎跃云给大家带来了优惠活动,为了更好的促销,枣庄高防BGP服务器最高配置16核32G仅需550元/月,有需要的小伙伴可以来看看哦!产品可以支持24H无条件退款(活动产品退款请以活动规则为准...

richtextbox为你推荐
gps数据格式GPS数据如何输入及导出比思论坛永久域名不知道为什么 比思论坛的网站打不开 怎么办呐flash实例Flash元件和实例的概念及关系?broadcast播哈尔滨哪里有卖broadcast播 这个服装品牌的netbios协议NETBEUI协议有什么用?医院排队系统医院排队叫号系统有哪些功能?医院排队系统怎么将排队系统的信息显示在led上东兴证券网站东兴证券超强版下载怎样删除聊天记录如何删除聊天在线沟通什么是在线状态?
虚拟主机试用 厦门虚拟主机 vps论坛 lamp ddos siteground idc测评网 轻博客 ssh帐号 服务器cpu性能排行 52测评网 美国十次啦服务器 php空间申请 卡巴斯基官方免费版 百兆独享 最好的免费空间 共享主机 isp服务商 t云 免费网页空间 更多