Visual Studio 中 RichTextBox 控件的使用


TextBox控件用到的所有属性、事件和方法,RichTextBox控件几乎都能支持,

例如 MaxLength、MultiLine、ScrollBars、SelLength、SelStart 和 SelText。


注意:
TextBoxBase.Undo 方法不可用于 KeyPress 或 TextChanged 事件。
RichTextBox 控件没有TextBox控件一样具有64K字符容量的限制。 

 

http://msdn.microsoft.com/zh-cn/library/system.windows.forms.richtextbox%28VS.80%29.aspx

 

RichTextBox 控件提供许多可对控件内任何文本部分应用格式设置的属性。若要更改文本的格式设置,必须首先选定此文本。只能为选定的文本分配字符和段落格式设置。对选定的文本内容进行设置后,在选定内容后输入的所有文本也用相同的设置进行格式设置,直到更改设置或选定控件文档的不同部分为止。


SelectionFont 属性使您得以将文本以粗体或斜体显示。还可以使用此属性更改文本的大小和字样。


SelectionColor 属性使您得以更改文本的颜色。


若要创建项目符号列表,可以使用 SelectionBullet 属性。


还可以通过设置 SelectionIndent、SelectionRightIndent 和 SelectionHangingIndent 属性调整段落格式设置。


RichTextBox 控件提供具有打开和保存文件的功能的方法。
LoadFile 方法使您得以将现有的 RTF 或 ASCII 文本文件加载到控件中。还可以从已打开的数据流加载数据。SaveFile 使您得以将文件保存到 RTF 或 ASCII 文本中。与 LoadFile 方法相似,还可以使用 SaveFile 方法保存到开放式数据流。


Find 方法被重载,可以同时查找控件文本内的文本字符串以及特定字符。


也可以将 RichTextBox 控件初始化为内存中存储的数据。例如,可以将 Rtf 属性初始化为包含要显示文本的字符串,包括确定如何设置该文本格式的 RTF 代码。


可以使用 DetectUrls 属性适当地显示控件文本中的链接(如到网站的链接)。然后可以处理 LinkClicked 事件以执行与该链接关联的任务。


SelectionProtected 属性使您得以保护控件内的文本不被用户操作。当控件中有受保护的文本时,可以处理 Protected 事件以确定用户何时曾试图修改受保护的文本,并提醒用户该文本是受保护的,或向用户提供标准方式供其操作受保护的文本。


示例 1

创建一个 RichTextBox 控件,该控件将 RTF 文件加载到控件中并搜索单词“Text”的第一个实例。然后代码更改选定文本的字体样式、字体大小和字体颜色并将更改保存到原始文件。在代码示例的最后,将该控件添加到其 Form 中。

     本示例要求1:将代码示例中创建的方法添加到 Form 类中并从窗体的构造函数调用此方法。
     本示例要求2:在 C 驱动器的根目录中创建一个包含单词“Text”的 RTF 文件。


ContractedBlock.gif ExpandedBlockStart.gif 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);
}

 

示例 2

ContractedBlock.gif ExpandedBlockStart.gif 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);
}
}

}
}

 

转载于:https://www.cnblogs.com/guangrou/archive/2010/05/19/1739501.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用C#语言在Visual Studio串口接收STM32数据的简单步骤: 1. 在Visual Studio创建一个新的Windows Forms应用程序项目。 2. 在工具箱添加一个SerialPort控件。SerialPort控件用于打开、关闭和读取串口数据。 3. 在Form的设计视图,将SerialPort控件拖放到窗体上。 4. 打开SerialPort控件的属性窗口,设置串口的属性,如端口号、波特率、数据位、停止位和校验位等。 5. 添加一个按钮控件,用于打开串口连接。在按钮的Click事件使用SerialPort控件的Open()方法打开串口连接。 6. 添加另一个按钮控件,用于关闭串口连接。在按钮的Click事件使用SerialPort控件的Close()方法关闭串口连接。 7. 添加一个RichTextBox控件,用于显示串口接收到的数据。 8. 在SerialPort控件的DataReceived事件使用ReadExisting()方法读取串口接收到的数据,并将数据显示在RichTextBox控件。 以下是示例代码: ```csharp using System; using System.Windows.Forms; using System.IO.Ports; namespace SerialPortExample { public partial class Form1 : Form { private SerialPort serialPort; public Form1() { InitializeComponent(); serialPort = new SerialPort(); serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived); } private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { string data = serialPort.ReadExisting(); Invoke(new Action(() => { richTextBox1.AppendText(data); })); } private void button1_Click(object sender, EventArgs e) { if (!serialPort.IsOpen) { serialPort.PortName = "COM1"; // 设置串口号 serialPort.BaudRate = 115200; // 设置波特率 serialPort.DataBits = 8; // 设置数据位 serialPort.StopBits = StopBits.One; // 设置停止位 serialPort.Parity = Parity.None; // 设置校验位 serialPort.Open(); // 打开串口连接 } } private void button2_Click(object sender, EventArgs e) { if (serialPort.IsOpen) { serialPort.Close(); // 关闭串口连接 } } } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值