Drawing a button on a winform panel when the panel scrolls

In the panel, I draw controls, such as a button / text field, located at the beginning (0,0) of the panel. These controls are invisible and disabled at the beginning. I have a button outside the panel that makes these buttons / text box visible when clicked and positions them in a new place on the panel.

The problem is that if the user scrolls the panel to some (x, y), and then click the "make visible" button, the new (x, y) button location is calculated from the current (x, y) panel location - not from the top (0 , 0) panels.

I am wondering if this panel behavior is correct, and to fix it, I need to consider this.VerticalScroll.Value as the offset (x, y) when I rearrange the buttons.

enter image description here

+6
source share
2 answers

This is what worked for me. When you get Y from your button / text box, etc., follow these steps:

relativeControlTop = theControl.Top - thePanel.AutoScrollPosition.Y;

When you install the top of do:

relativeControlTop = theControl.Top + thePanel.AutoScrollPosition.Y;

Hope this helps.

+2
source

Not sure if this will help in your situation, but I think it's worth a try:

If you plan to create a panel using Visual Designer, I recommend using two panels : full-size, containing all the controls, its size makes it easy to design; and smaller, which has a physical size. Then just add large as the only smaller one (e.g. innerPanel in bottomPanel).

For more information, see Article: AutoScroll Experiment for WinForms for more information.

0
source

All Articles