0

I am working on an application that works as a dashboard. The form includes a flowlayoutpanel that contains several datagridviews. The flowlayoutpanel is set to autoscroll because there are usually more datagrids than will fit on the visible page. Each datagrid has a buttonclick event that opens a chart form when a particular column (3) is selected. All of the chart functionality works fine. The problem I'm having is that when I click a datagrid that is only partially visible, the page scrolls to bring that datagridview completely onto the page. Because that happens on the click (MouseDown), the page scrolls so quickly that it doesn't actually trigger the clickevent, but selects several cells in the column, as if the user did a click/drag down the column.

What I would like to have happen is when the user clicks on a datagridview that is only partially visible, the chart would open, then the page would scroll on the chart close event. That actually happens if the datagridview in question has focus, but is partially off the page. For example, if I selet the datagridview, then scroll down/up until it is partially off the visible page, then click a cell in column 3. It will display the chart, then autoscroll when I close the chart. But if the datagridview does not have focus, it does the autoscroll thing. I have tried putting the flowlayoutpanel inside another panel, and that worked to some degree, but it was inconsistent.

Sorry for the long description, but I'm trying to be as detailed as possible. Any help would be greatly appreciated. The application is being built in .NET, using VS 2013.

1 Answers1

2

This seems similar to a issue I had with multiple datagridviews inside a panel, this article was the solution for me.

In short, all you need to do is create a custom panel and override the ScrollToControl method. I belive you can do the same with a FlowLayoutPanel.

EDIT: Your CustomPanel class should look something like this (code converted with developer fusion

Public Class CustomPanel
Inherits System.Windows.Forms.FlowLayoutPanel
Protected Overrides Function ScrollToControl(activeControl As System.Windows.Forms.Control) As System.Drawing.Point
    ' Returning the current location prevents the panel from
    ' scrolling to the active control when the panel loses and regains focus
    Return Me.DisplayRectangle.Location
End Function
End Class

If this doesn't work you could try inheriting a regular Panel and place a FlowLayoutPanel inside it.

TDull
  • 753
  • 5
  • 12
  • TDull, I tried this fix, but no joy. I've also tried variations of it where DisplayRectangle.Location is replaced with AutoScrollPosition or AutoScrollOffset. None have the desired effect. Maybe you can clarify something for me (and please pardon me if this is remedial). What do you mean when you say create a custom panel?My code:Public Class NoAutoScrollFlowLayoutPanel Inherits System.Windows.Forms.FlowLayoutPanel Protected Overrides Function ScrollToControl(activeControl As System.Windows.Forms.Control) As System.Drawing.Point Return DisplayRectangle.Location End Function End Class – Jimmie May Aug 19 '14 at 20:51
  • Sorry, that last bit got jumbled. I was trying to post my version of the suggested fix. Since it's in VB .NET, it's a little different. Maybe the syntax is off? My code: Public Class NoAutoScrollFlowLayoutPanel Inherits System.Windows.Forms.FlowLayoutPanel Protected Overrides Function ScrollToControl(activeControl As System.Windows.Forms.Control) As System.Drawing.Point Return DisplayRectangle.Location 'AutoScrollPosition End Function End Class – Jimmie May Aug 19 '14 at 20:57
  • It's a bit difficult to read, but it looks like your code is correct.. see my edit :) – TDull Aug 19 '14 at 23:08
  • TDull, Sorry for the bad Paste job. At any rate, I changed my code to use the CustomPanel naming convention, just for good measure, and I changed the type in the designer for the paren tform. So it went from: – Jimmie May Aug 20 '14 at 13:21
  • Geez, getting used to forum posting. Take 2: I double-checked my code, and it matches yours. I even changed my code to use the CustomPanel naming convention, just for good measure. Per one of the comments on the link you gave me, I tried to change the type in the designer for the parent form. The name of the FlowLayoutPanel is "actual". I was ablt to change the Friend WithEvents bit from “Friend WithEvents actual As FlowLayoutPanel” to Friend WithEvents actual As CustomPanel. – Jimmie May Aug 20 '14 at 13:38
  • (cont.) However, when I tried to change the declaration from Me.actual=New System.Windows.Forms.FlowLayoutPanel() to Me.actual=New System.Windows.Forms.CustomPanel(), I got this error: Type ‘System.Windows.Forms.CustomPanel’ is not defined. Having said that, if I leave the declaration as is, I get this error at runtime: Unable to cast object of type ‘System.Windows.Forms.FlowLayoutPanel’ to type ‘Project_Jimmie.CustomPanel’. I’m sure it’s clear at this point that I’ve gone beyond my scope of knowledge. Maybe I’m not making changes in the correct place. Thanks again for your help. – Jimmie May Aug 20 '14 at 13:38
  • TDUll, Woo-hoo! Found my issue. I neglected to remove the "System.Windows.Forms." piece when I changed the declaration. did that and it worked like a champe. Thanks so much again for your help. If it wasn't 10:23am I might have a drink to celebrate. – Jimmie May Aug 20 '14 at 14:23
  • btw - Sorry I can't "Vote up". Not enought rep yet. But I would if I could. – Jimmie May Aug 20 '14 at 14:24
  • TDull, I'm back! I have a variation of this same problem, wondering if you might be able to assist. This time the issue is with left/right scrolling. Same form as before, but now the panel expands off the side of the form, so the user is able to scroll to the right to see more information. The problem arises when the user clicks a different datagridview within the panel, it autoscrolls back to the left. Is there a similar tactic to make the panel not autoscroll left/right? – Jimmie May Jan 14 '15 at 18:55