How to bring Inline from a RichTextBox Child into View

c#, focus, richtextbox, wpf, xaml

Solution

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 { }));
    }

Problem

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?

Original source

Related problems