November 4, 2007

Using KeyDown Event On A Grid/List Control

This code adds keyboard handling to a grid or list control. The Delete Key performs a Delete, the Insert Key performs an Insert, the Return Key performs a an edit when a single row is selected

private void xxxxxxxxxxxxx_KeyDown(object sender, KeyEventArgs e)
{
   switch (e.KeyCode)
   {
       // Delete with one or more rows selected 
       // performs a Remove on those rows
       case Keys.Delete: 
         e.Handled = true;
         DeleteSelectedRows();
           break;
       case Keys.Insert: // Insert performs an Add
         e.Handled = true;
         AddRow();
           break;
       // RETURN When a single row is selected 
       // perform an Edit, editing the currently 
       // selected row
       case Keys.Return: 
           
         e.Handled = true;
         EditSelectedRow());
           break;
       default:
           break;
   }
}

No comments: