locked
Format indent of each individual word in a string in a richtextbox in Visual Studio 2015 RRS feed

  • Question

  • How can I format a richtextbox so that the first word in a line starts 2 spaces in from the left side of the richtextbox, the second word starts 20 spaces in from the left, the third word starts 40 spaces in from the left and so on? Having the spaces between the words be the same doesn't work because I get:

    Line1..1000    3000    4000

    Line2..100    30    400

    What I need is:

    Line1..1000    3000    4000

    Line2..100      30       400

    I am adding an entire line of 12 words/numbers with one button click.

    Thank you.


    • Edited by jcourier1 Saturday, May 4, 2019 3:44 PM
    Saturday, May 4, 2019 3:40 PM

Answers

  • If you have time, try the next example too:

    rtbJobLog.Text =
        vbTab & "Day 1:" & vbCrLf &
        vbTab & "Tue 5/7/2019" & vbTab & "2015" & vbTab & "Highlander" & vbTab & "Driver 1" & vbCrLf &
        vbCrLf &
        vbTab & "Day 2:" & vbCrLf &
        vbTab & "Tue 5/7/2019" & vbTab & "2015" & vbTab & "Highlander" & vbTab & "Driver 1" & vbCrLf &
        vbTab & "Wed 5/8/2019" & vbTab & "2015" & vbTab & "Ford" & vbTab & "Driver 2" & vbCrLf
    
    rtbJobLog.SelectAll()
    rtbJobLog.SelectionTabs = {20, 200, 400, 600}
    rtbJobLog.SelectionLength = 0
    

    • Marked as answer by jcourier1 Tuesday, May 7, 2019 6:39 PM
    Tuesday, May 7, 2019 5:35 PM

