RichTextBox:使用带有选项卡式内容的RichTextBox.Selection.start等

时间:2011-09-20 16:40:28

标签: c# winforms richtextbox

我希望强调所选文字,但发现下划线仍然是下一个标签 - 停止

示例代码

 //rtbList is a richTextBox
        rtbList.AppendText("\t");  
        selStart = rtbList.TextLength;
        rtbList.AppendText("Bought"); 
        rtbList.SelectionStart = selStart;           
        rtbList.SelectionLength = rtbList.TextLength - selStart;
        rtbList.SelectionFont = hdgFont; // bold & underline
        rtbList.AppendText("\t");
        //
        selStart = rtbList.TextLength;
        rtbList.SelectionLength = 0;
        rtbList.AppendText("Maturity");
        rtbList.SelectionStart = selStart;            
        rtbList.SelectionLength = rtbList.TextLength - selStart;
        rtbList.SelectionFont = hdgFontNoUnderline;

无论如何要克服这个问题,还是rtf格式的基本“缺陷”?

[显然我可以通过使用固定格式来避免这种情况,例如“快递”和构建字符串

用空格来对齐文字。]

1 个答案:

答案 0 :(得分:0)

看起来您的selStart发生在AppendText("\t")行之前。您的NoUnderline字体不包括包含Tab的范围。

基本上,在下划线字体后添加的任何文本都会获得该字体,直到您更改它为止。

rtbList.AppendText("\t");  
selStart = rtbList.TextLength;
rtbList.AppendText("Bought"); 
rtbList.SelectionStart = selStart;           
rtbList.SelectionLength = rtbList.TextLength - selStart;
rtbList.SelectionFont = hdgFont; // bold & underline

//Move before AppendText:
selStart = rtbList.TextLength;

rtbList.AppendText("\t");
rtbList.SelectionLength = 0;
rtbList.AppendText("Maturity");
rtbList.SelectionStart = selStart;            
rtbList.SelectionLength = rtbList.TextLength - selStart;
rtbList.SelectionFont = hdgFontNoUnderline;