Click here to Skip to main content
13,051,803 members (47,923 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hello. In my application, i need to automatically scroll. not to caret, but to scroll from the top to the bottom in a smooth effect. same with bottom to top. I will leave all of the complicated stuff out that actually lets the function below work.

private void scrolldown_Click(object sender, EventArgs e)
            t.Enabled = false;
            if (t.Enabled == false)
                t.Enabled = true;
            else t.Enabled = true;
            while (t.Enabled == true)
                scroll(telep.Handle, (speedslider.Value * 2));

scroll(telep.Handle, (speedslider.Value * 2)); needs to be looped. i have tried while(true) but this causes the application do this infinitely many times. this is okay. but when the scroll bar reaches the very bottom of the text box, the app becomes unresponsive. so. what i am trying to do is stop the loop when the scrollbar reaches the very bottom. now, what im about to show you is only PART of the code that causes the above to work.

// Increase posion by pixles
                if (si.nPos < (si.nMax - si.nPage))
                    si.nPos += pixels; //<-- the + can be changed to - for up movement instead of down. si is the SCROLLBARINFO and nPos is its position.
                    ptrWparam = new IntPtr(SB_ENDSCROLL);
                    t.Enabled = false;
                    SendMessage(handle, WM_VSCROLL, ptrWparam, ptrLparam);
Posted 19-Feb-11 12:53pm
Updated 19-Feb-11 19:14pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

In addition to SA's response, you should not do a loop in your main thread. That will freeze up your UI. You need to do tasks like this in a background thread so your main UI will remain responsive. You can use BackgroundWorker for this (look it up on MSDN).
SAKryukov 20-Feb-11 0:03am
I trust did not mind arguably fast loops, which are quite find and usually unavoidable...
My 5 anyway,
Espen Harlinn 6-Apr-11 16:09pm
Good point :)
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Having done a number of controls over the years, I've always found that the best, and smoothest, way to handle the scrollbar operations is to just to move the contents already painted - if anything you've already painted will be visible after scrilling; and then invalidate the rest of the window.

In other words you have to keep track of what's visible and painted.

The native windows API provides a nice function:
    int dx,
    int dy,
    const RECT *lprcScroll,
    const RECT *lprcClip,
    HRGN hrgnUpdate,
    LPRECT lprcUpdate

That efficiently moves what you've already painted - Documented here[^]

I'd start out by just invalidating the window, and enhance the implementation to reuse what's painted when you've got that part working.

Espen Harlinn
SAKryukov 6-Apr-11 19:02pm
Agree. This is an important point, in fact. My 5.
Espen Harlinn 8-Apr-11 3:44am
Thanks, SAKryukov!
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Vlad, for a programmer, there is not such thing as "crashing". You should use debugger properly and, in order to post a correct Question for a CodeProject, learn how to dump exception information.

Spotting this particular problem based on your posted code is a bit tedious and boring. We could do little better. Let me help you a bit with System.Windows.Forms specific exception handling feature which many developers miss; so it will help you in future as well (and please forgive me if you already know that). Most likely, all your problem unfolds in you UI thread. This is what you have to do first.

In your entry point function (usually called Program.Main), add the following line (assuming "using System.Windows.Forms;"):

static void Main() {
} //Main

Add some handler of the special Application event fired on exception inside of main loop if Application.Run. This code should be called before you show your main form, as soon as possible, for example, in its constructor:

Application.ThreadException += delegate(
    object sender,
    System.Threading.ThreadExceptionEventArgs eventArgs) {
                "{0}: \n\n{1}",
            string.Format(" {0}: Error", Application.ProductName),
            MessageBoxButtons.OK, MessageBoxIcon.Error);
}; //Application.ThreadException

If your Framework version is 3.5 or later, lambda form is more convenient:

Application.ThreadException += (sender, eventArgs) => {
}; //Application.ThreadException

This will show all your exceptions in UI thread without termination of the application, you also can effectively use debugger break point in this function.

Additionally or alternatively, handle the events System.AppDomain.UnhandledException and System.AppDomain.FirstChanceException.
My credit to Espen Harlinn for the idea.

If it does not provide enough information, add detailed dump: exception stack (first thing to do) and possibly all inner exceptions, recursively. If you want help from CodeProject, save this dump in file and post its content in your Question (please, try to format nicely!).

If your problem is still not resolved, ask follow-up questions, but please not post it as an Answer (important!); use "Improve question" or "Add comment".

By the way, I did not understand why you use P/Invoke, that is, why pure .NET API was not enough?

Sandeep Mewara 19-Feb-11 23:55pm
Good answer, my 5!
Don't know who gave a 3... :doh:
SAKryukov 20-Feb-11 0:03am
Thank you, Sandeep.
These days somebody does is often. Who called them "univoters"?
MCY 20-Feb-11 5:15am
nice. 5!
SAKryukov 20-Feb-11 13:17pm
Thank you.
RaviRanjankr 6-Apr-11 15:20pm
Nice Answer! My 5 :)
SAKryukov 6-Apr-11 17:31pm
Thank you, Ravi.
This is the old Answer, recently updated.
Espen Harlinn 6-Apr-11 16:08pm
Nice - 5ed!
SAKryukov 6-Apr-11 17:32pm
Thank you very much, Espen.
You're too nice. This is the old Answer, recently updated, but I think I should thank you for this update. That was you who brought my attention to these AppDomain events, don't you remember that?
Espen Harlinn 6-Apr-11 17:41pm
Yes :)
SAKryukov 6-Apr-11 17:34pm
Therefore I credited you for that in my new update.
Espen Harlinn 6-Apr-11 17:44pm
Thanks - Using a loop to handle the scrolling doesn't make much sense - so I added a suggestion about invalidating and using ScrollDC

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 6 Apr 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100