/// <summary> /// opens the CharacterForm after user selects the character and chooses the appropriate menu item or hotkey /// </summary> private void OnGameEdit(object sender, EventArgs e) { // opens character form var form = new CharacterForm(); // change title of Form to edit form.Text = "Edit Character"; //make sure we're editing appropriate Character, if no Character, return var character = GetSelectedCharacter(); if (character == null) { return; } // Character to edit form.Character = character; while (true) { if (form.ShowDialog(this) != DialogResult.OK) { return; } // try to update the Character try { _characters.Update(character.Id, form.Character); break; } catch (Exception ex) { DisplayError(ex); }; } ; BindList(); }
/// <summary> /// /// </summary> private void OnSafeAdd(CharacterForm form) { try { // adds Character from Character form _characters.Add(form.Character); } catch (NotImplementedException e) { //Rewriting an exception throw new Exception("Not implemented yet", e); } catch (Exception e) { //Log a message //Rethrow exception - wrong way //throw e; throw; }; }
/// <summary> /// opens the Character Form to create a new Character /// </summary> private void OnCharacterNew(object sender, EventArgs e) { // display UI var form = new CharacterForm(); // modal while (true) { //Modal if (form.ShowDialog(this) != DialogResult.OK) { return; } //Add try { //Anything in here that raises an exception will //be sent to the catch block OnSafeAdd(form); break; } //catch (InvalidOperationException) //{ // MessageBox.Show(this, "Choose a better game.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); //} catch (Exception ex) { //Recover from errors DisplayError(ex); }; } ; // bind list to ListBox BindList(); }