I have a timer that runs a SQL query every 30 seconds to retrieve updated information.

Unfortunately every time the query runs the vertical scroll bar resets to the top most part of the form.

I tried

int scrollPosition = this.VerticalScroll.Value;

at the beginning of the timer code and

this.VerticalScroll.Value = scrollPosition;

at the end, but it only scrolls 1/2 way back. For example, if you are all the way at the bottom of the form, it resets to the middle of the form.

Any suggestions? I'm lost. It's not a major bug, but it's annoying.

8 Years
Discussion Span
Last Post by zachattack05


The solution I was using was correct. For some reason when the form is small in size the code doesn't function the way I expected. But if the window is made larger it works perfectly, so much so that you don't even notice anything happened.

Here is my code:

#region Refresh RecordSet
        /// <summary>
        /// Reloads, trims and refreshes the current recordset every 30 seconds
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ReloadRecordSet(object sender, EventArgs e)
            //set cursor
            Cursor = Cursors.AppStarting;

            int scrollPosition = this.VerticalScroll.Value;

            //set the task
            Main main = ParentForm as Main;
            mainTasks task = new mainTasks(main.statusTaskCurrent);
            mainProgress progress = new mainProgress(main.statusTaskProgress);
            task("Refreshing account data...");

            //get our current position in the recordset
            int recordCurrent = int.Parse(DataNavigatorPositionItem.Text) - 1;
            //get the number of records we have right now before the refresh
            int oldRecordCount = bindsrc.Count;

            //update the dataset with the new records
            adrQuery.Fill(dsQuery, "accounts");
            //delete the old records from the data set
            int i = 0;
            while (i < oldRecordCount)
                //update our progress bar
                progress(oldRecordCount, i);


            //accept any changes to the data

            //set our current position
            bindsrc.Position = recordCurrent;

            //reset the task
            progress(100, 0);

            this.VerticalScroll.Value = scrollPosition;

            //return cursor
            Cursor = Cursors.Default;

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.