Advertisement
Guest User

Untitled

a guest
Jul 25th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. /// <summary>
  2. /// Overrided method to tab only in a DataGridComboBox column.
  3. /// </summary>
  4. /// <param name="msg">A Message, passed by reference, that represents the window message to process.</param>
  5. /// <param name="keyData">One of the Keys values that represents the key to process.</param>
  6. /// <returns>True if the character was processed by the control. Otherwise, False.</returns>
  7. protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
  8. {
  9. bool TabIsPressed = ((keyData == Keys.Tab) || (keyData == (Keys.Shift | Keys.Tab))); // Checks if Tab or Shift/Tab is pressed.
  10. Control CurrentControl = this.ActiveControl; // Gets the currently focused control.
  11. bool IsDataGridViewControl = (CurrentControl.GetType() == typeof(DataGridView)); // Checks if the currently focused control is a DataGridView control.
  12. bool IsComboBoxEditingControl = (CurrentControl.GetType() == typeof(DataGridViewComboBoxEditingControl)); // Checks if the currently focused control is a DataGridViewComboBoxEditingControl control.
  13. bool DataGridViewIsValid = (IsDataGridViewControl && dataGridViewDMSSettings.Rows.Count > -1); // Checks if a DataGridView control is focused is not empty.
  14. if
  15. (
  16. TabIsPressed // If a Tab key is pressed.
  17. && (
  18. DataGridViewIsValid // And a DataGridView is focused and valid.
  19. || IsComboBoxEditingControl // Or a DataGridViewComboBoxEditingControl is focused.
  20. )
  21. )
  22. {
  23. int CurrentRowIndex = dataGridViewDMSSettings.CurrentCell.RowIndex; // Gets the current rows index.
  24. int LastRowIndex = dataGridViewDMSSettings.RowCount - 1; // Gets the last rows index.
  25. if (IsComboBoxEditingControl) // If a DataGridViewComboBoxEditingControl is focused.
  26. {
  27. ((DataGridViewComboBoxEditingControl)CurrentControl).EditingControlDataGridView.EndEdit(); // Ends the editing mode for the control.
  28. }
  29. int NextRowIndex = CurrentRowIndex; // Creates a variable for the next rows index.
  30. switch (keyData) // Switches through the pressed keys.
  31. {
  32. case (Keys.Tab): // If the Tab key is pressed.
  33. if (IsComboBoxEditingControl) // If a DataGridViewComboBoxEditingControl is focused.
  34. {
  35. NextRowIndex++; // Sets the next row index to the next row.
  36. if (NextRowIndex > LastRowIndex) // If next row index is greater than the last row index.
  37. {
  38. NextRowIndex = 0; // Focuses the first row. (Alternatively call "base.ProcessCmdKey(ref msg, keyData)" to exit the DataGridView)
  39. }
  40. }
  41. break;
  42. case (Keys.Shift | Keys.Tab): // If Shift and Tab key is pressed.
  43. NextRowIndex--; // Sets the next row index to the previous row.
  44. if (NextRowIndex < 0) // If previous row index is smaller than the first row index.
  45. {
  46. NextRowIndex = LastRowIndex; // Focuses the last row. (Alternatively call "base.ProcessCmdKey(ref msg, keyData)" to exit the DataGridView)
  47. }
  48. break;
  49. }
  50. dataGridViewDMSSettings.CurrentCell = dataGridViewDMSSettings.Rows[NextRowIndex].Cells["DMSIndex"]; // Sets the focus on the next DataGridViewComboBox.
  51. this.Refresh(); // Rerenders the current form.
  52. return true; // Returns true to the caller.
  53. }
  54. else // If another key (Not a Tab key) is pressed or the currently focused control is not a DataGridView or DataGridViewComboBoxEditing control.
  55. {
  56. return base.ProcessCmdKey(ref msg, keyData); // Performs the standard ProcessCmdKey method.
  57. }
  58. }
  59.  
  60. this.Refresh();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement