当前位置: 移动技术网 > IT编程>开发语言>c# > C#中richtextbox使用方法详解

C#中richtextbox使用方法详解

2019年07月18日  | 移动技术网IT编程  | 我要评论
c#中richtextbox使用方法和textbox基本一样,只不过richtext除了txt外,还支持rtf格式的文档。本文详细介绍richtextbox的使用方法供大家

c#中richtextbox使用方法和textbox基本一样,只不过richtext除了txt外,还支持rtf格式的文档。本文详细介绍richtextbox的使用方法供大家参考,具体如下:

一、richtextbox的使用方法

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

语法:richtextbox
说明:
richtextbox 提供了一些属性,对于本控件文本的任何部分,用这些属性都可以指定格式。为了改变文本的格式,首先要选定它。只有选定的文本才能赋予字符和段落格式。使用这些属性,可把文本改为粗体或斜体,或改变其颜色,以及创建上标和下标。通过设置左右缩进和悬挂式缩进,可调整段落的格式。

richtextbox 控件能以 rtf 格式和普通 ascii 文本格式这两种形式打开和保存文件。可以使用控件的方法(loadfile 和 savefile)直接读写文件,或使用与 visual basic 文件输入/输出语句联结的、诸如 selrtf 和 textrtf 之类的控件属性打开和保存文件。

通过使用 oleobjects 集合,richtextbox 控件支持对象的嵌入。插入到控件中的每个对象,都代表 oleobject 对象。用这样的控件,就可以创建包含其它文档或对象的文档。例如,可创建这样的文档,它有一个嵌入的 microsoft excel 电子数据表格、或 microsoft word 文档、或其它已在系统中注册的 ole 对象。为了把一个对象插入到 richtextbox 控件中,只需简单地拖动一个文件(例如在windows 95“资源管理器”中的拖动),或拖动的是另一应用程序(如 microsoft word)所用文件的一个突出显示的区域,然后将所拖内容直接放入控件。

richtextbox 控件支持 ole 对象的剪贴板和 ole 拖/放操作。从剪贴板中粘贴进一个对象时,它被插在当前插入点处。一个对象被拖放到控件时,插入点将跟踪着鼠标光标的移动,直至鼠标按钮释放时该对象即被插入。这种行为和 microsoft word 的一样。

使用 selprint 方法,可以打印 richtextbox 控件的全部或部分文本。

因为 richtextbox 是一个数据绑定控件,通过 data 控件可以把它绑定到 microsoft access 数据库的 binary 或 memo 字段上,也可把它绑定到具有相同容量的其它数据库字段上(例如 sql 服务器中的 text 数据类型的字段)。

标准 textbox 控件用到的所有属性、事件和方法,richtextbox 控件几乎都能支持,例如 maxlength、 multiline、 scrollbars、 sellength、 selstart 和 seltext。对于那些可以使用 textbox 控件的应用程序,也可以很容易地使用 richtextbox 控件。而且,richtextbox 控件并没有和标准 textbox 控件一样具有 64k 字符容量的限制。

发行注意 为了能在应用程序中使用 richtextbox 控件,必须把richtx32.ocx 文件添加到工程中。因此,在应用程序发行时,richtx32.ocx 文件就应安装在 microsoft windows 的 system 目录内。

二、richtextbox实例代码:

private void 打开图形文件toolstripmenuitem_click(object sender, eventargs e)
 {
   string namefile;
   if (this.openfiledialog1.showdialog() == dialogresult.ok)
   {
 namefile = this.openfiledialog1.filename;
 if (namefile != "")
 {
   this.picturebox1.image = image.fromfile(namefile);
 }
   }
 }
private void 打开文本文件toolstripmenuitem_click(object sender, eventargs e)
 {
   string filename;
   picturebox1.visible = false;
   if (this.openfiledialog1.showdialog() == dialogresult.ok)
   {
 filename = openfiledialog1.filename;
 if (filename != "")
 {
   this.textbox1.text = filename;
   this.richtextbox1.loadfile(@filename, richtextboxstreamtype.plaintext);
 }
   }
 }

