Need help with scrolling to a percentage of richtextbox's maximum scroll amount (the richtextbox is in a scrollviewer)

I am trying to make it so that a user can ctrl-click outside of a richtextbox to scroll to a percentage of the richtextbox's maximum scroll amount based on the y.position of the mouse relative to the top of the richtextbox. Here's the code I'm currently using:

    private void MainWindow_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        if (Keyboard.Modifiers == ModifierKeys.Control)
        {
            if (!richTextBox1.IsMouseOver)
            {
                double d = (e.GetPosition(richTextBox1).Y / richTextBox1.ActualHeight);
                if (d > 1) { d = 1; }
                d = (richtextboxScrollViewer.ExtentHeight * d);
                richtextboxScrollViewer.ScrollToVerticalOffset(d);
            }
        }
    }

Right now it seems to work until I scroll to the bottom and then I have to click past halfway up the richtextbox in order to make it scroll upwards. What am I doing wrong?

Answers


Change the first computation to:

double d = (e.GetPosition(richtextboxScrollViewer).Y / richtextboxScrollViewer.ViewportHeight);

The result of GetPosition on the rich text box takes into account its full height, including the "invisible" (scrolled out) part. So the percentage computation must be done according to the "physical" height of the scroll viewer.


Need Your Help

Http live streaming to iphone

iphone objective-c ios http http-live-streaming

I'm trying to play an http live stream to iphone, it looks like i have looked every example, mistakes and everything that i could found on the internet and apple docs about http live stream, and i ...

No options for autoresizing window in size inspector (XCode 4.5.2)

iphone ios xcode ipad

in XCode 4.5.2, when I created a new XIB file, I don't get an options to use the auto-resizing window in the size inspector (on the right hand side). But if I use old Xib files, that was created in

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.