Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. ScrollableControl

ScrollableControl

Scheduled Pinned Locked Moved C#
helphtmlcomquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KEL3
    wrote on last edited by
    #1

    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

    K 1 Reply Last reply
    0
    • K KEL3

      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

      K Offline
      K Offline
      KEL3
      wrote on last edited by
      #2

      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

      E 1 Reply Last reply
      0
      • K KEL3

        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

        E Offline
        E Offline
        Erpizn_13
        wrote on last edited by
        #3

        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

        C M 2 Replies Last reply
        0
        • E Erpizn_13

          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

          C Offline
          C Offline
          CampwoodJim
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • E Erpizn_13

            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

            M Offline
            M Offline
            Mike_Finch
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups