0

Excuse me if this is a trivial question to WinForms users, I come from a background in game programming, and the WinForms API is very different than I'm used to for doing the UI in games, and this is my first time with WinForms.

I have a UserControl, attached to that is a Panel, attached to that panel is a TableLayoutPanel with many rows and 1 column, and in each of those rows is a Panel that displays an image. Hierarchy looks like this:

UserControl
|- Panel (I use this one to scroll the child of this)
   |- TableLayoutPanel (many rows, 1 column, one panel per row)
      |- Panel1 (contains an image)
      |- Panel2 (contains an image)
      ...
      |- PanelN (contains an image)

Each of the rows, as well as the panel within them, is created at runtime.

Now, since that TableLayoutPanel is usually too tall to fit on one screen, I have its parent panel set up to scroll.

So here is my problem: I need to be able to search all of the leaf panels (Panel1 - PanelN), and find the ones currently showing on the screen. I am trying to do this by taking each Panel's location (Panel1.Location), and seeing which y-coordinate is closest to 0, and using that as my starting point. The problem is that I can't seem to find the right combination of calls to PointToScreen, RectangleToClient, RectangleToScreen, etc to get useful data. Regardless of which Point/Rectangle I get from those calls, they either have y-values that are constant (i.e. defined relative to their parent TableLayoutPanel, not moving) or they do move with the scroll bar, but there is some constant added based on either the window's position on the screen or its position relative to some other parent elements that are above the UserControl.

What calls do I need to make to get the position of Panel1 to return (x, 0) when the scroll bar (on Panel) is all the way at the top of the page, and start returning negative numbers (x, y < 0) as the user scrolls down?

I've looked at these stackoverflow questions (and quite a few other resources), but the code in them did not help with my problem: Getting The Location Of A Control Relative To The Entire Screen? , c# absolute position of control on screen , How to get the Screen position of a control inside a group box control?

Community
  • 1
  • 1
Cody
  • 2,643
  • 2
  • 10
  • 24

1 Answers1

0

Try Panel1.Top + TableLayout1.Top - Panel.VerticalScroll.Value

AlexDev
  • 4,049
  • 31
  • 36