2017-02-17 5 views
0

リッチテキストボックスに入力されたときにテキストの書式を設定する必要があります。したがって、たとえば..2つの選択された文字の間にあるリッチテキストボックスのフォーマットテキスト

テキストテキスト(テキストテキストテキスト)テキストテキストテキスト。

括弧で囲まれたテキストは、指定されたフォントと色が変更されたときに変更され、括弧が閉じられると、次のテキストはリッチテキストボックスのデフォルトのフォント/カラーを「使用」します。私がこれまで持って何

...

private void FormatMiddleText (string start, string end, Font font, Color color)  
{ 
    this.richTextBox1.SelectionColor = color; 
    this.richTextBox1.SelectionFont = font; 
} 

使い方...

FontFamily fontFamily = new FontFamily("Arial"); 
Font font = new Font(
    fontFamily, 
    16, 
    FontStyle.Bold, 
    GraphicsUnit.Pixel); 


FormatMiddleText("(",")", Color.Red, font) 

RichTextBox選択に開始と終了の間にあるテキストを作成する方法を考え出すことはできません文字列。

+0

フォーマットを適用する前にテキストを選択するのは忘れてしまいます。 –

答えて

1

あなたが必要とするのは、選択したテキストを追跡することです。始まりと終わり。これはあなたのFormatMiddleText方法に行かなければならない、それがなければならないので、インデックスintが好ましいタイプのようになります。

private void FormatMiddleText(int start, int end, Font font, Color color) 
{ 
    richTextBox1.SelectionStart = start; 
    richTextBox1.SelectionLength = end; 

    this.richTextBox1.SelectionColor = color; 
    this.richTextBox1.SelectionFont = font; 
    // reset the selection start so you can keep typing at the right hand side 
    richTextBox1.SelectionStart = richTextBox1.Text.Length; 
} 

は今、あなたはタイピングを追跡する必要があります。 startIndexと、テキストの書式がどのくらいあるかを調べる必要があります。

:またフラグは、今、あなたはテキストでのユーザーの種類として書式設定を処理するために TextChangedイベントを使用することができ、あなたの「クール」形式または退屈なデフォルトは1

int startIndex = 0; 
int charCount = 0; 
bool startFormatting = false; 

適用するかどうかを知ることが良いでしょう

private void richTextBox1_TextChanged(object sender, EventArgs e) 
{ 
    if (richTextBox1.Text.Last() == '(') 
    { 
     // remember the start index 
     startIndex = richTextBox1.Text.Length-1; // -1 will take the "(" also to be formatted 
     startFormatting = true; // now you can start formatting with the cool font 
    } 

    if (richTextBox1.Text.Last() == ')') 
    { 
     MessageBox.Show("found end "); 
     startIndex = richTextBox1.Text.Length; 
     startFormatting = false; // now you can proceed to format with boring default font    
    } 

    charCount++; 

    if (startFormatting) 
    { 
     FormatMiddleText(startIndex, charCount, font, Color.Red); 
    } 
    else 
    { 
     FormatMiddleText(startIndex, charCount, DefaultFont, Color.Black); 
    } 
} 

フォントをリセットして、定型書式を使用せずにデフォルトで書込みを続ける方法が見つかりませんでした。 richTextBox1.Font = DefaultFont;を設定すると、かっこで書式がリセットされるためです。だから私は書式を維持することを選んだ。方法を見つけたら、私に教えてください

0

これはすべきことです。

private void rtb_KeyUp(object sender, KeyEventArgs e) 
    { 
     if (e.KeyCode == Keys.D0 && e.Shift) 
     { 
      this.richTextBox1.SelectionColor = Color.Black; 
     } 
    } 

    private void rtb_KeyPress(object sender, KeyPressEventArgs e) 
    { 
     if (e.KeyChar == '(') 
     { 
      this.richTextBox1.SelectionColor = Color.Red; 
     } 
    } 

あなたは、より複雑なフォントプロパティを提供するために、SelectionFontの代わりSelectionColorを使用することができます。また、同じ効果を得るには、かっこを任意の開閉文字で置き換えることができます。お役に立てれば!

+0

私はこれがtxtresponseとなるように働かないのですか? – user3755946

+0

お詫び申し上げます。 txtResponseはあなたのRichTextBoxの名前であるはずでした。私はそれに応じて答えを編集しました。 –

関連する問題