4

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?

Karl_Schuhmann
  • 1,272
  • 5
  • 17
  • 37

2 Answers2

5

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 { }));
    }
Community
  • 1
  • 1
H H
  • 263,252
  • 30
  • 330
  • 514
  • I had the same problem with BringIntoView(). For some reason, using tactics from the second answer works without DoEvents() and BringIntoView(). Replace them with code like this: `Rect rctStart = selected.ContentStart.GetCharacterRect(System.Windows.Documents.LogicalDirection.Forward); Rect rctEnd = selected.ContentEnd.GetCharacterRect(System.Windows.Documents.LogicalDirection.Forward); richTextBox1.ScrollToVerticalOffset((rctStart.Top + rctEnd.Bottom - richTextBox1.ViewportHeight) / 2 + richTextBox1.VerticalOffset);` – Winter Dragoness Jan 09 '16 at 06:16
3

you should try one of this methods

richTextBox.SelectionStart = richTextBox.Text.Length;
richTextBox.ScrollToCaret();

.

richTextBox.AppendText(text);  
richTextBox.ScrollToEnd();

futher informations are here and here

Edit

ok after a bit of digging in the WPF RichTextBox i thing you cloud try richTextBox.ScrollToVerticalOffset(Offset) to get the Offset maybe you could use this answer

EDIT 2

ok after some more research i found following Link where you can download this working example

Community
  • 1
  • 1
WiiMaxx
  • 5,322
  • 8
  • 51
  • 89
  • the first method is in windows forms useful. The second let me scroll to the end of the richtextbox and not to the, which i have add – Karl_Schuhmann Mar 18 '13 at 08:42
  • 1
    to calculate the Offset see the following [LINK](http://stackoverflow.com/a/3487361/1993545) – WiiMaxx Mar 18 '13 at 12:31