WPF: How To append text in Richtextbox in column/ tab separed.

TheDoublen 41 Reputation points
2022-02-07T16:06:33.41+00:00

Hello,

In my WPF application I would like to use a Richtextbox for appearing data in column style ( like tab separated but not a real table element).

I search a lot, but I did not find the way how to append text continuously: new string data tab separated from last text on the Richtextbox. And the distance between first and second 'column' is equal.

Here is a example that I found, this almost that I want, just I would like it in WPF version.
171986-howto-richtextbox-tabs.png

Can you help me and provide a little example how I need to do this?
Yes, I not added code part because I don't know how to do it. So, there is no part of code which not work; simple: I don't have :(

Thanks for the help.

Developer technologies | Windows Presentation Foundation
0 comments No comments
{count} votes

Answer accepted by question author
  1. Hui Liu-MSFT 48,711 Reputation points Microsoft External Staff
    2022-02-08T05:45:03.613+00:00

    Viorel-1's answer is great. If it's just a simple string arrangement, you could set the distance between strings by adding \t as required.
    xaml:

     <RichTextBox Name="rtb" Width="400" Height="300" HorizontalAlignment="Left"/>  
    

    xaml.cs:

    string txt ="Breakfast\tLunch\t\tDinner\n" +  
           "Coffee\t\tSoda\t\tWine\n" +  
           "Bagel\t\tSandwich\tSalad\n" +  
           "Fruit\t\tChips\t\tTofuburger\n" +  
                "\t\tCookie\t\tVeggies";  
    
          rtb.AppendText(txt);  
    

    The result:
    172084-image.png


    If the response is helpful, please click "Accept Answer" and upvote it.
     Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread.
     

    [5]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Viorel 125.7K Reputation points
    2022-02-07T17:30:25.243+00:00

    Try adding a <Table> element to your textbox:

    <RichTextBox>
        <FlowDocument>
            <Table>
                <Table.Columns>
                    <TableColumn />
                    <TableColumn />
                    <TableColumn />
                </Table.Columns>
                <TableRowGroup x:Name="myRows"/>
            </Table>
        </FlowDocument>
    </RichTextBox>
    

    To insert a new row programmatically:

    var r = new TableRow();
    
    r.Cells.Add( new TableCell( new Paragraph( new Run( "Breakfast" ) ) ) );
    r.Cells.Add( new TableCell( new Paragraph( new Run( "Lunch" ) ) ) );
    r.Cells.Add( new TableCell( new Paragraph( new Run( "Dinner" ) ) ) );
    
    myRows.Rows.Add( r );
    

    Use <TableColumn Width="..."/> to set the width of the columns.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.