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.

Figured it out. :)

If you have an answer why not post it so others can benefit.

Sure,

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...");
            Application.DoEvents();

            //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);

                dsQuery.Tables["accounts"].Rows[i].Delete();
                i++;
            }

            //accept any changes to the data
            dsQuery.AcceptChanges();

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

            //reset the task
            task("Idle");
            progress(100, 0);
            Application.DoEvents();

            this.VerticalScroll.Value = scrollPosition;

            //return cursor
            Cursor = Cursors.Default;
        }

        #endregion
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.