Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8733507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:39:44+00:00 2026-06-13T09:39:44+00:00

I’m making a textbox to autoscroll to the end when text is added. However

  • 0

I’m making a textbox to autoscroll to the end when text is added. However I wanted the option to not scroll the textbox when the mouse is over the textbox. I’ve done all that, but when the user selects text and the textbox receives an event to update the text, everything goes haywire.

Here’s what I’m working with:


<TextBox Text="{Binding ConsoleContents, Mode=OneWay}" TextWrapping="Wrap" 
    IsReadOnly="True" ScrollViewer.VerticalScrollBarVisibility="Visible" 
    TextChanged="TextBox_TextChanged" MouseEnter="TextBox_MouseEnterLeave"
    MouseLeave="TextBox_MouseEnterLeave" AllowDrop="False" Focusable="True" 
    IsUndoEnabled="False"></TextBox>

private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        TextBox textBox = sender as TextBox;
        if (textBox == null) return;

        // ensure we can scroll
        if (_canScroll)
        {
            textBox.Select(textBox.Text.Length, 0); //This was an attempt to fix the issue
            textBox.ScrollToEnd();
        }
    }

private void TextBox_MouseEnterLeave(object sender, MouseEventArgs e)
    {
        TextBox textBox = sender as TextBox;

        // Don't scroll if the mouse is in the box
        if (e.RoutedEvent.Name == "MouseEnter")
        {
            _canScroll = false;
        }
        else if (e.RoutedEvent.Name == "MouseLeave") 
        {
            _canScroll = true;
        }
    }

To further explain what haywire means, when the textbox receives and propertychanged event it sets the text and scrolls down to the end if the mouse is not hovering over it. If the mouse is hovering over it does not scroll. But if I select text and the textbox recives the propertychanged event the content gets updated, but the box does not scroll down. This is expected. The problem is that my selection then goes from where the cursor currently is to the top of the text. If I remove the cursor from the box, it continues fine but once the cursor returns, the box gets stuck at the top and cannot scroll down. I thought it might have been the cursor so I try to move it to the end but that solves nothing.

Any ideas?!

I’ve been ripping my hair out! Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:39:45+00:00Added an answer on June 13, 2026 at 9:39 am

    Well it took all morning but here’s how to do it:

    <TextBox Text="{Binding ConsoleContents, Mode=OneWay}" 
             TextWrapping="Wrap" IsReadOnly="True" ScrollViewer.VerticalScrollBarVisibility="Visible" TextChanged="TextBox_TextChanged" 
             MouseEnter="TextBox_MouseEnterLeave" MouseLeave="TextBox_MouseEnterLeave" SelectionChanged="TextBox_SelectionChanged" AllowDrop="False" Focusable="True" 
             IsUndoEnabled="False"></TextBox>
    

    public partial class ConsoleView : UserControl
    {
        private bool _canScroll;
    
        // saves
        private int _selectionStart;
        private int _selectionLength;
        private string _selectedText;
    
        public ConsoleView(ConsoleViewModel vm)
        {
            InitializeComponent();
            this.DataContext = vm;
    
            _canScroll = true;
        }
    
    
        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox textBox = sender as TextBox;
            if (textBox == null) return;
    
            // ensure we can scroll
            if (_canScroll)
            {
                // set the cursor to the end and scroll down
                textBox.Select(textBox.Text.Length, 0);
                textBox.ScrollToEnd();
    
                // save these so the box doesn't jump around if the user goes back in
                _selectionLength = textBox.SelectionLength;
                _selectionStart = textBox.SelectionStart;
            }
            else if (!_canScroll)
            {
    
                // move the cursor to where the mouse is if we're not selecting anything (for if we are selecting something the cursor has already moved to where it needs to be)
                if (string.IsNullOrEmpty(_selectedText))
                //if (textBox.SelectionLength > 0)
                {
                    textBox.CaretIndex = textBox.GetCharacterIndexFromPoint(Mouse.GetPosition(textBox), true);
                }
                else
                {
                    textBox.Select(_selectionStart, _selectionLength); // restore what was saved
                }
    
            }
        }
    
    
        private void TextBox_MouseEnterLeave(object sender, MouseEventArgs e)
        {
            TextBox textBox = sender as TextBox;
            if (textBox == null) return;
    
            // Don't scroll if the mouse is in the box
            if (e.RoutedEvent.Name == "MouseEnter")
            {
                _canScroll = false;
            }
            else if (e.RoutedEvent.Name == "MouseLeave")
            {
                _canScroll = true;
            }
    
        }
    
    
        private void TextBox_SelectionChanged(object sender, RoutedEventArgs e)
        {
            TextBox textBox = sender as TextBox;
            if (textBox == null) return;
    
            // save all of the things
            _selectionLength = textBox.SelectionLength;
            _selectionStart = textBox.SelectionStart;
            _selectedText = textBox.SelectedText; // save the selected text because it gets destroyed on text update before the TexChanged event.
    
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I need a function that will clean a strings' special characters. I do NOT
I have a reasonable size flat file database of text documents mostly saved in
I'm making a simple page using Google Maps API 3. My first. One marker
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a bunch of posts stored in text files formatted in yaml/textile (from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.