1

I'm currently making a typing program. I have a text file being read into a richtext box. This text is what needs to be typed but the richtextbox can only fit so many lines and I want to be able to scroll to the next line to show the rest of the texts once the previous lines have already been attempted.

I've tried textbox.ScrollToCaret() but all it does it makes the text box flicker, as in scrolling up and down.

I do record the index but the ways I have tried has not worked. Hacky ways such as (to mainly test it out):

if(index > 300) 
  textbox.ScrollToCaret();

300 Being the current maximum characters visible on the text box. Is there any way to scroll to show the rest of the lines on the text box? I'm happy to provide more information if needed.

            wordPreview.BeginUpdate();                                  
            wordPreview.SelectionStart = wordPreview.TextLength;
            wordPreview.ScrollToCaret();
            wordPreview.EndUpdate();
Jed5931
  • 265
  • 1
  • 2
  • 8
  • Can you try the ScrollToCaret() on a text changed event - like this: http://stackoverflow.com/questions/9416608/rich-text-box-scroll-to-the-bottom-when-new-data-is-written-to-it – David Oesterreich Apr 05 '16 at 18:42
  • @DavidOesterreich That's the one I attempted but I changed the selection start to 0 as I can't access the index in TextChanged and setting it to the Text Length only automatically scrolls to the end when started. It mainly flickers as if it's trying to scroll when it does not need to yet and when it needs to scroll, it doesn't. – Jed5931 Apr 05 '16 at 18:46
  • - Edit: Apologies, just tested it again. TextLength automatically shows the end of the lines but when there's input it keeps flickering from the last lines to the first lines. – Jed5931 Apr 05 '16 at 18:51
  • Need to force the richtextbox from redrawing everytime text is updated. There should be few ways to do this: http://weblogs.asp.net/jdanforth/88458 or http://stackoverflow.com/questions/192413/how-do-you-prevent-a-richtextbox-from-refreshing-its-display – David Oesterreich Apr 05 '16 at 19:06

2 Answers2

1

To try to get rid of the flickering of the richtextbox, you can extend the RichTextBox class and add BeginUpdate and EndUpdate methods

Extension Class:

 public static class MyExtensions
    {


        private const int WM_USER = 0x0400;
        private const int EM_SETEVENTMASK = (WM_USER + 69);
        private const int WM_SETREDRAW = 0x0b;
        private static IntPtr OldEventMask;

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

        public static void BeginUpdate(this RichTextBox rtb)
        {
            SendMessage(rtb.Handle, WM_SETREDRAW, IntPtr.Zero, IntPtr.Zero);
            OldEventMask = (IntPtr)SendMessage(rtb.Handle, EM_SETEVENTMASK, IntPtr.Zero, IntPtr.Zero);
        }

        public static void EndUpdate(this RichTextBox rtb)
        {
            SendMessage(rtb.Handle, WM_SETREDRAW, (IntPtr)1, IntPtr.Zero);
            SendMessage(rtb.Handle, EM_SETEVENTMASK, IntPtr.Zero, OldEventMask);
        }       
    }

Then in your text changed event you can call BeginUpdate and EndUpdate when every the richtextbox is being updated or scrolling.

    richTextBox1.BeginUpdate();

    richTextBox1.EndUpdate();
  • I've added the class and in my TextChanged event I have (edited the post above) and it still flickers. – Jed5931 Apr 06 '16 at 09:11
  • it still scrolls up and down and still can't find a fix? – Jed5931 Apr 06 '16 at 16:21
  • +1 thanks. Worked well for TextBox (haven't tried with RichEdit). That's almost exactly what Delphi does under the hood of its BeginUpdate/EndUpdate routines. Might be just fine to change the extension method to accept any .NET Control as this mechanism is invaluable in other situations and should work for TabControl, Panel and what not. – TByte Dec 16 '20 at 15:20
  • Although the static OldEventMask opens up to a risk of getting/setting the value for the wrong control (if nested calls to BeginUpdate and EndUpdate occur), so if this OldEventMask is really necessary (as I've not looked too deeply as to why it's here), inheriting from RichEdit and implementing this functionality in the descendant is probably a better way to do it. As for TextBox, EM_SETEVENTMASK doesn't apply, so can remain as an extension. – TByte Dec 16 '20 at 15:26
0

Use TextBlock Control instead of textbox control and use TextWrapping property of TextBlock.

<TextBlock TextWrapping="Wrap"/> 
abhi312
  • 364
  • 1
  • 6
  • 25