Example #1
0
        private bool Validate(IValidatableObject character)
        {
            var results = ObjectValidator.TryValidateObject(character);

            if (results.Count() > 0)
            {
                //if (!String.IsNullOrEmpty(message))
                foreach (var result in results)
                {
                    MessageBox.Show(this, result.ErrorMessage,
                                    "Error", MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                }
                ;
                return(false);
            }
            ;

            return(true);
        }
Example #2
0
        private void OnSave(object sender, EventArgs e)
        {
            if (!ValidateChildren())
            {
                DialogResult = DialogResult.None;
                return;
            }
            ;
            var character = new Character();

            character.Name         = _txtName.Text;
            character.Profession   = _CbProfession.Text;
            character.Race         = _CbRace.Text;
            character.Strength     = (int)_txtStrength.Value;
            character.Intelligence = (int)_txtIntelligence.Value;
            character.Agility      = (int)_txtAgility.Value;
            character.Constitution = (int)_txtConstitution.Value;
            character.Charisma     = (int)_txtCharisma.Value;
            character.Description  = _txtDescription.Text;

            // Validation
            var validationResults = new ObjectValidator().TryValidateFullobject(character);

            if (validationResults.Count() > 0)
            {
                var builder = new System.Text.StringBuilder();
                foreach (var result in validationResults)
                {
                    builder.AppendLine(result.ErrorMessage);
                }
                ;
                // Show error message
                MessageBox.Show(this, builder.ToString(), "Save failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
                DialogResult = DialogResult.None;
                return;
            }
            ;
            SelectedCharacter = character;
            Close();
        }