【问题标题】:RichTextBox: using RichTextBox.Selection.start etc with tabbed contentRichTextBox:使用带有标签内容的 RichTextBox.Selection.start 等
【发布时间】:2011-09-20 16:40:28
【问题描述】:

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

示例代码

 //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”和构造字符串

用空格对齐文本。]

【问题讨论】:

    标签: c# winforms richtextbox


    【解决方案1】:

    看起来您的 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;
    

    【讨论】:

    • 感谢 LarsTech 这肯定回答了问题
    • 它肯定回答了所提出的问题。但我真正尝试的是限制非空格字符的下划线!有什么想法吗?
    • @MartinLord 无论在 RTB 中选择什么并应用字体,都会得到该字体。您只需从下划线字体中排除非空格字符,或者相反,在应用非下划线字体时包括非空格字符,这就是我的解决方案所做的。没有神奇的公式可以将非空格字符排除在接收特定字体之外。你必须选择你的范围并应用你的字体。
    • 问题的出现,我相信是因为非空格字符在添加的文本中并不明确,而是从文本末尾到选项卡指定的 PIXEL 坐标之间的空白。选择开始,长度与字符有关。制表符位置不一定是整数字符位置,制表符之前的尾随空格也不一定是整数个字符。我开始得出结论,做我想做的唯一(?)方法是在所需的选项卡位置使用 Graphics.DrawString。谢谢你们的cmets。
    猜你喜欢
    • 2015-05-31
    • 2015-07-03
    • 1970-01-01
    • 1970-01-01
    • 2019-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多