System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Control
System.Windows.Forms.TextBoxBase
System.Windows.Forms.RichTextBox
public class RichTextBox : TextBoxBase
このクラスのコンストラクタも、他のコントロール関連クラスと同様にusing System;
using System.Drawing;
using System.Windows.Forms;
public class WinMain : Form {
MenuItem[] menuItem;
RichTextBox textBox1 = new RichTextBox();
public static void Main() {
Application.Run(new WinMain());
}
public WinMain() {
menuItem = new MenuItem[] {
new MenuItem("色を変える" , new EventHandler(menuItemClick)) ,
new MenuItem("フォントを変える" , new EventHandler(menuItemClick)) ,
};
textBox1.Dock = DockStyle.Fill;
textBox1.ContextMenu = new ContextMenu(menuItem);
Controls.Add(textBox1);
}
private void menuItemClick(object sender , EventArgs e) {
if(sender == menuItem[0])
textBox1.SelectionColor = Color.Red;
else if(sender == menuItem[1])
textBox1.SelectionFont = new Font("MS Serif" , 20);
}
}

| メンバ | 意味 |
|---|---|
| PlainText | OLE (Object Linking and Embedding) オブジェクトの代わりに 空白が含まれているプレーン テキスト ストリーム |
| RichNoOleObjs | OLE オブジェクトの代わりの空白を持つリッチ テキスト形式 (RTF: Rich Text Format) のストリーム この値は、 RichTextBox コントロールの SaveFile メソッドを使用する場合に限り有効です |
| RichText | リッチ テキスト形式 (RTF) ストリーム |
| TextTextOleObjs | OLE オブジェクトのテキスト表現を持つプレーン テキスト ストリーム この値は、 RichTextBox コントロールの SaveFile メソッドを使用する場合に限り有効です |
| UnicodePlainText | OLE オブジェクトの代わりに空白が含まれているテキスト ストリーム テキストは Unicode でエンコードされています |
using System;
using System.Drawing;
using System.Windows.Forms;
public class WinMain : Form {
MenuItem[] menuItem;
RichTextBox textBox1 = new RichTextBox();
public static void Main() {
Application.Run(new WinMain());
}
public WinMain() {
menuItem = new MenuItem[] {
new MenuItem("色を変える" , new EventHandler(menuItemClick)) ,
new MenuItem("フォントを変える" , new EventHandler(menuItemClick)) ,
new MenuItem("保存" , new EventHandler(menuItemClick)) ,
};
textBox1.Dock = DockStyle.Fill;
textBox1.ContextMenu = new ContextMenu(menuItem);
Controls.Add(textBox1);
}
private void menuItemClick(object sender , EventArgs e) {
if(sender == menuItem[0])
textBox1.SelectionColor = Color.Red;
else if(sender == menuItem[1])
textBox1.SelectionFont = new Font("MS Serif" , 20);
else if (sender == menuItem[2])
textBox1.SaveFile("test.rtf" , RichTextBoxStreamType.RichNoOleObjs);
}
}
これは、先ほどのプログラムに保存メニューを設けた拡張版ですusing System.Drawing;
using System.Windows.Forms;
public class WinMain : Form {
RichTextBox textBox1 = new RichTextBox();
public static void Main() {
Application.Run(new WinMain());
}
public WinMain() {
textBox1.Dock = DockStyle.Fill;
textBox1.LoadFile("test.rtf");
Controls.Add(textBox1);
}
}
このプログラムは、カレントフォルダ内の test.rtf ファイルを読み込みますusing System;
using System.Drawing;
using System.Windows.Forms;
public class WinMain : Form {
TextBox textBox1 = new TextBox();
RichTextBox textBox2 = new RichTextBox();
public static void Main() {
Application.Run(new WinMain());
}
public WinMain() {
textBox1.Dock = DockStyle.Top;
textBox1.Multiline = textBox2.Multiline = true;
textBox1.TextChanged += new EventHandler(textBox1_TextChanged);
textBox2.Dock = DockStyle.Bottom;
textBox2.ReadOnly = true;
Controls.AddRange(new Control[] {textBox1 , textBox2});
}
private void textBox1_TextChanged(object sender , EventArgs e) {
try {textBox2.Rtf = textBox1.Text;}
catch(Exception) {}
}
override protected void OnLayout(LayoutEventArgs e) {
textBox1.Height = textBox2.Height = ClientSize.Height / 2;
base.OnLayout(e);
}
}
