0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RichText に書式付きで追加する

Posted at

RichTextBox (リッチテキストボックス)

RichTextBox は、フォントを設定したり、色を設定できるテキストボックス

System.Windows.Forms.RichTextBox

RichTextBox (リッチテキストボックス)

既に書式設定がされている状態で、テキストを追加する場合、↓のような方法では無理

String str = "Hello";
int iti = this.richTextBox1.TextLength;
this.richTextBox1.Text += str;
this.richTextBox1.SelectionStart = iti;
this.richTextBox1.SelectionStart = this.richTextBox1.TextLength - iti;
this.richTextBox1.SelectionColor = Color.Red;

↑のやり方だと、書式が一旦初期化(.Textなので、テキスト/文字列だけになってしまう)

↓のやり方でイケた

String str = "Hello";
this.richTextBox1.SelectionStart = this.richTextBox1.TextLength;
this.richTextBox1.SelectionColor = Color.Red;
this.richTextBox1.SelectionLength = 0;
this.richTextBox1.SelectedText = str;

選択領域に文字列を設定して、書式を設定して・・・という方法でうまくイケた。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?