novacara
novacara

Reputation: 2257

Scrollbar location is moving visible/nonvisible changing controls

I have a form with a scrollable panel and two controls sitting right on top of each other - one visible one not. Based on a certain condition when that form is activated I might swap the visible properties of the two controls. These controls are at the bottom of the scrollable panel. If when I leave that form I leave it scrolled to the bottom, go change the condition that will cause the controls' visibility to swap and go back to that form the visible control will have dropped about 200px down the page leaving a large gap. Anyone know what could be causing this? I tried resetting the scrollbar position to the top on form close but that just causes a smaller gap and sometimes the control to move higher into other controls. Any ideas?

Upvotes: 1

Views: 1219

Answers (3)

Gogu CelMare
Gogu CelMare

Reputation: 815

Have you verified the order that you change visibility of the two controls? The scroll bars on a container with auto scroll set to true will appear and disappear depending on the position of controls that are outside of the visible area of the control. Controls that are invisible do not count. So in your case if you make both controls invisible at anytime, the scroll bars will disappear. They will come back when one control is made visible. So to make sure you don't have a jump in scroll bar position and controls position you should make sure that at no time are both controls invisible. Another solution is to have a pseudo-visible control on the container. That is a control that has its visibility set to true but it is not actually visible for the user (for example a dot of the color of the background, a label with no text ...). Position this control in the furthest position x,y and the scroll bars will never disappear..

Upvotes: 0

Loathing
Loathing

Reputation: 5256

Here is an example that reproduces the problem. If the mouse is moved over the red label, the visibility of button2 is changed to true which causes the scroll jumps back up to Button1.

public class Form123456 : Form {

    public Form123456() {
        Controls.Add(new UC1());
    }

    public class UC1 : UserControl {
        Button b1 = new Button { Text = "Button1" };
        Label lb = new Label { Text = "_", AutoSize = true, BackColor = Color.Red };
        Button b2 = new Button { Text = "Button2", Visible = false };
        Button b2b = new Button { Text = "x" };
        Button b3 = new Button { Text = "Button3" };
        public UC1() {
            AutoScroll = true;
            Dock = DockStyle.Fill;
            b1.Location = new Point(0, 200);
            b2.Location = new Point(0, 600);
            lb.Location = new Point(70, 600);
            b2b.Location = new Point(90, 600);
            b3.Location = new Point(0, 800);
            Controls.Add(b1);
            Controls.Add(b2);
            Controls.Add(lb);
            Controls.Add(b2b);
            Controls.Add(b3);

            lb.MouseEnter += delegate {
                b2.Visible = true;
            };
            lb.MouseLeave += delegate {
                b2.Visible = false;
            };
        }
    }
}

To fix it, one solution is to add this code:

    protected override Point ScrollToControl(Control activeControl) {
        return this.AutoScrollPosition;
    }

Solution from: Why does clicking in a text box cause an AutoScroll panel to scroll back to the top?

Upvotes: 1

Hans Passant
Hans Passant

Reputation: 941317

No repro. Sounds to me that you are doing more than just changing the Visible property. Whenever you assign the Location property, you have to add the AutoScrollPosition to compensate for the scroll state. Post code if this doesn't help.

Upvotes: 0

Related Questions