简体   繁体   中英

painting a button in winform panel while the panel is scrolled

In a panel i draw controls such as button/textbox positioned starting (0,0) in the panel. These controls are invisible and disabled at the start. I have a button outside the panel that makes these buttons/textbox visible when pressed and position them to a new location in the panel.

The problem is if user scrolled the panel to some (x,y) and then press the 'make visible' button the new (x,y) location of the button is calculated from the current (x,y) location of the panel - not from the top (0,0) of the panel.

I am wondering if this is the correct behavior of panel and that to fix this i need to consider the this.VerticalScroll.Value as an (x,y) offset when i re-position the buttons.

在此处输入图片说明

This is what worked for me. When you get the Y of you button/textbox etc do:

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

When you set the top do:

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

Hope this Helps.

Not sure if it would help in your situation, but I think it's worth trying:

If you were to design the Panel using Visual Designer, I recommend using two Panels : a full-size one holding all the Controls, its size allows for easy designing; and the smaller one which has the intended physical size. Then simply add the large one as the only child of the smaller one (like innerPanel in bottomPanel).

See this article Article: WinForms AutoScroll Experiment for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM