b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

richtextbox 添加

电脑杂谈  发布时间:2016-12-28 01:01:19  来源:网络整理

语法:RichTextBox

说明:

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

C# RichTextBox控件能以 rtf 格式和普通 ASCII 文本格式这两种形式打开和保存文件。richtextbox 添加可以使用控件的方法(LoadFile 和 SaveFile)直接读写文件,或使用与 Visual Basic 文件输入/输出语句联结的、诸如 SelRTF 和 TextRTF 之类的控件属性打开和保存文件。

通过使用OLEObjects 集合,RichTextBox 控件支持对象的嵌入。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 目录内。

例子:

  1.   private void 打开图形文件ToolStripMenuItem_Click(object sender, EventArgs e)  
  2.          {  
  3.              string NameFile;  
  4.              if (this.openFileDialog1.ShowDialog() == DialogResult.OK)  
  5.              {  
  6.                  NameFile = this.openFileDialog1.FileName;  
  7.                  if (NameFile != "")  
  8.                  {  
  9.                      this.pictureBox1.Image = Image.FromFile(NameFile);  
  10.                  }  
  11.              }  
  12.          }  
  13.        private void 打开文本文件ToolStripMenuItem_Click(object sender, EventArgs e)  
  14.          {  
  15.              string Filename;  
  16.              pictureBox1.Visible = false;  
  17.              if (this.openFileDialog1.ShowDialog() == DialogResult.OK)  
  18.              {  
  19.                  Filename = openFileDialog1.FileName;  
  20.                  if (Filename != "")  
  21.                  {  
  22.                      this.textBox1.Text = Filename;  
  23.                      this.richTextBox1.LoadFile(@Filename, RichTextBoxStreamType.PlainText);  
  24.                  }  
  25.              }  
  26.          }  
  27.  
  28.  
  29.  
  30.  
  31.  
  32. //构造函数  
  33.                this.textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress);  
  34.                this.textBox1.Validating += new CancelEventHandler(textBox1_Validating);  
  35.                this.richTextBox1.LinkClicked += new LinkClickedEventHandler(richTextBox1_LinkClicked);  
  36.          //取消或置为粗体  
  37.          private void button2_Click(object sender, System.EventArgs e)  
  38.          {  
  39.                Font oldFont = this.richTextBox1.SelectionFont;  
  40.                Font newFont;  
  41.                if (oldFont.Bold)  
  42.                    newFont = new Font(oldFont,oldFont.Style & ~FontStyle.Bold);  
  43.                else 
  44.                    newFont = new Font(oldFont,oldFont.Style | FontStyle.Bold);  
  45.                this.richTextBox1.SelectionFont = newFont;  
  46.                this.richTextBox1.Focus();  
  47.          }  
  48.          //取消或置为斜体  
  49.          private void button7_Click(object sender, System.EventArgs e)  
  50.          {  
  51.                Font oldFont = this.richTextBox1.SelectionFont;  
  52.                Font newFont;  
  53.                if (oldFont.Italic)  
  54.                    newFont = new Font(oldFont,oldFont.Style & ~FontStyle.Italic);  
  55.                else 
  56.                    newFont = new Font(oldFont,oldFont.Style | FontStyle.Italic);  
  57.                this.richTextBox1.SelectionFont = newFont;  
  58.                this.richTextBox1.Focus();  
  59.          }  
  60.          //取消或加上下划线  
  61.          private void button8_Click(object sender, System.EventArgs e)  
  62.          {  
  63.                Font oldFont = this.richTextBox1.SelectionFont;  
  64.                Font newFont;  
  65.                if (oldFont.Underline)  
  66.                    newFont = new Font(oldFont,oldFont.Style & ~FontStyle.Underline);  
  67.                else 
  68.                    newFont = new Font(oldFont,oldFont.Style | FontStyle.Underline);  
  69.                this.richTextBox1.SelectionFont = newFont;  
  70.                this.richTextBox1  
  71.                    .Focus();  
  72.          }  
  73.          //取消或置为居中  
  74.          private void button5_Click(object sender, System.EventArgs e)  
  75.          {  
  76.                if (this.richTextBox1.SelectionAlignment == HorizontalAlignment.Center)  
  77.                    this.richTextBox1.SelectionAlignment = HorizontalAlignment.Left;  
  78.                else 
  79.                    this.richTextBox1.SelectionAlignment = HorizontalAlignment.Center;  
  80.                this.richTextBox1.Focus();  
  81.          }  
  82.          private void textBox1_KeyPress(object sender, KeyPressEventArgs e)  
  83.          {  
  84.                if((e.KeyChar <  48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar !=13)   
  85.                {  
  86.                    e.Handled = true;  
  87.                }  
  88.                else if(e.KeyChar == 13)   
  89.                {  
  90.                    TextBox txt = (TextBox)sender;  
  91.                    if(txt.Text.Length > 0)   
  92.                        ApplyTextSize(txt.Text);  
  93.                    e.Handled = true;  
  94.                    this.richTextBox1.Focus();  
  95.                }  
  96.          }  
  97.          private void textBox1_Validating(object sender, CancelEventArgs e)  
  98.          {  
  99.                TextBox txt = (TextBox)sender;  
  100.                ApplyTextSize(txt.Text);  
  101.                this.richTextBox1.Focus();  
  102.            }  
  103.          //改变字体大小  
  104.          private void ApplyTextSize(string textSize)  
  105.          {  
  106.                float newSize = Convert.ToSingle(textSize);  
  107.                FontFamily currentFontFamily;  
  108.                Font newFont;  
  109.                currentFontFamily = this.richTextBox1.SelectionFont.FontFamily;  
  110.                newFont = new Font(currentFontFamily, newSize);  
  111.                this.richTextBox1.SelectionFont = newFont;  
  112.          }  
  113.          //打开网页  
  114.          private void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)  
  115.          {  
  116.                System.Diagnostics.Process.Start(e.LinkText);  
  117.          }  
  118.          //打开文件  
  119.          private void button1_Click(object sender, System.EventArgs e)  
  120.          {  
  121.                try 
  122.                {  
  123.                    this.richTextBox1.LoadFile(@"..\..\test.txt");  
  124.                }  
  125.                catch(System.IO.FileNotFoundException)  
  126.                {  
  127.                    MessageBox.Show("File not found!");  
  128.                }  
  129.          }  
  130.          //保存文件  
  131.          private void button6_Click(object sender, System.EventArgs e)  
  132.          {  
  133.                try 
  134.                {  
  135.                    this.richTextBox1.SaveFile(@"..\..\test.txt");  
  136.                }  
  137.                catch(System.Exception err)  
  138.                {  
  139.                    MessageBox.Show(err.Message);  
  140.                }  
  141.          } 


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-23636-1.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    热点图片
    拼命载入中...