All replies

  • How can I format a richtextbox so that the first word in a line starts 2 spaces in from the left side of the richtextbox, the second word starts 20 spaces in from the left, the third word starts 40 spaces in from the left and so on? Having the spaces between the words be the same doesn't work because I get:

    Line1..1000    3000    4000

    Line2..100    30    400

    What I need is:

    Line1..1000    3000    4000

    Line2..100      30       400

    I am adding an entire line of 12 words/numbers with one button click.

    Thank you.


    Hi

    Try this out:

    ' Form1 with RichTextBox1
    Option Strict On
    Option Explicit On
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim s1 As String = "1000 3000 4000"
            Dim s2 As String = "100 30 400"
    
            Dim s3() As String = s1.Split(" "c)
            Dim s4() As String = s2.Split(" "c)
    
            PadS(s3)
            PadS(s4)
        End Sub
        Sub PadS(s() As String)
            RichTextBox1.AppendText(Space(2))
            For Each st As String In s
                RichTextBox1.AppendText(st.PadRight(6) & vbTab)
            Next
            RichTextBox1.AppendText(vbCrLf)
        End Sub
    End Class


    Regards Les, Livingston, Scotland

    Saturday, May 4, 2019 4:08 PM
  • Check this example too:

    Dim text = vbTab & "Line1" & vbTab & 1000 & vbTab & 3000 & vbTab & 4000 & vbCrLf &
        vbTab & "Line2" & vbTab & 100 & vbTab & 30 & vbTab & 400 & vbCrLf
    
    RichTextBox1.Text = text
    
    RichTextBox1.SelectAll()
    RichTextBox1.SelectionTabs = {20, 200, 400, 600}
    RichTextBox1.SelectionLength = 0
    

    Adjust the tabs according to font size etc.

    Saturday, May 4, 2019 5:09 PM
  • Thank you for the help.
    Saturday, May 4, 2019 11:45 PM
  • Thank you for the help.
    Saturday, May 4, 2019 11:45 PM
  • Hi,

    Is your problem solved? If so, please mark the useful replies as answers.  

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, May 6, 2019 1:19 AM
  • Alex, I haven't been able to get the result I need yet. I'll try to be clearer in what I am trying to accomplish. Every day I input text into  Comboboxes and Textboxes, the input varies from day to day. When I click on my "'Save" button the text in all the Comboboxes and Textboxes is transfered to a Richtextbox I named rtbJobLog. This is the (simplified) code I use to do that:

     rtbJobLog.Text = rtbJobLog.Text & vbNewLine & txtDate.Text & Space(TabSpace) & cbVeh.Text & Space(TabSpace) & cbDri.Text 

    The problem is as the textlength in the Comboboxes and Textboxes varies from day to day when I save to the rtbJobLog, the info in the lines are staggered. Examples below:

    Day 1:

    Tue 5/7/2019     2015 Highlander       Driver 1

    Day 2:

    Tue 5/7/2019     2015 Highlander       Driver 1

    Wed 5/8/2019     2015 Ford       Driver 2

    The result I need is:

    Tue 5/7/2019     2015 Highlander       Driver 1

    Wed 5/8/2019    2015 Ford                Driver 2

    Thanks

    Tuesday, May 7, 2019 3:56 PM
  • Hi

    Suggest you use a mono spaced font - there are a few available by default.

    One of the issues may be that you are using a proportionally space font where the characters have different 'widths' thus causing the misaligned results.


    Regards Les, Livingston, Scotland

    Tuesday, May 7, 2019 4:02 PM
  • I think the problem is more that there is a big difference in the length of the text in each textbox and combobox with every days input. Using a Tab space between the texts doesn't work because the Tab sets the number of spaces from the end of one text to the beginning of the next text. I need to format how many spaces each text begins from the left side of the richtextbox, not from the preceding text.
    Tuesday, May 7, 2019 4:37 PM
  • I think the problem is more that there is a big difference in the length of the text in each textbox and combobox with every days input. Using a Tab space between the texts doesn't work because the Tab sets the number of spaces from the end of one text to the beginning of the next text. I need to format how many spaces each text begins from the left side of the richtextbox, not from the preceding text.

    Hi

    All spacing is effected by the font in use. Did you try my suggestion at all?


    Regards Les, Livingston, Scotland


    • Edited by leshay Tuesday, May 7, 2019 4:48 PM
    Tuesday, May 7, 2019 4:47 PM
  • Yes, I did, this is the result:

     Tue 5/7/19    2015 Highlander    Mary    Duke Cinti Rte    Mar Wal    Mer Wes    300    600    300    
    Wed 5/8/19    2013 Ford    Mary    139    Mar Wal    Mer Wes    300    600    300   

    I need:

     Tue 5/7/19    2015 Highlander    Mary    Duke Cinti Rte    Mar Wal    Mer Wes    300    600     Wed 5/8/19  2013 Ford              Mary    139                   Mar Wal    Mer Wes    300    600     


    • Edited by jcourier1 Tuesday, May 7, 2019 5:30 PM
    Tuesday, May 7, 2019 5:29 PM
  • If you have time, try the next example too:

    rtbJobLog.Text =
        vbTab & "Day 1:" & vbCrLf &
        vbTab & "Tue 5/7/2019" & vbTab & "2015" & vbTab & "Highlander" & vbTab & "Driver 1" & vbCrLf &
        vbCrLf &
        vbTab & "Day 2:" & vbCrLf &
        vbTab & "Tue 5/7/2019" & vbTab & "2015" & vbTab & "Highlander" & vbTab & "Driver 1" & vbCrLf &
        vbTab & "Wed 5/8/2019" & vbTab & "2015" & vbTab & "Ford" & vbTab & "Driver 2" & vbCrLf
    
    rtbJobLog.SelectAll()
    rtbJobLog.SelectionTabs = {20, 200, 400, 600}
    rtbJobLog.SelectionLength = 0
    

    • Marked as answer by jcourier1 Tuesday, May 7, 2019 6:39 PM
    Tuesday, May 7, 2019 5:35 PM
  • That works! Thank you!
    Tuesday, May 7, 2019 6:40 PM