ispiro
ispiro

Reputation: 27643

Prevent AutoScroll when contained Control gets focus

I have a Panel with two Buttons on it, one of which is partially hidden. When the partially-hidden button gets focus - such as when the other is clicked and then it (-the original) is clicked - the panel gets scrolled. I need the vertical scrollbar but not the auto scrolling, and there seems to be no way of getting a functioning scrollbar without the auto scrolling.

The code:

using System.Drawing;
using System.Windows.Forms;

namespace test
{
    public partial class Form1 : Form
    {
        Panel panel = new Panel
        {
            AutoScroll = true,
            Size = new Size(200, 200),
            Location = new Point(0, 30),
        };
        Button b1 = new Button
        {
            Location = new Point(100, 100),
            Size = new Size(50, 150),
            BackColor = Color.Black
        };
        Button b2 = new Button();

        public Form1()
        {
            InitializeComponent();
            panel.Controls.Add(b1);
            Controls.Add(panel);
            Controls.Add(b2);
        }
    }
}

Upvotes: 3

Views: 6907

Answers (1)

ispiro
ispiro

Reputation: 27643

Use a derived class which can access the VScroll property.

But more simply and works better:

Override ScrollToControl. See How can you stop a Winforms Panel from scrolling? .

Upvotes: 4

Related Questions