locked
Left Margin in a RichTextBox Control

    Question

  • OK, I must be missing something obvious here. How do I set a left margin in a richtextbox? This would be separate from indents. Just need the text to NOT be crowded up against the left side of the control. Maybe 5 pixels or so.

    Thanks - AB


    Sunday, March 2, 2008 12:37 PM

Answers

  • There is no such property in a RichTextBox (nor in any other control I know of), but you can fake it.

     

    Place the RichTextBox inside a GroupBox and fit it in neatly so it just leaves the left edge of the group box showing.  Change the BackColor of the GroupBox to white and delete the text.  Then change the BorderStyle of the RichTextBox to None.  Now you have a simulated or pseudo left margin.

     

    Sunday, March 2, 2008 9:48 PM
  • Hi,

    Try the following code snippet. I agree with Solitaire. You need to fake it.

    Public Class Form1
        Dim lb As RichTextBox
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            RichTextBox1.BackColor = Color.White
            lb = New RichTextBox
            lb.Enabled = True
            RichTextBox1.Controls.Add(lb)
            lb.Dock = DockStyle.Right
            lb.BorderStyle = BorderStyle.None
            lb.Width = RichTextBox1.Width - 10
            lb.AutoSize = False
            lb.Text = String.Empty
        End Sub
        Private Sub Form1_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
            lb.Focus()
            Timer1.Enabled = True
            Timer1.Interval = 100
        End Sub
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            If (RichTextBox1.Focused) Then
                lb.Focus()
            End If
        End Sub
    End Class

    Best regards,
    Riquel
    Wednesday, March 5, 2008 1:36 PM
    Moderator

All replies

  • There is no such property in a RichTextBox (nor in any other control I know of), but you can fake it.

     

    Place the RichTextBox inside a GroupBox and fit it in neatly so it just leaves the left edge of the group box showing.  Change the BackColor of the GroupBox to white and delete the text.  Then change the BorderStyle of the RichTextBox to None.  Now you have a simulated or pseudo left margin.

     

    Sunday, March 2, 2008 9:48 PM

  • That'd solve the cosmetics of the problem, but I was looking to get away from the occasional problem of accidentally selecting a whole line when trying to just click myself into the left end of the line. And, someone could end up clicking away on the edge of the groupbox with no clue that it was another control.

    Still, you made me put my thinking cap back on. Maybe making the area left of the rtb mouse-sensitive and doing a bit of math to see what line is closest to the point at which the groupbox was clicked. The effect would be the same as clicking inside the rtb, except that users might notice the cursor jump to the right a couple of pixels.

    Tedious, but possible.

    Sunday, March 2, 2008 11:31 PM
  • Hi,

    Try the following code snippet. I agree with Solitaire. You need to fake it.

    Public Class Form1
        Dim lb As RichTextBox
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            RichTextBox1.BackColor = Color.White
            lb = New RichTextBox
            lb.Enabled = True
            RichTextBox1.Controls.Add(lb)
            lb.Dock = DockStyle.Right
            lb.BorderStyle = BorderStyle.None
            lb.Width = RichTextBox1.Width - 10
            lb.AutoSize = False
            lb.Text = String.Empty
        End Sub
        Private Sub Form1_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
            lb.Focus()
            Timer1.Enabled = True
            Timer1.Interval = 100
        End Sub
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            If (RichTextBox1.Focused) Then
                lb.Focus()
            End If
        End Sub
    End Class

    Best regards,
    Riquel
    Wednesday, March 5, 2008 1:36 PM
    Moderator