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

C# RichTextBox:使用带有选项卡内容的RichTextBox.Selection.start等,c#,winforms,richtextbox,C#,Winforms,Richtextbox,我希望在所选文本下加下划线,但发现下划线会继续到下一个选项卡- 停止 示例代码 //rtbList is a richTextBox rtbList.AppendText("\t"); selStart = rtbList.TextLength; rtbList.AppendText("Bought"); rtbList.SelectionStart = selStart; rtbLis

我希望在所选文本下加下划线,但发现下划线会继续到下一个选项卡- 停止

示例代码

 //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格式中的一个基本“缺陷”

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


使用空格对齐文本。]

看起来您的selStart发生在
追加文本(“\t”)
行之前。您的NoUnderline字体不包括包含选项卡的范围

本质上,任何在下划线字体之后附加的文本都将获得该字体,直到您更改它为止

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;

谢谢LarsTech,它肯定回答了问题,它肯定回答了提出的问题。但我真正尝试的是将下划线限制为非空格字符!有什么想法吗?@MartinLord无论在RTB中选择什么,只要应用一种字体,就会得到该字体。您只需要从下划线字体中排除非间隔字符,或者反过来,在应用非下划线字体时包括非间隔字符,这就是我的解决方案所做的。没有神奇的公式可以排除接收特定字体时的非间隔字符。你必须选择你的范围并应用你的字体。我认为问题的出现是因为非空格字符在所添加的文本中并不明确,而是从文本末尾到选项卡指定的像素坐标之间的空白。选择开始,长度与字符相关。制表符位置不一定是整型字符位置,制表符前的尾随空格也不一定是整型字符数。我开始得出结论,我想要做的唯一(?)方法是在所需的制表符位置使用Graphics.DrawString。谢谢你的评论。