0

I am trying to enable scrolling on a picturebox. I have everything working except for one nagging little detail. The focus shifts in an inexplicable way to the wrong scroll bar with the first click of the arrow key. The picture box has two scrollbars which become visible when the picture being displayed is too large. If the picture fits inside the box no scrollbars show and the vertical arrow keys behave the same as page up/down keys.

On the mouse-down event over the picture box I set the focus to the picture box object so the user can scroll with the mouse wheel. The user can then move the image one of three ways. They can click and hold and drag it up/down/right/left, or they can scroll with the mouse wheel vertically or they can use the arrow keys to scroll in all four direction. If they use the arrow keys I check PreviewKeyDownEvent on the scroll bars for which key and if it is an up/down or right/left arrow key I set the focus on either the horizontal or vertical scroll bar.

The problem is (I see this in the debugger) that when the down key is pressed (as an example) the PreviewKeyDownEvent handler runs and sets the focus (verticalBar.Focus() in this case) I see the focus shift to the vertical scroll bar, but as soon as the scope leaves the event handler it shifts to the opposite scroll bar (horizontal in this case)

I am writing a form into a third party application so I could see focus going to some default control being caused by some outside function. The odd thing is that it always goes to the opposite scroll bar from what I just set; vertical if I want horizontal and horizontal if I want vertical. Once the focus is on either scroll bar a second click on the an arrow key will shift the focus to the correct scroll bar and things will then work appropriately after that.

If the focus is already ON one scroll bar or the other it will work as expected. Focus will shift back and forth between them appropriately. But if focus starts on the picture box it will always be set to the wrong bar on the first click.

I am new to C# and GUI stuff in general so any advice will be much appreciated.

thanks

Terry H
  • 310
  • 4
  • 19
  • That sounds weird. Just a funny thought: You mention that is always stays at the opposite ScrollBar. Can you simply set focus to the wrong one on purpose? – TaW Apr 02 '14 at 15:55
  • Yeah, that's a little TOO clever. I have tried it and focus goes to the wrong one and stays there. After investigating further I have found that the focus shifting only happens for the up and right arrow keys. If I do not set focus at all in my code the down/left keys will set it appropriately but the other two wont set it at all and will not scroll. If I only set it for up and right it works just the way I want for down/left but will shift the way I described above for up/right. – Terry H Apr 03 '14 at 13:10

0 Answers0