Exemplo n.º 1
0
        private Person HarvestData()
        {
            //Data Handler Variables
            var tempPerson         = new Person();
            var countOfValidFields = 0;
            //Set The Rules In One Place
            var requiredValidFields = 3;

            //Use Try Catch
            try
            {
                //Validate the name and assign if there
                if (!string.IsNullOrEmpty(PersonNameTextBox.Text))
                {
                    tempPerson.PersonName = PersonNameTextBox.Text;
                    countOfValidFields   += 1;
                }
                else
                {
                    MessageBox.Show("You must enter a name");
                }
                //Validate the Age and assign if there
                if (!string.IsNullOrEmpty(PersonAgeTextBox.Text))
                {
                    tempPerson.Age      = Convert.ToInt32(PersonAgeTextBox.Text);
                    countOfValidFields += 1;
                }
                else
                {
                    MessageBox.Show("You must enter a Age");
                }
                //Validate the Weight and assign if there
                if (!string.IsNullOrEmpty(PersonWeightTextBox.Text))
                {
                    tempPerson.Weight   = float.Parse(PersonWeightTextBox.Text);
                    countOfValidFields += 1;
                }
                else
                {
                    MessageBox.Show("You must enter a Weight");
                }
            }
            catch (Exception)
            {
                throw;
            }

            if (countOfValidFields == requiredValidFields)
            {
                formDataVaild = true;
                return(tempPerson);
            }
            else
            {
                MessageBox.Show("Form data is not valid");
                PersonNameTextBox.Focus();
            }

            return(tempPerson);
        }
Exemplo n.º 2
0
 private void ClearControls()
 {
     PersonNameTextBox.Text   = string.Empty;
     PersonAgeTextBox.Text    = string.Empty;
     PersonWeightTextBox.Text = string.Empty;
     PersonNameTextBox.Focus();
 }