user934820
user934820

Reputation: 1178

RightToLeft RichTextBox with pre-loaded text

Ok i found a strange type of bug in MS default Richtextbox in vb.net 2008. If we add some line of text in Richtextbox programmaticlly. there is a gape from right side. see the image below

ٰerror

here is my code

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim f As New Form
    Dim rtb As New RichTextBox
    f.Width = 500
    f.Height = 500
    rtb.RightToLeft = Windows.Forms.RightToLeft.Yes
    For i = 1 To 20
        rtb.AppendText("بسم اللہ الرحمن الرحیم" & vbNewLine)
    Next
    rtb.Dock = DockStyle.Fill
    f.Controls.Add(rtb)
    f.Show()
End Sub

Upvotes: 1

Views: 597

Answers (2)

Andrew Morton
Andrew Morton

Reputation: 25013

If you also set

rtb.Width = 500
rtb.Height = 500

Then it works as desired.

I agree it is strange behaviour. It does the same with VS 2012 RC.

Upvotes: 0

LarsTech
LarsTech

Reputation: 81610

I can't explain it, but try changing the order of your code so that the RichTextBox control is added to the form before you append the text. This worked for me:

Private Sub btn1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn1.Click
  Dim f As New Form
  f.Width = 500
  f.Height = 500

  Dim rtb As New RichTextBox
  rtb.Name = "rtb"
  rtb.Dock = DockStyle.Fill
  rtb.RightToLeft = RightToLeft.Yes

  f.Controls.Add(rtb)
  For i = 1 To 25
    rtb.AppendText("بسم اللہ الرحمن الرحیم" & vbNewLine)
  Next
  f.Show()

  f.BeginInvoke(New Action(Of RichTextBox)(AddressOf RunFix), rtb)
End Sub

Sub RunFix(ByVal rtfControl As RichTextBox)
  rtfControl.Select(0, 0)
  rtfControl.ScrollToCaret()
End Sub

I added a hack BeginInvoke method that performs a ScrollToCaret() call which seems to fix the problem.

Upvotes: 1

Related Questions