/// <summary> /// Called when the mouse is down on a cell. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ClassroomData_Cell_Mouse_Down(object sender, DataGridViewCellMouseEventArgs e) { if (e.Button == MouseButtons.Right && e.RowIndex >= 0 && e.ColumnIndex >= 0) { //Set the selected cell. ClassroomData.CurrentCell = ClassroomData.Rows[e.RowIndex].Cells[e.ColumnIndex]; //Create a Context Menu. ContextMenu menu = new ContextMenu(); //Create the MenuItems MenuItem desk = new MenuItem("Desk"); MenuItem addStudent = new MenuItem("Add Student"); //Add the MenuItems to the Context Menu. menu.MenuItems.Add(desk); menu.MenuItems.Add("-"); menu.MenuItems.Add(addStudent); desk.Click += new System.EventHandler(ContextMenu_Click); //If there are students in the class. if (classroom.students.Count > 0) { //For each student in the class. for (int i = 0; i < classroom.students.Count; i++) { //Create the Menuitem. MenuItem student = new MenuItem(classroom.students[i].name); //Add the student to the menu. addStudent.MenuItems.Add(student); //Add the Event Handler. student.Click += new System.EventHandler(ContextMenu_AddStudent_Click); } } else { addStudent.Enabled = false; } //The location of the menu. Point location = ClassroomData.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).Location; location.X = location.X + 25; location.Y = location.Y + 25; //Show the Context Menu. menu.Show(ClassroomData, location); } }
/// <summary> /// KeyBoard Shortcuts. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Main_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { //Ctrl + O if (e.KeyCode == Keys.O && e.Modifiers == Keys.Control) { //Show the Open Dialog. File_Open_Click(sender, e); } //Ctrl + S else if (e.KeyCode == Keys.S && e.Modifiers == Keys.Control) { //Show the Save Dialog. File_Save_Click(sender, e); } //Ctrl + Shift + S else if (e.KeyCode == Keys.S && e.Modifiers == (Keys.Control | Keys.Shift)) { //Show the Save Dialog. File_SaveAs_Click(sender, e); } else if (e.KeyCode == Keys.Space && ClassroomData.SelectedCells.Count == 1) { //Create a Context Menu. ContextMenu menu = new ContextMenu(); //Create the MenuItems MenuItem desk = new MenuItem("Desk"); MenuItem addStudent = new MenuItem("Add Student"); //Add the MenuItems to the Context Menu. menu.MenuItems.Add(desk); menu.MenuItems.Add("-"); menu.MenuItems.Add(addStudent); addStudent.MenuItems.Add(new MenuItem("Nathan")); Point location = ClassroomData.GetCellDisplayRectangle(ClassroomData.SelectedCells[0].ColumnIndex, ClassroomData.SelectedCells[0].RowIndex, false).Location; location.X = location.X + 25; location.Y = location.Y + 25; //Show the Context Menu. menu.Show(ClassroomData, location); } }