3

I would like to know if it is possible to show the line numbers (in a separate column) of a RichEditBox in UWP C#, or if there are other ways to get it. I'm looking for a solution to this problem... And it seems odd to me that there is no documentation about it: i need only a simple text editor. There are a lot of applications for Windows 10 that implement it and I refuse to think that it is not possible.

enter image description here

This is just an example from CodeWriter, a code editor application. Any idea?

  • *"I'm looking for a solution to this problem."* - Which one? You have asked about 2 distinct features: Displaying line numbers and retrieving line numbers (from content, presumably). – IInspectable May 22 '17 at 10:58
  • I would like to have a column on the side of the richeditbox that displays line numbers. The fact of having to get line numbers can be related or not depending on the implementation. The are old and inefficient solutions that use a stackpanel with content updated during text editing. – GiacomoFrisoni May 22 '17 at 11:35
  • Can't do this... I tried solutions with a ListView, but it seems so ugly that I deleted that project and repo I made for it. Really nobody has idea how to manage LINE NUMBERS with a RichEditBox (not RichTextBox!)? – Martinocom Jul 15 '17 at 09:45

1 Answers1

0

I achieve a "result" thinking about a list and the RichEditBox. Now, the solution isn't good, after about 50 lines typing it's laggy as hell, but at least I tried, 'cos the question is important also for me.

So, I designed a ListView and RichEditBox in a Grid with two columns

<ScrollViewer>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>

        <ListView Grid.Column="0" Name="LineNumbers" ScrollViewer.VerticalScrollMode="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled"></ListView>
        <RichEditBox Grid.Column="1" x:Name="RebText" TextChanged="RebText_TextChanged" ScrollViewer.VerticalScrollMode="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled"></RichEditBox>
    </Grid>
</ScrollViewer>

At the code behind I added this one:

private void RebText_TextChanged(object sender, RoutedEventArgs e)
{
    //Clear line numbers
    LineNumbers.Items.Clear();
    int i = 1;

    //Get all the thext
    ITextRange text = RebText.Document.GetRange(0, TextConstants.MaxUnitCount);
    string s = text.Text;

    if (s != "\r")
    {
        //Replace return char with some char that will be never used (I hope...)
        string[] tmp = s.Replace("\r", "§").Split('§');
        foreach (string st in tmp)
        {
            //String, adding new line
            if (st != "")
            {
                LineNumbers.Items.Add(i++);
            }
            //No string, empty space
            else
            {
                LineNumbers.Items.Add("");
            }                      
        }
    }
}

I think the .clear() method and readding all the lines every time is n't a good practice. But if you need a quick solution on max 50 lines, this MAYBE is the way.

Martinocom
  • 150
  • 1
  • 12