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