0
public MainPage()
        {
            this.InitializeComponent();
            box1 = new RichEditBox()
            {
                Width = 500,
                Height = 500,
                BorderBrush = new SolidColorBrush(Colors.Blue),
                BorderThickness = new Thickness(1, 1, 1, 1)
            };
            btn = new Button() { Content = "button" };
            btn.Click += Btn_Click;
            string str = "ppppppavaniiiiipavanlllllpavankjhgfcdsa";
            box1.Document.SetText(0, str);
            box1.TextAlignment = TextAlignment.Center;

            {
                ITextSelection selection1 = box1.Document.Selection;

                selection1.StartPosition = 5;
                selection1.EndPosition = 10;
                Debug.WriteLine("<<<<<<<<<<<<<<<<<<<<<" + selection1.StartPosition);
                Debug.WriteLine("<<<<<<<<<<<<<<<<<<<<<" + selection1.EndPosition);

                selection1.CharacterFormat.Bold = FormatEffect.On;

                string s = "\"google.com\"";
                selection1.Link = s;

                selection1.Collapse(false);
            }
            {
                ITextSelection selection2 = box1.Document.Selection;

                selection2.StartPosition = 25;
                selection2.EndPosition = 30;
                Debug.WriteLine("<<<<<<<<<<<<<<<<<<<<<" + selection2.StartPosition);
                Debug.WriteLine("<<<<<<<<<<<<<<<<<<<<<" + selection2.EndPosition);

                selection2.CharacterFormat.Bold = FormatEffect.On;
                selection2.CharacterFormat.Size = 20;
                selection2.CharacterFormat.BackgroundColor = Color.FromArgb(0, 50, 77, 98);
                selection2.CharacterFormat.Name = "Algerian";

                string s = "\"gjhgfdghje.com\"";
                selection2.Link = s;
            }

            grid.Children.Add(box1);
            grid.Children.Add(btn);
        }

After setting the Link for the range 5 to 10 before setting the Link and RichText for the range 25 to 30 which some how getting applied for the range 5 to 10 instead of 25 to 30. here is the output enter image description here

If I move the second block of code above the first as below

        public MainPage()
        {
            this.InitializeComponent();
            box1 = new RichEditBox()
            {
                Width = 500,
                Height = 500,
                BorderBrush = new SolidColorBrush(Colors.Blue),
                BorderThickness = new Thickness(1, 1, 1, 1)
            };
            btn = new Button() { Content = "button" };
            btn.Click += Btn_Click;
            string str = "ppppppavaniiiiipavanlllllpavankjhgfcdsa";
            box1.Document.SetText(0, str);
            box1.TextAlignment = TextAlignment.Center;

            {
                ITextSelection selection2 = box1.Document.Selection;

                selection2.StartPosition = 25;
                selection2.EndPosition = 30;
                Debug.WriteLine("<<<<<<<<<<<<<<<<<<<<<" + selection2.StartPosition);
                Debug.WriteLine("<<<<<<<<<<<<<<<<<<<<<" + selection2.EndPosition);

                selection2.CharacterFormat.Bold = FormatEffect.On;
                selection2.CharacterFormat.Size = 20;
                selection2.CharacterFormat.BackgroundColor = Color.FromArgb(0, 50, 77, 98);
                selection2.CharacterFormat.Name = "Algerian";

                string s = "\"gjhgfdghje.com\"";
                selection2.Link = s;
            }
            {
                ITextSelection selection1 = box1.Document.Selection;

                selection1.StartPosition = 5;
                selection1.EndPosition = 10;
                Debug.WriteLine("<<<<<<<<<<<<<<<<<<<<<" + selection1.StartPosition);
                Debug.WriteLine("<<<<<<<<<<<<<<<<<<<<<" + selection1.EndPosition);

                selection1.CharacterFormat.Bold = FormatEffect.On;

                string s = "\"google.com\"";
                selection1.Link = s;

                selection1.Collapse(false);
            }
            grid.Children.Add(box1);
            grid.Children.Add(btn);
        }

then the output is this enter image description here

why is this behavior is like this.

3
  • 1
    After setting the ITextRange.Link Property, ITextRange.EndPosition will go to the end of the Text. This behavior has not been explained in the official document. According to the current behavior, please set the link in the order from the back to the front. Commented May 11, 2023 at 7:33
  • @JunjieZhu-MSFT Then if we want to set the Link in runtime for the larger ranges after setting the Link for the lower ranges, it is not possible! Commented May 12, 2023 at 5:36
  • @JunjieZhu-MSFT And how to remove the Link which has been set, how to extend or compress the range of the Link which has been set Commented May 12, 2023 at 6:03

1 Answer 1

1

i finally figured it out how RichEditBox is working while having links, After applying link for this range

{
                ITextSelection selection1 = box1.Document.Selection;
                selection1.StartPosition = 5;
                selection1.EndPosition = 10;
                string s = "\"google.com\"";
                selection1.Link = s;
}

if we see the text of RichEditBox using box1.TextDocument.GetText(0, out txt); then it is in this format pppppHYPERLINK "google.com"pavaniiiiipavanlllllpavankjhgfcdsa,here the fixed characters are HYPERLINK total 10 characters with space at the end, and we can get the link length from the

int prevlinklength;
{
                ITextSelection selection1 = box1.Document.Selection;
                selection1.StartPosition = 5;
                selection1.EndPosition = 10;
                prevlinklength = selection1.Link.length;
}

//now if we want to apply the link for the 25 to 30 range means 
{
                ITextSelection selection1 = box1.Document.Selection;
                selection1.StartPosition = 25 + 10 + prevlinklength;
                selection1.EndPosition = 30 + 10 + prevlinklength;
                string s = "\"google.com\"";
                selection1.Link = s;
}

now final text will be in this format

pppppHYPERLINK "google.com"pavaniiiiipavanlllllHYPERLINK "google.com"pavankjhgfcdsa
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.