Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WPF] RichTextArea Selection not working correctly #1234

Closed
DanWBR opened this issue Oct 4, 2018 · 7 comments
Closed

[WPF] RichTextArea Selection not working correctly #1234

DanWBR opened this issue Oct 4, 2018 · 7 comments
Milestone

Comments

@DanWBR
Copy link
Contributor

DanWBR commented Oct 4, 2018

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

@ManuelHu
Copy link
Contributor

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...

@cwensley
Copy link
Member

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!

@cwensley cwensley added this to the 2.x milestone Oct 16, 2018
@cwensley
Copy link
Member

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.

@cwensley cwensley modified the milestones: 2.x, 2.5 Nov 15, 2018
@DanWBR
Copy link
Contributor Author

DanWBR commented Nov 16, 2018

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

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

@cwensley
Copy link
Member

cwensley commented Dec 5, 2018

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

@DanWBR
Copy link
Contributor Author

DanWBR commented Dec 5, 2018

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.

@cwensley
Copy link
Member

cwensley commented Dec 5, 2018

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants