Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bring Inline from a RichTextBox Child into View

How can i focus a Inline in a RichTextBox?
I Create a FlowDocument from a Text-File and load it in my richTextBox1 and mark one Inline after an other accordingly to a Button_click (be recreating the FlowDocument)

with this code:

            richTextBox1.SelectAll();
            richTextBox1.Selection.Text = "";

            string text = System.IO.File.ReadAllText(file);
            int iZeile = 0;

            string[] split = text.Split(new string[] {"\r\n"},StringSplitOptions.None);

                    foreach (string s in split)
                    {
                        if (iZeile != 27)
                        {
                            paragraph.Inlines.Add(s + "\r\n"); // adds line added without marking
                        }
                        else
                        {
                            Run run = new Run(split[27]); // adds line with marking
                            run.Background = Brushes.Yellow;
                            paragraph.Inlines.Add(run);
                            paragraph.Inlines.Add("\r\n");
                        }
                        iZeile++;
                    }

            FlowDocument document = new FlowDocument(paragraph);
            richTextBox1.Document = new FlowDocument();
            richTextBox1.Document = document;
            Keyboard.Focus(richTextBox1);
        }

I know its not.. perfect.

the Issue

It works so far but the problem that occurs is me Market Inline doesn't comes intoView. Is there a easy way to bring this Inline intoView?

like image 250
Karl_Schuhmann Avatar asked Feb 17 '23 04:02

Karl_Schuhmann


1 Answers

The straightforward solution seemed to be FrameworkContentElement.BringIntoView() but after putting it in the code below it initially had no effect. As it turns out this is one of these timing issues (I've seen similar problems in WinForms) that can be solved by processing the outstanding Windows Messages. WPF has no direct equivalent of DoEvents() but there exists a well known substitute.

I placed this in a ButtonClick, changes marked with //**:

        Paragraph paragraph = new Paragraph();
        Inline selected = null;   //**

        richTextBox1.SelectAll();
        richTextBox1.Selection.Text = "";

        string text = System.IO.File.ReadAllText(@"..\..\MainWindow.xaml.cs");
        int iZeile = 0;

        string[] split = text.Split(new string[] { "\r\n" }, StringSplitOptions.None);

        foreach (string s in split)
        {
            if (iZeile != 27)
            {
                paragraph.Inlines.Add(s + "\r\n"); // adds line added without marking
            }
            else
            {
                Run run = new Run(split[27]); // adds line with marking
                run.Background = Brushes.Yellow;
                paragraph.Inlines.Add(run);
                paragraph.Inlines.Add("\r\n");
                selected = run;                // ** remember this element
            }
            iZeile++;
        }

        FlowDocument document = new FlowDocument(paragraph);
        richTextBox1.Document = new FlowDocument();
        richTextBox1.Document = document;
        Keyboard.Focus(richTextBox1);

        DoEvents();                   // ** this is required, probably a bug
        selected.BringIntoView();     // ** 

And the helper method, from here:

    public static void DoEvents()
    {
        Application.Current.Dispatcher.Invoke(
            System.Windows.Threading.DispatcherPriority.Background, 
            new Action(delegate { }));
    }
like image 73
Henk Holterman Avatar answered Mar 03 '23 19:03

Henk Holterman