locked
Scrollbar moved to display left,top edge of usercontrol when usercontrol too wide RRS feed

  • 質問

  •  

    Ok.. so here's the scenario:

     

    I have a main user control (MUC) set to autoscroll its width cannot be more than 400px wide.

     

    I add (using code not drag and drop) a series of identical inner user controls (IUCs) to MUC which are wider than 400px there the scroll bars kick into action (which is fine).

     

    But... When I click on an IUC the MUC adjusts itself so the the top,left edge of the IUC is visible. e.g. if I scroll to the right most edge of an IUC and click on it, as soon as it get focus it causes the MUC to jump back to the left most top edge of the IUC I clicked on.

     

    I am assuming the the Scrollbars on the MUC are trying to automatically fit the IUC into the visible area and I would like to stop it from doing this. I have tried a number of things but the only thing that stops this occuring is disabling the IUCs.

     

    Thanks in advance

     

    Rob

    2007年12月12日 3:43

回答

  • Hi, rcfearn,

     

    Based on my understanding, you don't want your  Main User Control auto scroll to the selected Inner User Control, do you?

     

    I think this is a feature of Scrollable Controls.

    If you want to change this behavior, you should override ScrollToControl method.

    For example,

    Code Block

            protected override Point ScrollToControl(Control activeControl)

            {

                return this.AutoScrollPosition;

            }

     

     

    This will always return the current AutoScrollPosition, and then it will not scroll to the Control anymore.

     

    More info

    http://msdn2.microsoft.com/en-us/library/system.windows.forms.scrollablecontrol.scrolltocontrol(VS.80).aspx

     

    Hope this helps,

    Regards

     

    2007年12月17日 6:12

すべての返信

  • Hi, rcfearn,

     

    Based on my understanding, you don't want your  Main User Control auto scroll to the selected Inner User Control, do you?

     

    I think this is a feature of Scrollable Controls.

    If you want to change this behavior, you should override ScrollToControl method.

    For example,

    Code Block

            protected override Point ScrollToControl(Control activeControl)

            {

                return this.AutoScrollPosition;

            }

     

     

    This will always return the current AutoScrollPosition, and then it will not scroll to the Control anymore.

     

    More info

    http://msdn2.microsoft.com/en-us/library/system.windows.forms.scrollablecontrol.scrolltocontrol(VS.80).aspx

     

    Hope this helps,

    Regards

     

    2007年12月17日 6:12
  • Fantastic!!! that is exactly what I needed to do to fix the problem.

     

    Thanks so much for taking the time to reply!!   

     

    Cheers

    Rob

     

    2007年12月18日 1:57
  • Hi,

    I was also facing the same issue and I did it the same way as given below.

    I created a class inheriting from panel class and in that class I overriden the

    ScrollToControl method and everything works perfect. But, having a seperate

    class just to override one single method is not at all good practise. So, I was

    thinking around a question that why can't we override the same method in the

    user control which is having panels, but that I tried and it is not working.

    Can anybody please elaborate or give some idea on this?

    2010年9月29日 5:39
  • Rohit,

    Actually, extending a class to override even a single method is a very good practice. If I understand your question correctly, you can't override ScrollToControl inside an instance of UserControl because that method is declared Protected. It was designed that way on purpose.

    2010年9月29日 13:22