locked
windows form jumping when mouse clicked RRS feed

  • Question

  • I am working in .Net csharp.

    we have a panel that is rather large and causes a vertical scroll bar. Near the bottom on the panel there is a large grid control and only the first couple of rows are visible. When I click on the control, the entire panel scroll up until the entire grid control is visible. This is undesirable because it makes the panel jump around and causes the user to lose their place on the form. I need some way to keep the scroll bars but prevent the panel from jumping around. For some reason the Scroll even is not getting fired when this happens so I don't know exacly what is causing the scrolling. We want the grid control to remain where it is when the user click in it. Any help is appreciated.

    Thursday, April 15, 2010 9:49 PM

Answers

  • Hi James,

    It's not just a DataGridView; it happens with any control that's higher than the panel, eg Infragistics UltraWinGrid, RichTextBox.

    If your DataGridView is the only control in the panel, just set the Dock to Fill and let the dataGridView handle scrolling on it's own.

    Any way, back to the point. This is caused by the ScrollToControl event being fired automatically by the ScrollableControl class, and the event handler scrolling to show the top left of the control that got focus. This behavior is not helpful when the scrollable container control only contains one control. I was very frustrated by this behavior until I found out how to stop it.

    The way to stop this behavior is to override the ScrollToControl event handler, like this:

         class PanelNoScrollOnFocus : Panel 
         { 
              protected override System.Drawing.Point ScrollToControl(Control activeControl) 
              { 
                  return DisplayRectangle.Location; 
              } 
         }

    Best Wishes,
    Helen Zhou


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Tuesday, April 20, 2010 3:05 AM