// When we are in edit mode we have to populate many of the controls in the form because // we must show the information from a character that has already been created. If we are // not in edit mode then none of these controls have to be populated because we are creating // character from scratch private void CharacterEditForm_Load(object sender, EventArgs e) { if (editMode == true) { this.txtCharacterName.Text = _characterManager.CharacterList[_characterIndex].Name; this.txtCharacterSize.Text = _characterManager.CharacterList[_characterIndex].Size; this.txtCharacterType.Text = _characterManager.CharacterList[_characterIndex].Type; this.txtCharacterAllignment.Text = _characterManager.CharacterList[_characterIndex].Allignment; this.txtCharacterAffiliation.Text = _characterManager.CharacterList[_characterIndex].Affiliation; // Here we display and image, but we must specify a file name within which to save it picFilename = _characterManager.CharacterList[_characterIndex].ImageFileName; picCharacter.Image = Image.FromFile(dataPath + @"\" + picFilename); // We use the character stat block file name to cell up the character stat block this.txtStatBlockFilename.Text = _characterManager.CharacterList[_characterIndex].StatBlockFileName; CharacterStatBlock characterStatBlock = _characterManager.FetchCharacterStatBlock(_characterIndex); this.txtStatBlock.Text = characterStatBlock.StatBlockText; this.txtStatBlockFilename.Enabled = false; this.Text = "Character Editor"; } else { this.Text = "Add a Charecter"; } }
} // End RetrieveCharacterStatBlock() // This method takes an updated Character object and CharacterStatBlock object and uses it to overwrite the current // files, effectively updating them. public static bool UpdateCharacter(int characterIndex, CharacterStatBlock characterStatBlock, List <Character> characterList) { try { // This overwrites the currently held StatBlock text file of the chosen character with the updated one. StreamWriter fileWriter = new StreamWriter(AppData.DataPath + @"\" + characterList[characterIndex].StatBlockFileName); fileWriter.WriteLine(characterStatBlock.CharacterName); fileWriter.WriteLine(characterStatBlock.StatBlockText); fileWriter.Close(); // This overwrites the currentlyheld CharacterList.csv file with updated object information. Everytime // this update is done the entire csv is overwritten StreamWriter fileWriter2 = new StreamWriter(AppData.DataPath + @"\" + AppData.CharacterListFileName); foreach (Character character in characterList) { fileWriter2.WriteLine(character.Name + "," + character.Size + "," + character.Type + "," + character.Allignment + "," + character.Affiliation + "," + character.StatBlockFileName + "," + character.ImageFileName); } fileWriter2.Close(); } catch (Exception) // If there is an exception the method returns false. { return(false); } return(true); // If the method runs successfully it returns true. } // End UpdateCharacter
} // End UpdateCharacter // This method uses a Character object to save information to the charcterList.csv file. It also // uses some text entered by the user to create a new charcter stat block file. public static bool AddCharacter(Character character, CharacterStatBlock characterStatBlock) { try { // This adds a new stat block text file to a user chosen filename. StreamWriter fileWriter = new StreamWriter(AppData.DataPath + @"\" + character.StatBlockFileName); fileWriter.WriteLine(characterStatBlock.CharacterName); fileWriter.Write(characterStatBlock.StatBlockText); fileWriter.Close(); // This adds a new character to the CharacterList.csv file. StreamWriter fileWriter2 = new StreamWriter(AppData.DataPath + @"\" + AppData.CharacterListFileName, true); fileWriter2.WriteLine(character.Name + "," + character.Size + "," + character.Type + "," + character.Allignment + "," + character.Affiliation + "," + character.StatBlockFileName + "," + character.ImageFileName); fileWriter2.Close(); } catch (Exception) // If there is an exception the method returns false. { return(false); } return(true); // If the method runs successfully it returns true. } // End AddCharacter()
// This method displays the stat block of a character that is either chosen from the previous // screen or that comes up by clicking the next button private void DisplayStatBlock() { try { _characterStatBlock = _characterManager.FetchCharacterStatBlock(_characterIndex); this.lblTitle.Text = _characterStatBlock.CharacterName; this.txtStatBlock.Text = _characterStatBlock.StatBlockText; this.picCharacterImage.Image = Image.FromFile(AppData.DataPath + @"\" + _characterManager.CharacterList[_characterIndex].ImageFileName); this.btnNext.Select(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
} // End EditCharacter() // This method takes a character that will be added to the characterlist.csv file and a statblock that will // saved to a a seperate file. public List <Character> AddCharacter(Character character, CharacterStatBlock characterStatBlock) { try { if (CharacterDataAccessor.AddCharacter(character, characterStatBlock)) { _characterList = CharacterDataAccessor.RetrieveCharacterList(); } return(_characterList); } catch (Exception) { throw; } } // End AddCharacter()
} // End of FetchCharacterStatBlock() // This method will take a character and a stat block to replace on that is already in the character list // at the index that is entered. public List <Character> EditCharacter(Character character, CharacterStatBlock characterStatBlock, int characterIndex) { try { _characterList[characterIndex] = character; if (CharacterDataAccessor.UpdateCharacter(characterIndex, characterStatBlock, _characterList)) { _characterList = CharacterDataAccessor.RetrieveCharacterList(); return(_characterList); } return(_characterList); } catch (Exception) { throw; } } // End EditCharacter()
} // End RetrieveCharacterList() // This method reads from a csv file to instantiate a new instance of a StatBlock type object. public static CharacterStatBlock RetrieveCharacterStatBlock(Character character) { CharacterStatBlock newCharacterStatBlock = new CharacterStatBlock(); try { // This reads from a csv file and uses it to create a new StatBlock object StreamReader fileReader = new StreamReader(AppData.DataPath + @"\" + character.StatBlockFileName); newCharacterStatBlock.CharacterName = fileReader.ReadLine(); newCharacterStatBlock.StatBlockText = fileReader.ReadToEnd(); fileReader.Close(); } catch (Exception) // if an exception occurs this will throw it up towards the presentation layer { throw; } return(newCharacterStatBlock); } // End RetrieveCharacterStatBlock()
// Here we gather all the data from the various text boxes and use it to // create a new instantiation of the Character class private void btnSave_Click(object sender, EventArgs e) { // Here we do some checks to make sure the user has not left any fields blank. if (txtCharacterName.Text == "") { MessageBox.Show("You need to enter a character name."); txtCharacterName.Focus(); return; } if (txtCharacterSize.Text == "") { MessageBox.Show("You need to enter a character size."); txtCharacterSize.Focus(); return; } if (txtCharacterType.Text == "") { MessageBox.Show("You need to enter a character type."); txtCharacterType.Focus(); return; } if (txtCharacterAllignment.Text == "") { MessageBox.Show("You need to enter a character allignment."); txtCharacterAllignment.Focus(); return; } if (txtCharacterAffiliation.Text == "") { MessageBox.Show("You need to enter a character affiliation."); txtCharacterAffiliation.Focus(); return; } if (picCharacter.Image == null) { MessageBox.Show("You need to choose a character image."); btnChooseImage.Focus(); return; } if (txtStatBlock.Text == "") { MessageBox.Show("You need to enter the stat block text."); txtStatBlock.Focus(); return; } if (txtStatBlockFilename.Text == "") { MessageBox.Show("You need to enter a stat block filename."); txtStatBlockFilename.Focus(); return; } if ((txtStatBlockFilename.Text).ToLower().EndsWith(".txt") == false) { MessageBox.Show("Your filename must end with a '.txt'"); txtStatBlockFilename.Focus(); return; } if (txtStatBlockFilename.Text.Length < 5) { MessageBox.Show("Invalid file name."); txtStatBlockFilename.Focus(); return; } if (System.IO.File.Exists(dataPath + @"\" + txtStatBlockFilename.Text) && editMode == false) { MessageBox.Show("File already exists."); txtStatBlockFilename.Focus(); return; } // When all the data has been entered we can use it to create objects to be saved var character = new Character(); character.Name = txtCharacterName.Text; character.Size = txtCharacterSize.Text; character.Type = txtCharacterType.Text; character.Allignment = txtCharacterAllignment.Text; character.Affiliation = txtCharacterAffiliation.Text; character.ImageFileName = picFilename; character.StatBlockFileName = txtStatBlockFilename.Text; var characterStatBlock = new CharacterStatBlock(); characterStatBlock.CharacterName = txtCharacterName.Text; characterStatBlock.StatBlockText = txtStatBlock.Text; if (editMode == true) // saves over existing records { _characterManager.EditCharacter(character, characterStatBlock, _characterIndex); this.Close(); } else // adds new records { int characterCount = _characterManager.CharacterList.Count; var newList = _characterManager.AddCharacter(character, characterStatBlock); if (newList.Count > characterCount) { MessageBox.Show("New character added."); this.Close(); } else { MessageBox.Show("Add failed"); this.Close(); } } } // End of btnSave_Click