Dawit
Dawit

Reputation: 642

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.

enter image description here

Upvotes: 6

Views: 511

Answers (2)

Dawit
Dawit

Reputation: 642

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.

Upvotes: 2

Victor Zakharov
Victor Zakharov

Reputation: 26424

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.

Upvotes: 0

Related Questions