ScrollableControl
-
I 'm using a ScrollableControl and I want to draw it's client area according to the position of its scrollbars. I 've set "AutoScroll" to false and all the properties of the horizontal & vertical scrollbars to the desired values. However, when the user tries to scroll the scrollbars, their position (Value) becomes zero. You can download a demo project of my problem from: http://rapidshare.com/files/149469756/ScrollableControl\_Bug.zip.html Could it be a bug of the ScrollableControl ? Thanks in advance.
kostas KEL
-
I 'm using a ScrollableControl and I want to draw it's client area according to the position of its scrollbars. I 've set "AutoScroll" to false and all the properties of the horizontal & vertical scrollbars to the desired values. However, when the user tries to scroll the scrollbars, their position (Value) becomes zero. You can download a demo project of my problem from: http://rapidshare.com/files/149469756/ScrollableControl\_Bug.zip.html Could it be a bug of the ScrollableControl ? Thanks in advance.
kostas KEL
Here is the code (just in case you don't like downloading):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace ScrollableControl_Bug
{
public partial class Form1 : Form
{
ScrollableControl scrl = new ScrollableControl();public Form1() { InitializeComponent(); } private void Form1\_Load(object sender, EventArgs e) { this.Controls.Add(scrl); this.Resize += new EventHandler(Form1\_Resize); scrl.Dock = DockStyle.Fill; scrl.AutoScroll = false; // We want to draw the contents of "scrl" based on the scrollbar position. (No controls inside it) scrl.Visible = true; scrl.HorizontalScroll.Minimum = 0; scrl.HorizontalScroll.Maximum = 400; scrl.HorizontalScroll.LargeChange = this.Height; scrl.HorizontalScroll.SmallChange = 10; scrl.HorizontalScroll.Value = 30; scrl.HorizontalScroll.Visible = true; scrl.VerticalScroll.Minimum = 0; scrl.VerticalScroll.Maximum = 400; scrl.VerticalScroll.LargeChange = this.Width; scrl.VerticalScroll.SmallChange = 10; scrl.VerticalScroll.Value = 30; scrl.VerticalScroll.Visible = true; scrl.Scroll += new ScrollEventHandler(scrl\_Scroll); scrl.Paint += new PaintEventHandler(scrl\_Paint); } void scrl\_Paint(object sender, PaintEventArgs e) { Graphics gr = e.Graphics; string str = "Horiz. Scroll Value = " + scrl.HorizontalScroll.Value.ToString() + "\\r\\nVert. Scroll Value = " + scrl.VerticalScroll.Value.ToString() + "\\r\\n\\r\\nTry to scroll..."; gr.DrawString(str, new Font("Arial", 10.0f), new SolidBrush(Color.Black), 0.0f, 0.0f); } void Form1\_Resize(object sender, EventArgs e) { scrl.VerticalScroll.LargeChange = this.Height; scrl.HorizontalScroll.LargeChange = this.Width; scrl.Refresh(); } void scrl\_Scroll(object sender, ScrollEventArgs e) { scrl.Refresh(); } }
}
kostas KEL
-
Here is the code (just in case you don't like downloading):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace ScrollableControl_Bug
{
public partial class Form1 : Form
{
ScrollableControl scrl = new ScrollableControl();public Form1() { InitializeComponent(); } private void Form1\_Load(object sender, EventArgs e) { this.Controls.Add(scrl); this.Resize += new EventHandler(Form1\_Resize); scrl.Dock = DockStyle.Fill; scrl.AutoScroll = false; // We want to draw the contents of "scrl" based on the scrollbar position. (No controls inside it) scrl.Visible = true; scrl.HorizontalScroll.Minimum = 0; scrl.HorizontalScroll.Maximum = 400; scrl.HorizontalScroll.LargeChange = this.Height; scrl.HorizontalScroll.SmallChange = 10; scrl.HorizontalScroll.Value = 30; scrl.HorizontalScroll.Visible = true; scrl.VerticalScroll.Minimum = 0; scrl.VerticalScroll.Maximum = 400; scrl.VerticalScroll.LargeChange = this.Width; scrl.VerticalScroll.SmallChange = 10; scrl.VerticalScroll.Value = 30; scrl.VerticalScroll.Visible = true; scrl.Scroll += new ScrollEventHandler(scrl\_Scroll); scrl.Paint += new PaintEventHandler(scrl\_Paint); } void scrl\_Paint(object sender, PaintEventArgs e) { Graphics gr = e.Graphics; string str = "Horiz. Scroll Value = " + scrl.HorizontalScroll.Value.ToString() + "\\r\\nVert. Scroll Value = " + scrl.VerticalScroll.Value.ToString() + "\\r\\n\\r\\nTry to scroll..."; gr.DrawString(str, new Font("Arial", 10.0f), new SolidBrush(Color.Black), 0.0f, 0.0f); } void Form1\_Resize(object sender, EventArgs e) { scrl.VerticalScroll.LargeChange = this.Height; scrl.HorizontalScroll.LargeChange = this.Width; scrl.Refresh(); } void scrl\_Scroll(object sender, ScrollEventArgs e) { scrl.Refresh(); } }
}
kostas KEL
Thanks to Anonymous poster at some Blogger.com site... --- Just in case someone stumbles across this post searching for a way to disable the AutoScroll behavior of scrolling to the focused control, the cleanest solution is provided in .NET 2.0: There is an overrideable ScrollToControl method in the ScrollableControl now. Replace the call to the base class implementation to return DisplayRectangle.Location and problem solved. protected override Point ScrollToControl(Control activeControl) { return DisplayRectangle.Location; } --- Hope it helps. - Erpizn13
-
Thanks to Anonymous poster at some Blogger.com site... --- Just in case someone stumbles across this post searching for a way to disable the AutoScroll behavior of scrolling to the focused control, the cleanest solution is provided in .NET 2.0: There is an overrideable ScrollToControl method in the ScrollableControl now. Replace the call to the base class implementation to return DisplayRectangle.Location and problem solved. protected override Point ScrollToControl(Control activeControl) { return DisplayRectangle.Location; } --- Hope it helps. - Erpizn13
This didn't work for me - my panel still scrolls to the top of the selected control when the panel is reactivated after some other window has the focus. But thanks to you I did find a solution. I was able to disable scrolling by overriding method AdjustFormScrollbars(). I use a class-level flag (_enableScroll) to control when I want scrolling enabled, as follows: protected override void AdjustFormScrollbars(bool displayScrollbars) { if (_enableScroll) { base.AdjustFormScrollbars(displayScrollbars); } } I set _enableScroll false when my main form is deactivated and use a timer to set it true again a second after my main form is re-activated.
-
Thanks to Anonymous poster at some Blogger.com site... --- Just in case someone stumbles across this post searching for a way to disable the AutoScroll behavior of scrolling to the focused control, the cleanest solution is provided in .NET 2.0: There is an overrideable ScrollToControl method in the ScrollableControl now. Replace the call to the base class implementation to return DisplayRectangle.Location and problem solved. protected override Point ScrollToControl(Control activeControl) { return DisplayRectangle.Location; } --- Hope it helps. - Erpizn13
Overriding ScrollToControl() in my custom UserControl as above almost worked for me. It did prevent the scroll from moving when my UserControl regained focus. But, the scroll would still move unexpectedly when I did something else, such as changing the location of a child of the UserControl. However, returning the AutoScrollPosition, instead of the DisplayRectangle's location, does work for me.
protected override Point ScrollToControl( Control c ) { return AutoScrollPosition; }
I got the clue from http://yue-gao.blogspot.com/2009/02/c-want-scroll-to-stay-still.html