scrollbar bound to arrow keys

J

jonpb

Hi,
The arrow keys (up, down, left, right keys) are bound to the scrollbars
of my UserControl. For instance, if another control is in focus and I
press an arrow key the last scrollbar that had focus gets focus and it's
value changes with each press. Strangely enough, this even happens when
the scrollbars have .Visibility = false.

Is there a property that controls this behaviour? I can't find it.

Thanks
 
M

Morten Wennevik [C# MVP]

Hi,

Scrollbars do not automatically grab the arrow key events unless there is nothing else taking key input. Firing up a simple program with a HScrollBar and a button to switch visibility on the scrollbar I could not reproduce your problem. Once the scrollbar was invisible, it did not adjust the position when using the arrow keys.

Do you have custom binding or key handling that could interfere?
 
J

jonpb

Hi,
Thanks for the response, in your test did you handle the scrollbar
change events? I tried both the Scroll event and ValueChanged event, in
both cases you see the behaviour I describe (i.e. the scrollbars trap
the arrow key events.

Add a vertical scrollbar, horizontal scrollbar and a button positioned
at 10, 10. The following code reproduces the behaviour:

namespace TestControl
{
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
vScrollBar1.Height = this.Height;
vScrollBar1.Minimum = 10;
vScrollBar1.Maximum = 50;
hScrollBar1.Width = this.Width;
hScrollBar1.Minimum = 10;
hScrollBar1.Maximum = 50;
}

private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
button1.Left = hScrollBar1.Value;
}

private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
button1.Top = vScrollBar1.Value;
}
}
}
 
J

jonpb

To update, I was mistaken on the Visibility issue, I'm using a
TableLayoutPanel and to hide the scrollbars I resize the column\row, so
the scrollbars were still visible. In any case the question remains, is
it possible to stop the scrollbars from trapping the arrow key events.
 
J

jonpb

I found the solution. I didn't realize the Keydown is not fired for
control keys. The function you need to override is ProcessDialogKey.
Returning true from this function signifies that the key has been
processed and no further processing is performed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top