Disable focus in button with arrow right left up down
-
I have a problem. When i press the arrow down, up, right or left with focus in a button, i want the button have moved for direction selected,
But this does not happen, another button is focused. I set TabStop of the button to false, but even so with the arrows still focuses other buttons.
Below is the code for creating the button, and the method for moving it.private void btnCreateNewButton_Click(object sender, EventArgs e)
{
Button button = new Button();
button.Text = "";
button.FlatStyle = FlatStyle.Popup;
button.Left = 30;
button.Top = 30;
button.Width = 10;
button.Height = 40;
button.TabIndex = 1;
button.TabStop = false;
button.Click += new EventHandler(button_Click);
button.MouseDown += new MouseEventHandler(cli_MouseDown);
button.MouseMove += new MouseEventHandler(cli_MouseMove);
button.PreviewKeyDown += new PreviewKeyDownEventHandler(cli_PreviewKeyDown);panel3.Controls.Add(button); }
private void cli_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
Button but = sender as Button;
if (butMensagem.Focused)
{
int x = but.Location.X;
int y = but.Location.Y;if (e.KeyCode == Keys.Right) { x += 1; } else if (e.KeyCode == Keys.Left) { x -= 1; } else if (e.KeyCode == Keys.Up) { y -= 1; } else if (e.KeyCode == Keys.Down) { y += 1; } but.Location = new Point(x, y); } else { } }
-
I have a problem. When i press the arrow down, up, right or left with focus in a button, i want the button have moved for direction selected,
But this does not happen, another button is focused. I set TabStop of the button to false, but even so with the arrows still focuses other buttons.
Below is the code for creating the button, and the method for moving it.private void btnCreateNewButton_Click(object sender, EventArgs e)
{
Button button = new Button();
button.Text = "";
button.FlatStyle = FlatStyle.Popup;
button.Left = 30;
button.Top = 30;
button.Width = 10;
button.Height = 40;
button.TabIndex = 1;
button.TabStop = false;
button.Click += new EventHandler(button_Click);
button.MouseDown += new MouseEventHandler(cli_MouseDown);
button.MouseMove += new MouseEventHandler(cli_MouseMove);
button.PreviewKeyDown += new PreviewKeyDownEventHandler(cli_PreviewKeyDown);panel3.Controls.Add(button); }
private void cli_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
Button but = sender as Button;
if (butMensagem.Focused)
{
int x = but.Location.X;
int y = but.Location.Y;if (e.KeyCode == Keys.Right) { x += 1; } else if (e.KeyCode == Keys.Left) { x -= 1; } else if (e.KeyCode == Keys.Up) { y -= 1; } else if (e.KeyCode == Keys.Down) { y += 1; } but.Location = new Point(x, y); } else { } }
You should not only change the values of your variables x and/or y At the end of your method you should write their values back to the location of the button - otherwise nothing happens ...
-
You should not only change the values of your variables x and/or y At the end of your method you should write their values back to the location of the button - otherwise nothing happens ...
At the end of the method it does this. "but.Location = new Point (x, y);".
The problem is that by pressing the directional arrow, it changes the location of the button and does not continue with the focus on the button it focuses on another button. -
At the end of the method it does this. "but.Location = new Point (x, y);".
The problem is that by pressing the directional arrow, it changes the location of the button and does not continue with the focus on the button it focuses on another button. -
I have a problem. When i press the arrow down, up, right or left with focus in a button, i want the button have moved for direction selected,
But this does not happen, another button is focused. I set TabStop of the button to false, but even so with the arrows still focuses other buttons.
Below is the code for creating the button, and the method for moving it.private void btnCreateNewButton_Click(object sender, EventArgs e)
{
Button button = new Button();
button.Text = "";
button.FlatStyle = FlatStyle.Popup;
button.Left = 30;
button.Top = 30;
button.Width = 10;
button.Height = 40;
button.TabIndex = 1;
button.TabStop = false;
button.Click += new EventHandler(button_Click);
button.MouseDown += new MouseEventHandler(cli_MouseDown);
button.MouseMove += new MouseEventHandler(cli_MouseMove);
button.PreviewKeyDown += new PreviewKeyDownEventHandler(cli_PreviewKeyDown);panel3.Controls.Add(button); }
private void cli_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
Button but = sender as Button;
if (butMensagem.Focused)
{
int x = but.Location.X;
int y = but.Location.Y;if (e.KeyCode == Keys.Right) { x += 1; } else if (e.KeyCode == Keys.Left) { x -= 1; } else if (e.KeyCode == Keys.Up) { y -= 1; } else if (e.KeyCode == Keys.Down) { y += 1; } but.Location = new Point(x, y); } else { } }
Some of this key handling stuff is not obvious and I'm no expert! In this case you have to tell the Button control not to give the arrow keys special handling. To do this override the IsInputKey method. My example also overrides OnKeyDown to make buttons of this class move in response to the arrow keys. I imagine that you would want to have that functionality in a separate KeyDown event handler.
public class AnnoyingButton : Button {
protected override bool IsInputKey(Keys keyData) {
return keyData == Keys.Up || keyData == Keys.Down ||
keyData == Keys.Left || keyData == Keys.Right;
}protected override void OnKeyDown(KeyEventArgs args) {
Point newLocation = Location;switch (args.KeyCode) { case Keys.Right: newLocation.Offset(1, 0); Location = newLocation; break; case Keys.Left: newLocation.Offset(-1, 0); Location = newLocation; break; case Keys.Down: newLocation.Offset(0, 1); Location = newLocation; break; case Keys.Up: newLocation.Offset(0, -1); Location = newLocation; break; default: base.OnKeyDown(args); }
}
}I did have a good link about this stuff but as I'm moving computer I don't know where it is at the moment. EDIT: HERE IS THAT LINK Exploring Secrets of .NET Keystroke Handling[^] Alan.
-
At the end of the method it does this. "but.Location = new Point (x, y);".
The problem is that by pressing the directional arrow, it changes the location of the button and does not continue with the focus on the button it focuses on another button.If you use the Control.KeyPress Event (System.Windows.Forms) | Microsoft Docs[^] instead of
PreviewKeyDown
you can set theHandled
property of the KeyPressEventArgs Class (System.Windows.Forms) | Microsoft Docs[^] to tell the framework not to do anything with the keypress. -
I have a problem. When i press the arrow down, up, right or left with focus in a button, i want the button have moved for direction selected,
But this does not happen, another button is focused. I set TabStop of the button to false, but even so with the arrows still focuses other buttons.
Below is the code for creating the button, and the method for moving it.private void btnCreateNewButton_Click(object sender, EventArgs e)
{
Button button = new Button();
button.Text = "";
button.FlatStyle = FlatStyle.Popup;
button.Left = 30;
button.Top = 30;
button.Width = 10;
button.Height = 40;
button.TabIndex = 1;
button.TabStop = false;
button.Click += new EventHandler(button_Click);
button.MouseDown += new MouseEventHandler(cli_MouseDown);
button.MouseMove += new MouseEventHandler(cli_MouseMove);
button.PreviewKeyDown += new PreviewKeyDownEventHandler(cli_PreviewKeyDown);panel3.Controls.Add(button); }
private void cli_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
Button but = sender as Button;
if (butMensagem.Focused)
{
int x = but.Location.X;
int y = but.Location.Y;if (e.KeyCode == Keys.Right) { x += 1; } else if (e.KeyCode == Keys.Left) { x -= 1; } else if (e.KeyCode == Keys.Up) { y -= 1; } else if (e.KeyCode == Keys.Down) { y += 1; } but.Location = new Point(x, y); } else { } }
You attached the key handler to the button. You should (probably) attach it at the form level and see what has focus from there; intercept the keys; etc. Your "view" is too narrow.
"(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal
-
Some of this key handling stuff is not obvious and I'm no expert! In this case you have to tell the Button control not to give the arrow keys special handling. To do this override the IsInputKey method. My example also overrides OnKeyDown to make buttons of this class move in response to the arrow keys. I imagine that you would want to have that functionality in a separate KeyDown event handler.
public class AnnoyingButton : Button {
protected override bool IsInputKey(Keys keyData) {
return keyData == Keys.Up || keyData == Keys.Down ||
keyData == Keys.Left || keyData == Keys.Right;
}protected override void OnKeyDown(KeyEventArgs args) {
Point newLocation = Location;switch (args.KeyCode) { case Keys.Right: newLocation.Offset(1, 0); Location = newLocation; break; case Keys.Left: newLocation.Offset(-1, 0); Location = newLocation; break; case Keys.Down: newLocation.Offset(0, 1); Location = newLocation; break; case Keys.Up: newLocation.Offset(0, -1); Location = newLocation; break; default: base.OnKeyDown(args); }
}
}I did have a good link about this stuff but as I'm moving computer I don't know where it is at the moment. EDIT: HERE IS THAT LINK Exploring Secrets of .NET Keystroke Handling[^] Alan.
Upvoted ! A good solution. One issue to examine is whether you want to special case the Button's Click event to distinguish whether it's a "regular" click, or a click whose purpose is to set focus to the Button so that you can then move it with the arrow keys. After all, we use a Button Click event to trigger some action. Another consideration is whether to confine Button movement so it stays within the bounds of its container. If I have time, I may post some code addressing these points.
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
-
You attached the key handler to the button. You should (probably) attach it at the form level and see what has focus from there; intercept the keys; etc. Your "view" is too narrow.
"(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal
Gerry Schmitz wrote:
You attached the key handler to the button. You should (probably) attach it at the form level
The potential advantage I see in choosing to make the Button move facility self-contained is that you can easily define the sub-classed Button as a custom component ... a separate project ... then you can re-use it, mount it in the ToolBox in VS, etc.
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot