Text correctly selected according to defined range.
There is a misplacement of 5 characters to the right.
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);
WPF:
GTK2 (Linux):
Xamarin.Mac:
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:
Hey @DanWBR, do you have a repro for the off by one issue? I can't seem to reproduce it.
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.
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.