1

I have a button and I want it to be always at the same location on my form (top-left corner of visible part of the form).

Here is the example:

enter image description here

So when I scroll vertically or horizontally, the button should always be at the top-left corner and it should be visible. What should I do to make it fixed?

Zelzer
  • 561
  • 1
  • 5
  • 16
  • @Chepene I tried but this doesn't help. Anchor helps when you need to keep an element at the same location on resizing the form. – Zelzer May 17 '13 at 13:24

2 Answers2

2

I guess you are using Form's AutoScroll feature.

What about placing Panel with AutoScroll = true on the form and use its scrolling instead? Then you will be able to place a button on a form but over this scrollable panel. So, the scroll rulers will scroll the view panel contents, but the button will stay pinned to the form.

If you don't want to add another container component, then you can add a handler on Scroll event and adjust the button position in response to form scrolling. The ScrollEventArgs argument has ScrollOrientation, NewValue and OldValue to calculate new X/Y position of the control.

Artemix
  • 2,113
  • 2
  • 23
  • 34
  • It's a possible option, but for that I'll need to add another container which is undesirable for me. If nothing else is possible, then this will be the answer. – Zelzer May 17 '13 at 13:52
  • Then you need to add handler on Scroll event and adjust the button position in response to form scrolling. Its ScrollEventArgs argument has ScrollOrientation and NewValue/OldValue to calculate new X/Y position of the control. – Artemix May 17 '13 at 13:57
  • Maybe this will help to catch mousewheel: http://stackoverflow.com/questions/4429901/how-to-capture-mouse-wheel-on-panel . The form has Control.MouseWheel Event, you can try to process it as well. – Artemix May 17 '13 at 14:09
  • Ok, I've tried this one. Looks like everything is fine. Thanks! – Zelzer May 17 '13 at 15:49
0

On the form, you place a Panel and set: its Dock property to Fill, and AutoScroll property to True.

You place all the other controls inside this panel, but not the button you want to keep visible. Right-click on the panel->Send-to-Back. The Panel will adjust the size to match the form; the scrolling will only happen in the panel, so the button will always stay visible(you can set Anchor:Left,Top on it)

In order to be able to scroll(with the mouse wheel), the focus must be on a control inside the scrollable area(inside the Panel), NOT on the button. To prevent the button from getting focus: set TabStop to false on it; also, when it is clicked, you must also set the focus on an other control, by calling:

this.SelectNextControl(the_button, true, true, true, true);

Andy
  • 3,631
  • 2
  • 23
  • 32