Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Disable focus in button with arrow right left up down

Disable focus in button with arrow right left up down

Scheduled Pinned Locked Moved C#
help
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member 11426986
    wrote on last edited by
    #1

    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
            {
                
            }
        }
    
    R A L 3 Replies Last reply
    0
    • M Member 11426986

      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
              {
                  
              }
          }
      
      R Offline
      R Offline
      Ralf Meier
      wrote on last edited by
      #2

      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 ...

      M 1 Reply Last reply
      0
      • R Ralf Meier

        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 ...

        M Offline
        M Offline
        Member 11426986
        wrote on last edited by
        #3

        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.

        L 2 Replies Last reply
        0
        • M Member 11426986

          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.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          You need to tell the framework that you have handled the keypress, so it does not treat like a request to move to the next control.

          1 Reply Last reply
          0
          • M Member 11426986

            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
                    {
                        
                    }
                }
            
            A Offline
            A Offline
            Alan N
            wrote on last edited by
            #5

            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.

            B 1 Reply Last reply
            0
            • M Member 11426986

              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.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              If you use the Control.KeyPress Event (System.Windows.Forms) | Microsoft Docs[^] instead of PreviewKeyDown you can set the Handled property of the KeyPressEventArgs Class (System.Windows.Forms) | Microsoft Docs[^] to tell the framework not to do anything with the keypress.

              1 Reply Last reply
              0
              • M Member 11426986

                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
                        {
                            
                        }
                    }
                
                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                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

                B 1 Reply Last reply
                0
                • A Alan N

                  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.

                  B Offline
                  B Offline
                  BillWoodruff
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • L Lost User

                    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

                    B Offline
                    B Offline
                    BillWoodruff
                    wrote on last edited by
                    #9

                    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

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups