locked
the panel scrollbar reset? RRS feed

  • Question

  • when I create a winform program(MDI, maybe SDi also has this bug),
    I have a bug and I don't know how to solve:
    Step1, create 3 forms,:form1 is the parent winform, form2 and form3 are the chilform, show bose form2&form3 in the form1.
    Step2, the form2 has a panel(Panel1) and AutoScroll = true, and the panel contains a richbox(or other) that the size is bigger than Panel1,
    Step3, run the program, first I scroll the panel's scrollbar, then click form3, final click back form2 and the scorllbar reset!!!!!
    and this bug also accuse when switch windows or I re assignment the position:
    this.Activated += new EventHandler(delegate(object o, EventArgs x)

        {

            this.panel1.VerticalScroll.Value = this.verticalScroll;

            this.panel1.HorizontalScroll.Value = this.horizontalScroll;

        });

    can anybody tell me how I can solve this bug?

    P.S.
    1,you can use the UserControl instead of panel,but I find it will not used.
    2,don't use the VScrollBar or HScrollBar.

    THX


    Monday, November 30, 2009 4:29 PM

Answers

  • Hi,

    you have to store the position of scroll bars in two variables and when the form is activated. Assign the position from that variables to scroll bars again.

    Check this code for Form2.

        public partial class Form2 : Form
        {
            int X_Value; // Stores position of Horizontal scroll bar
            int Y_Value; // Stores position of Vertical scroll bar
    
            public Form2()
            {
                InitializeComponent();
            }
    
            private void panel1_Scroll(object sender, ScrollEventArgs e)
            {
                if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
                {
                    X_Value = e.NewValue;
                }
    
                if(e.ScrollOrientation == ScrollOrientation.VerticalScroll)
                {
                    Y_Value = e.NewValue;
                }
            }
    
            private void Form2_Activated(object sender, EventArgs e)
            {
                panel1.AutoScrollPosition = new Point(X_Value, Y_Value);
            }
        }
    • Marked as answer by sinbbxxx Tuesday, December 1, 2009 12:58 AM
    Monday, November 30, 2009 5:15 PM
  • hi,

    it works cool!!!
    lol

    thanks! 
    • Marked as answer by sinbbxxx Tuesday, December 1, 2009 12:57 AM
    Tuesday, December 1, 2009 12:56 AM

All replies

  • Hi,

    you have to store the position of scroll bars in two variables and when the form is activated. Assign the position from that variables to scroll bars again.

    Check this code for Form2.

        public partial class Form2 : Form
        {
            int X_Value; // Stores position of Horizontal scroll bar
            int Y_Value; // Stores position of Vertical scroll bar
    
            public Form2()
            {
                InitializeComponent();
            }
    
            private void panel1_Scroll(object sender, ScrollEventArgs e)
            {
                if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
                {
                    X_Value = e.NewValue;
                }
    
                if(e.ScrollOrientation == ScrollOrientation.VerticalScroll)
                {
                    Y_Value = e.NewValue;
                }
            }
    
            private void Form2_Activated(object sender, EventArgs e)
            {
                panel1.AutoScrollPosition = new Point(X_Value, Y_Value);
            }
        }
    • Marked as answer by sinbbxxx Tuesday, December 1, 2009 12:58 AM
    Monday, November 30, 2009 5:15 PM
  • hi,

    it works cool!!!
    lol

    thanks! 
    • Marked as answer by sinbbxxx Tuesday, December 1, 2009 12:57 AM
    Tuesday, December 1, 2009 12:56 AM
  • AFAICT, this does not really work. If you observe the behavior closely, you will see that the scrollbar(s) briefly return to the zero position before moving to the 'saved' position. Start the app in debug mode, size the IDE and the running app so that you can see them both and they don't overlap each other, move one or both of the scrollbars to a new, non-zero position, set a breakpoint at the beginning of the Activated event handler, then return focus to the app. The breakpoint will be hit and you will see in your app that both scroll bars are at the zero position. Hit F5 to continue and the scrollbars will go to the saved position.

    When the area of the control within the panel is large, the double resetting of the scrollbars is visually obvious. What is the best way to handle the event that proceeds the moving of the scrollbars?

     

    Friday, August 6, 2010 4:45 PM