Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have 4 lines of text in RTF from Microsoft Word

First line of text.
Second line of text.
Third line of text.
Fourth line of text.


I have also code for generating text from each line to console:

rtb = New RichTextBox
rtb.Rtf = My.Computer.Clipboard.GetText(TextDataFormat.Rtf)
Dim linesCount As Integer = rtb.Lines.Count
For i = 0 To linesCount - 1
    Dim start As Integer = rtb.GetFirstCharIndexFromLine(i)
    Dim currLine As Integer = rtb.GetLineFromCharIndex(start)
    Dim currLineText As String = rtb.Lines(currLine)
    Dim length As Integer = currLineText.Length

    rtb.Select(start, length)
    Debug.Print(rtb.SelectedText)
Next


And there is my issue.

While I use dynamically created RichTextBox (as above) I get weirdo result

First line of text.
text.
Second line of
Second line of text
text.
Third line of


But when I switch using RichTextBox1 created on Form1 I get what I espected.

Form11.RichTextBox1.Rtf = My.Computer.Clipboard.GetText(TextDataFormat.Rtf)
Dim linesCount As Integer = Form11.RichTextBox1.Lines.Count
For i = 0 To linesCount - 1
    Dim start As Integer = Form11.RichTextBox1.GetFirstCharIndexFromLine(i)
    Dim currLine As Integer = Form11.RichTextBox1.GetLineFromCharIndex(start)
    Dim currLineText As String = Form11.RichTextBox1.Lines(currLine)
    Dim length As Integer = currLineText.Length

    Form11.RichTextBox1.Select(start, length)
    Debug.Print(Form11.RichTextBox1.SelectedText)
Next


First line of text.
Second line of text.
Third line of text.
Fourth line of text.


Any idea why is this happening? I would prefer to use dynamically created RichTextBox in my scenario.

What I have tried:

Not sure what to write here :)
Posted
Updated 29-May-18 2:46am

1 solution

You haven't set the width of the dynamically created RichTextBox so it is wrapping the text. Try setting rtb.Width to the same value as in your RichTextBox1
 
Share this answer
 
Comments
Marcin Bednarek 29-May-18 9:04am    
Ahh, no width, but in my case better approach is to set WordWrap to False. But you idea fixed my problem. Thank you very much!
CHill60 29-May-18 9:04am    
My pleasure!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900