//构造函数
    this.textbox1.keypress += new keypresseventhandler(textbox1_keypress);
    this.textbox1.validating += new canceleventhandler(textbox1_validating);
    this.richtextbox1.linkclicked += new linkclickedeventhandler(richtextbox1_linkclicked);
 //取消或置为粗体
 private void button2_click(object sender, system.eventargs e)
 {
    font oldfont = this.richtextbox1.selectionfont;
    font newfont;
    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();
 }
 //取消或置为斜体
 private void button7_click(object sender, system.eventargs e)
 {
    font oldfont = this.richtextbox1.selectionfont;
    font newfont;
    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();
 }
 //取消或加上下划线
 private void button8_click(object sender, system.eventargs e)
 {
    font oldfont = this.richtextbox1.selectionfont;
    font newfont;
    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();
 }
 //取消或置为居中
 private void button5_click(object sender, system.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)
 {
    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();
    }
 }
 private void textbox1_validating(object sender, canceleventargs e)
 {
    textbox txt = (textbox)sender;
    applytextsize(txt.text);
    this.richtextbox1.focus();
  }
 //改变字体大小
 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;
 }
 //打开网页
 private void richtextbox1_linkclicked(object sender, linkclickedeventargs e)
 {
    system.diagnostics.process.start(e.linktext);
 }
 //打开文件
 private void button1_click(object sender, system.eventargs e)
 {
    try
    {
  this.richtextbox1.loadfile(@"..\..\test.txt");
    }
    catch(system.io.filenotfoundexception)
    {
  messagebox.show("file not found!");
    }
 }
 //保存文件
 private void button6_click(object sender, system.eventargs e)
 {
    try
    {
  this.richtextbox1.savefile(@"..\..\test.txt");
    }
    catch(system.exception err)
    {
  messagebox.show(err.message);
    }
 }

三、在 richtextbox 的内容内搜索文本:

1.重载列表:

在 richtextbox 控件的文本中搜索字符列表中某个字符的第一个实例
 

public int find(char[]);

下面的示例在 richtextbox 的内容中搜索在 text 参数中传递到方法的字符。如果在 richtextbox 中找到了 text 数组的内容,则该方法返回所找到值的索引;否则,它将返回 -1。该示例假定此方法位于 form 的类中,该窗体包含一个名为 richtextbox1 的 richtextbox 控件和一个连接到该示例中定义的单击事件处理方法的 button 控件(名为 button1)。

如下代码:

private void button1_click(object sender, system.eventargs e)
{
   messagebox.show(findmytext(new char[]{'d','e','l','t','a'}).tostring());
}
public int findmytext(char[] text)
{
   // initialize the return value to false by default.
   int returnvalue = -1;
   // ensure that a search string has been specified and a valid start point.
   if (text.length > 0) 
   {
     // obtain the location of the first character found in the control
     // that matches any of the characters in the char array.
     int indextotext = richtextbox1.find(text);
     // determine whether the text was found in richtextbox1.
     if(indextotext >= 0)
     {
       // return the location of the character.
       returnvalue = indextotext;
     }
   }
   return returnvalue;
}

2.在 richtextbox 控件的文本中搜索字符串。
 

public int find(string);

从特定的起始点开始,在 richtextbox 控件的文本中搜索字符列表中某个字符的第一个实例。

 public int find(char[], int);

在对搜索应用特定选项的情况下,在 richtextbox 控件的文本中搜索字符串。

 public int find(string, richtextboxfinds);

下面的示例在 richtextbox 的整个内容中搜索传递到此方法文本参数中的搜索字符串的第一个实例。如果在 richtextbox 中找到搜索字符串,此方法将返回 true 值并突出显示文本;否则返回 false。本示例还在搜索中指定匹配指定搜索字符串的大小写的选项。此示例假定此方法放置在 form 的类中,并且该类包含一个名为 richtextbox1 的 richtextbox。

具体代码如下: 

public bool findmytext(string text)
{
  // initialize the return value to false by default.
  bool returnvalue = false;
  // ensure a search string has been specified.
  if (text.length > 0) 
  {
    // obtain the location of the search string in richtextbox1.
    int indextotext = richtextbox1.find(text, richtextboxfinds.matchcase);
    // determine if the text was found in richtextbox1.
    if(indextotext >= 0)
    {
     returnvalue = true;
    }
  }
  return returnvalue;
}

在 richtextbox 控件的某个文本范围中搜索字符列表的某个字符的第一个实例。
 

public int find(char[], int, int);

在对搜索应用特定选项的情况下,在 richtextbox 控件的文本中搜索位于控件内特定位置的字符串。

 public int find(string, int, richtextboxfinds);

在对搜索应用特定选项的情况下,在 richtextbox 控件文本中搜索控件内某个文本范围内的字符串。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网