Eto: [WPF] RichTextArea Selection not working correctly

Created on 4 Oct 2018  ·  7Comments  ·  Source: picoe/Eto

Expected Behavior

Text correctly selected according to defined range.

Actual Behavior

There is a misplacement of 5 characters to the right.

Steps to Reproduce the Problem

  1. Create a RichTextArea control
  2. Input some text
  3. Define a selection to set its color

Code that Demonstrates the Problem

rtfcontrol.Appen(initialtext, true);
rtfcontrol.Append(sometext, true);
rtfcontrol.Selection = new Range<int>(rtfcontrol.Text.Length - sometext.Length, rtfcontrol.Text.Length -1);
rtfcontrol.SelectionForeground = Colors.Blue;
rtfcontrol.Append("\n", true);
rtfcontrol.Selection = new Range<int>(rtfcontrol.Text.Length);

Specifications

  • Version: 2.5.0-ci-10013
  • Platform(s): WPF
  • Operating System(s): Windows 10

WPF:

wpf

GTK2 (Linux):

gtk2_linux

Xamarin.Mac:

xamarin mac

Most helpful comment

@DanWBR, ah yes that won't work the way you have it unfortunately - WPF adds a newline at the end of the Text property (regardless if your text has it or not). I have not found a way around that at this point, but it is a separate issue from this one.

What I'd recommend is to keep the current position in a variable and increment it after appending text. Accessing the Text property each time just to get the length will slow down as the text gets larger.

All 7 comments

On Windows, the line ending characters are somehow not counted to the indices for selections. I currently use the following code as work-around:

private void Write(string message, Color c)
        {
            Append(message + Environment.NewLine, true);

            int idx = -1, last = -1;
            if (Environment.OSVersion.Platform == PlatformID.Unix)
            {
                idx = Text.LastIndexOf(message);
                last = Text.Length;
            }
            else
            {
                var lines = string.Join("", Text.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries));
                idx = lines.LastIndexOf(message);
                last = lines.Length;
            }

            if (idx == -1)
                return;

            Selection = new Range<int>(idx, idx + message.Length);
            SelectionForeground = c;
            Selection = new Range<int>(last, last);
        }

But this is definitely a bug...

Yeah I've used the same hack as @ManuelHu before, it isn't pretty. I'm not sure yet how to effectively address this without being dog slow with large text.

Thanks for reporting the issue!

Hey @DanWBR,

This should be fixed in latest develop branch. Could you give it a go? On all platforms newlines are now treated as a single \n character.

Hi @cwensley, there's still one character left to fix:

captura de tela 2018-11-16 as 08 06 39

Hey @DanWBR, do you have a repro for the off by one issue? I can't seem to reproduce it.

https://github.com/DanWBR/dwsim5/blob/windows/DWSIM.UI.Desktop.Forms/Forms/Flowsheet/Flowsheet.eto.cs#L1245

Maybe I'm using wrong indexes? It does work on macOS and GTK, though.

@DanWBR, ah yes that won't work the way you have it unfortunately - WPF adds a newline at the end of the Text property (regardless if your text has it or not). I have not found a way around that at this point, but it is a separate issue from this one.

What I'd recommend is to keep the current position in a variable and increment it after appending text. Accessing the Text property each time just to get the length will slow down as the text gets larger.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

LaraSQP picture LaraSQP  ·  7Comments

voronoipotato picture voronoipotato  ·  16Comments

azunyuuuuuuu picture azunyuuuuuuu  ·  23Comments

Krakean picture Krakean  ·  6Comments

ManuelHu picture ManuelHu  ·  4Comments