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. Intercept up and down arrow keys in datagrid

Intercept up and down arrow keys in datagrid

Scheduled Pinned Locked Moved C#
questionhelp
6 Posts 3 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.
  • K Offline
    K Offline
    kyledunn
    wrote on last edited by
    #1

    When a cell inside a datagrid has the focus and the up or down arrow key is pressed the focus moves to the previous or next cell. How can I intercept this event and execute code based on the pressing of the up or down arrow key? The OnKeyUp, OnKeyPress and OnKeyDown are not working as normal in the datagrid. Thanks for your help. Kyle

    J 1 Reply Last reply
    0
    • K kyledunn

      When a cell inside a datagrid has the focus and the up or down arrow key is pressed the focus moves to the previous or next cell. How can I intercept this event and execute code based on the pressing of the up or down arrow key? The OnKeyUp, OnKeyPress and OnKeyDown are not working as normal in the datagrid. Thanks for your help. Kyle

      J Offline
      J Offline
      James T Johnson
      wrote on last edited by
      #2

      kyledunn wrote: How can I intercept this event and execute code based on the pressing of the up or down arrow key? The OnKeyUp, OnKeyPress and OnKeyDown are not working as normal in the datagrid. You need to override the IsInputKey method to tell the datagrid that those keys are to be sent to OnKey* rather than treated specially by the underlying code. You can only do this if you are creating a class that inherits from DataGrid though. HTH, James Sonork ID: 100.11138 - Hasaki "Smile your little smile, take some tea with me awhile. And every day we'll turn another page. Behind our glass we'll sit and look at our ever-open book, One brown mouse sitting in a cage." "One Brown Mouse" from Heavy Horses, Jethro Tull 1978

      L 1 Reply Last reply
      0
      • J James T Johnson

        kyledunn wrote: How can I intercept this event and execute code based on the pressing of the up or down arrow key? The OnKeyUp, OnKeyPress and OnKeyDown are not working as normal in the datagrid. You need to override the IsInputKey method to tell the datagrid that those keys are to be sent to OnKey* rather than treated specially by the underlying code. You can only do this if you are creating a class that inherits from DataGrid though. HTH, James Sonork ID: 100.11138 - Hasaki "Smile your little smile, take some tea with me awhile. And every day we'll turn another page. Behind our glass we'll sit and look at our ever-open book, One brown mouse sitting in a cage." "One Brown Mouse" from Heavy Horses, Jethro Tull 1978

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

        Thanks James, What you say about this only being possible if you are creating a class that inherits from DataGrid makes sense since the IsInputKey is a Protected method. Do you know of any code examples available that would help me build this class and add the custom datagrid control to a windows form? Kyle

        J 1 Reply Last reply
        0
        • L Lost User

          Thanks James, What you say about this only being possible if you are creating a class that inherits from DataGrid makes sense since the IsInputKey is a Protected method. Do you know of any code examples available that would help me build this class and add the custom datagrid control to a windows form? Kyle

          J Offline
          J Offline
          James T Johnson
          wrote on last edited by
          #4

          The simplest thing to do is just override the methods that you need to.

          public class myArrowKeyDataGrid : DataGrid {
          public myArrowKeyDataGrid() : base() { }

          protected override bool IsInputKey(Keys key) {
          if( key == Keys.Left || key == Keys.Up || key == Keys.Down || key == Keys.Right ) {
          return true;
          }
          else {
          return base.IsInputKey(key);
          }
          }

          protected override void OnKeyDown(KeyEventArgs e) {
          Keys key = e.KeyData;

          if( e.Handled == false && (key == Keys.Left || key == Keys.Up || key == Keys.Down || key == Keys.Right ) ) {
          // Process left, up, down, right
          e.Handled = true;
          }

          // Let the base class do its thing, as well as firing the KeyDown event
          base.OnKeyDown(e);
          }
          }

          Untested code of course, but thats the premise behind it all :) HTH, James Sonork ID: 100.11138 - Hasaki "Smile your little smile, take some tea with me awhile. And every day we'll turn another page. Behind our glass we'll sit and look at our ever-open book, One brown mouse sitting in a cage." "One Brown Mouse" from Heavy Horses, Jethro Tull 1978

          K 1 Reply Last reply
          0
          • J James T Johnson

            The simplest thing to do is just override the methods that you need to.

            public class myArrowKeyDataGrid : DataGrid {
            public myArrowKeyDataGrid() : base() { }

            protected override bool IsInputKey(Keys key) {
            if( key == Keys.Left || key == Keys.Up || key == Keys.Down || key == Keys.Right ) {
            return true;
            }
            else {
            return base.IsInputKey(key);
            }
            }

            protected override void OnKeyDown(KeyEventArgs e) {
            Keys key = e.KeyData;

            if( e.Handled == false && (key == Keys.Left || key == Keys.Up || key == Keys.Down || key == Keys.Right ) ) {
            // Process left, up, down, right
            e.Handled = true;
            }

            // Let the base class do its thing, as well as firing the KeyDown event
            base.OnKeyDown(e);
            }
            }

            Untested code of course, but thats the premise behind it all :) HTH, James Sonork ID: 100.11138 - Hasaki "Smile your little smile, take some tea with me awhile. And every day we'll turn another page. Behind our glass we'll sit and look at our ever-open book, One brown mouse sitting in a cage." "One Brown Mouse" from Heavy Horses, Jethro Tull 1978

            K Offline
            K Offline
            kyledunn
            wrote on last edited by
            #5

            Thanks James! Everything you wrote made perfect sense and should work. But I still can't get it to work, at least not completely. My data contains 5 text fields and a checkbox. It does work for the cell that has the checkbox but the cells that have the textbox never fire for either the IsInputKey and OnKeyDown events. How it's working for one data type and not another is a mystery to me. If you can think of anything I would appreciate hearing from you. It was very kind of you to help. Thanks. Kyle

            J 1 Reply Last reply
            0
            • K kyledunn

              Thanks James! Everything you wrote made perfect sense and should work. But I still can't get it to work, at least not completely. My data contains 5 text fields and a checkbox. It does work for the cell that has the checkbox but the cells that have the textbox never fire for either the IsInputKey and OnKeyDown events. How it's working for one data type and not another is a mystery to me. If you can think of anything I would appreciate hearing from you. It was very kind of you to help. Thanks. Kyle

              J Offline
              J Offline
              James T Johnson
              wrote on last edited by
              #6

              It'll make it so the Key* events won't fire when an arrow key is pressed but try moving the base.OnKeyDown call so its only called if the key wasn't an arrow key. It could also be that the TextBox inside the cell is trapping those keys; so you're seeing the behavior of the TextBox and not the datagrid. James Sonork ID: 100.11138 - Hasaki "Smile your little smile, take some tea with me awhile. And every day we'll turn another page. Behind our glass we'll sit and look at our ever-open book, One brown mouse sitting in a cage." "One Brown Mouse" from Heavy Horses, Jethro Tull 1978

              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