Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
315 views
in Technique[技术] by (71.8m points)

c# - .NET - Long Line in RichTextBox Wrapped after 3,510 Characters

I have a RichTextBox in a WInForms program that is wrapping any line that is longer than 3,510 characters. I have WordWrap set to false, so all lines under that length extend normally without wrapping. What I used to do to get around this was to set RightMargin to a high number such as 100,000, which still works, but now that I am on Windows 7 and not XP, I get a scroll bar that scrolls as though the text box had lines of that large length, when it doesn't.

To replicate, just create a winforms program with a RichTextBox and Button. In the button's click event, put the following:

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 3511; i++)
            sb.Append('A');
        richTextBox1.Text = sb.ToString();

After you see the wrapping, change the RightMargin of the text box to 100000, and notice the scrollbar that appears even before running the program (only on 7 and probably Vista). The line no longer wraps, but I want the scrollbar to only act on the text in the box, and not some pre-determined length.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could set the rightmargin of the richTextBox to the width of the string? Like this

richTextBox1.RightMargin = 
TextRenderer.MeasureText(sb.ToString(), this.richTextBox1.Font).Width;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...