Autoscroll scrollbar thumb position being reset upon focus change

G

Guest

Given the following Form with a panel with autoscroll set to true and with
enough controls at different locations to cause autoscrolling to be visibly
on, there is a slight annoyance with the behavior of the scrollbar upon
switching focus to another window -- the scrollbar thumb position
automatically gets reset to zero upon the Form getting the focus back. When
I open standard windows apps like Microsoft Word and scroll down a document
and then lose focus by switching to another application and then regain the
focus on that Word document, the scrollbar thumb position is same as it was
upon losing focus. Is this a bug/feature that I need to work around by
saving off the thumb position in the Control.LostFocus event handler and
restore in the Control.GotFocus event handler? Or is there some super simple
way like a property setting - (the more I learn C#, the more I realize that
some things are ridiculously simple if you know what property to use -- e.g.
using Control.UseWaitCursor property of a container control instead of having
to worry about setting the Cursor property to WaitCursor for each control
contained by that container).

<code>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace autoScrollTest
{
public partial class Form1 : Form
{
public Form1()
{
Panel p = new Panel();
this.Controls.Add(p);
p.AutoScroll = true;
for (int i = 0; i < 1000; i++)
{
Button btn = new Button();
btn.Visible = true;
btn.Text = "Button " + i;
btn.Location = new Point(0,i * btn.Height);
p.Controls.Add(btn);
}
}
}
}
</code>
 
G

Guest

The following code fixes the thumb position being reset. However I don't
really understand why -- it seems strange to have to multiply the
AutoScrollPosition coordinates by -1, unless you consider setting the
AutoScrollPosition to be like mapping from logical to viewport coordinates
and retreiving the position as being the reverse mapping:

<code>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace autoScrollTest
{
public partial class Form1 : Form
{
private Point thumbPos = new Point();
Panel p;
int iHandles = 0;
public Form1()
{
p = new Panel();
this.Controls.Add(p);
p.AutoScroll = true;
this.Activated +=new EventHandler(Form1_Activated);
this.Deactivate +=new EventHandler(Form1_Deactivate);
//p.Enter += new EventHandler(p_Enter);
//p.Leave +=new EventHandler(p_Leave);
for (int i = 0; i < 1000; i++)
{
Button btn = new Button();
btn.HandleCreated += new EventHandler(btn_HandleCreated);
btn.Visible = true;
btn.Text = "Button " + i;
btn.Location = new Point(0,i * btn.Height);
p.Controls.Add(btn);
p.AutoScrollPosition = new Point(0, 2000);
}
}

public void btn_HandleCreated(object sender, EventArgs e)
{
iHandles++;
}

public void Form1_Deactivate(object sender, EventArgs e)
{
thumbPos = p.AutoScrollPosition;
}

public void Form1_Activated(object sender, EventArgs e)
{
thumbPos.X *= -1;
thumbPos.Y *= -1;
p.AutoScrollPosition = thumbPos;
}
}
}
</code>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top