2012-06-25 15 views
5

RTBのテキストの特定の部分は、フォントスタイル/色を変更する意味ではなく、特定の色でブロック選択を行う意味で強調表示する必要があります。これは、Visual Studioがデバッグモードで行をハイライト表示する方法と似ています。リッチテキストボックステキストブロックをハイライトする方法

RTBを使用してこの機能を実現するにはどうすればよいですか?可能でない場合は、上記の作業を実行する別の方法を聞きたいと思います。

+0

のための線の色を交互に持っている[方法の可能性の重複:あなたが興味を持って、あなたはずっと

シナリオ

https://sites.google.com/site/greateindiaclub/mobil-apps/windows8/customwpfrichtextboxwithcolorchangeandhighlightfunctionality

ソースコードを心配せずに直接このユーザーコントロールを再利用することができる場合Winforms RichTextBox?](http://stackoverflow.com/questions/5982006/how-to-have-alternating-line-colors-for-a-winforms-richtextbox) –

+0

私はあなたが[Sci ntillaNET](http://scintillanet.codeplex.com/)。一方、RTBで自分でこれを行うには、まず[TextBoxBase.Lines](http://msdn.microsoft.com/en-us/library/system)を使って 'lineNumber'を見つけてください.windows.forms.textboxbase.lines.aspx)プロパティ。 Then ... //その番号から行を選択しますrichTextBox.GetFirstCharIndexFromLine(lineNumber); richTextBox.Select(startIndex、length); //選択したテキストを前面と背面の色に設定します。richTextBox.SelectionColor = System.Drawing.Color.White; richTextBox.SelectionBackColor = System.Drawin – ABH

答えて

7

私はあなたがScintillaNETを探していると思います。

一方、RTBで自分でこれを行う場合は、まずプロパティを使用してlineNumberを見つけてください。その後...

//Select the line from it's number 
startIndex = richTextBox.GetFirstCharIndexFromLine(lineNumber); 
richTextBox.Select(startIndex, length); 

//Set the selected text fore and background color 
richTextBox.SelectionColor = System.Drawing.Color.White; 
richTextBox.SelectionBackColor= System.Drawing.Color.Blue; 
+0

'StartIndex'変数に保存するのではなく、' GetFirstCharIndexFromLine() '戻り値を無視していませんか? – Jack

+1

@Jack - 更新しました。ハイライトに感謝します。 – ABH

9

はい、あなたはRichTextBox.SelectionBackColorプロパティを使用してリッチテキストボックスの選択のBackColorプロパティを設定することができます。

int blockStart = 1; //arbitrary numbers to test 
int blockLength = 15; 
richTextBox1.SelectionStart = blockStart; 
richTextBox1.SelectionLength = blockLength; 
richTextBox1.SelectionBackColor = Color.Yellow; 
関連する問題