private void btnAdd_Click(object sender, EventArgs e) { // Make sure the feedback/output labels is cleared from any previous attempts lblFeedback.Text = ""; lblOutput.Text = "Contact"; bool isValid = true; if (isValid) { // Check to see which Radio Button is selected if (radMr.Checked) { salutation = radMr.Text; } else if (radMrs.Checked) { salutation = radMrs.Text; } else { salutation = radMs.Text; } /* Loop through the list and add the list to the listbox on the form * lboxContacts.Items.Add(salutation + " " + txtFirstName.Text + " " + txtLastName.Text); * lboxContacts.Items.Add(txtStreet1.Text + " " + txtStreet2.Text); * lboxContacts.Items.Add(txtCity.Text + ", " + cmbState.SelectedItem.ToString() + ", " + txtZip.Text); * lboxContacts.Items.Add(txtEmail.Text); * lboxContacts.Items.Add(txtHomePhone.Text); * lboxContacts.Items.Add(txtWorkPhone.Text); * lboxContacts.Items.Add(txtCellPhone.Text); * lboxContacts.Items.Add(dtpBirthday.Value); * lboxContacts.Items.Add(dtpAnniversary.Value); * lboxContacts.Items.Add(chkCardWorthy.Text.ToString()); * lboxContacts.Items.Add(cmbRelationship.SelectedItem.ToString()); * * * DateTime dtBDay = new DateTime(); * // dtBDay = Convert.ToDateTime(dtpBirthday.Text); * dtBDay = dtpBirthday.Value; * * // lblFeedback.Text = dtBDay.ToShortDateString*/ } /**************************** FORM VALIDATION ********************************/ // First Name Validation if (!Validators.IsItFilledIn(txtFirstName.Text)) { isValid = false; lblFeedback.Text += "Error: Please enter a first name.\n"; } // Last Name Validation if (!Validators.IsItFilledIn(txtLastName.Text)) { isValid = false; lblFeedback.Text += "Error: Please enter a last name.\n"; } // State Validator if (cmbState.SelectedIndex == 0) { isValid = false; lblFeedback.Text += "Error: Please select a state from the dropdown menu.\n"; } // Zip Code Validation if (!Validators.IsValidZip(txtZip.Text)) { isValid = true; lblFeedback.Text += "Error: Must enter a valid zip code.\n"; } // Email Validation if (!Validators.IsValidEmail(txtEmail.Text)) { isValid = true; lblFeedback.Text += "Error: Please enter a valid email address.\n"; } // Home Phone Validator if (!Validators.IsValidPhoneNumber(txtHomePhone.Text)) { isValid = false; lblFeedback.Text += "Error: Please enter a valid home phone number.\n"; } // Work Phone Validator if (!Validators.IsValidPhoneNumber(txtWorkPhone.Text)) { isValid = false; lblFeedback.Text += "Error: Please enter a valid work phone number.\n"; } // Cell Phone Validator if (!Validators.IsValidPhoneNumber(txtCellPhone.Text)) { isValid = false; lblFeedback.Text += "Error: Please enter a valid cell phone number.\n"; } // Set values for both Checkbox States if (chkCardWorthy.Checked) { chkCardWorthy.Text = "Card Worthy"; } else { chkCardWorthy.Text = "Not Card Worthy"; } /****************************** Open/Append Data to File ******************************/ // create a string to gather the data // string contactRecord; // Start storing contact info contactRecord = DateTime.Now.ToShortDateString() + "," + txtFirstName.Text + "," + txtLastName.Text + "," + txtStreet1.Text + "," + txtStreet2.Text + "," + txtCity.Text + "," + cmbState.Text.ToString() + "," + txtZip.Text + "," + txtEmail.Text + "," + txtHomePhone.Text + "," + txtWorkPhone.Text + "," + txtCellPhone.Text + "," + dtpBirthday.Value.ToShortDateString() + "," + dtpAnniversary.Value.ToShortDateString() + "," + chkCardWorthy.Text.ToString() + "," + cmbRelationship.Text.ToString(); // Display Successful Storage Message lblOutput.Text = "Contact Stored Successfully"; // Display Contact Name in Output/Feedback Label // lblOutput.Text = DateTime.Now.ToShortDateString() + " " + txtFirstName.Text + " " + txtLastName.Text; // Display Contact Info in Output Textbox // lboxContacts.Items.Add(contactRecord); FileIO.writeFile(@"Contacts.csv", contactRecord); }
private void btnAdd_Click(object sender, EventArgs e) { // Make sure the feedback label is cleared from any previous attempts lblFeedback.Text = ""; bool isValid = true; if (isValid) { // Check to see which Radio Button is selected if (radMr.Checked) { salutation = radMr.Text; } else if (radMrs.Checked) { salutation = radMrs.Text; } else { salutation = radMs.Text; } // Loop through the list and add the list to the listbox on the form lboxContacts.Items.Add(salutation + " " + txtFirstName.Text + " " + txtLastName.Text); lboxContacts.Items.Add(txtStreet1.Text + " " + txtStreet2.Text); lboxContacts.Items.Add(txtCity.Text + ", " + cmbState.SelectedItem.ToString() + ", " + txtZip.Text); lboxContacts.Items.Add(txtEmail.Text); lboxContacts.Items.Add(txtHomePhone.Text); lboxContacts.Items.Add(txtWorkPhone.Text); lboxContacts.Items.Add(txtCellPhone.Text); lboxContacts.Items.Add(dtpBirthday.Value); lboxContacts.Items.Add(dtpAnniversary.Value); lboxContacts.Items.Add(chkCardWorthy.Text.ToString()); lboxContacts.Items.Add(cmbRelationship.SelectedItem.ToString()); lboxContacts.Items.Add(txtNotes.Text); DateTime dtBDay = new DateTime(); // dtBDay = Convert.ToDateTime(dtpBirthday.Text); dtBDay = dtpBirthday.Value; // lblFeedback.Text = dtBDay.ToShortDateString(); } /**************************** FORM VALIDATION ********************************/ // First Name Validation if (!Validators.IsItFilledIn(txtFirstName.Text)) { isValid = false; lblFeedback.Text += "Error: Please enter a first name.\n"; } // Last Name Validation if (!Validators.IsItFilledIn(txtLastName.Text)) { isValid = false; lblFeedback.Text += "Error: Please enter a last name.\n"; } /** Street 1 Validator * * if (!Validators.IsItFilledIn(txtStreet1.Text)) * { * isValid = true; * lblFeedback.Text += "Error: Please fill in a Street Address.\n"; * } * */ // State Validator if (cmbState.SelectedIndex == 0) { isValid = false; lblFeedback.Text += "Error: Please select a state from the dropdown menu.\n"; } // Zip Code Validation if (!Validators.IsValidZip(txtZip.Text)) { isValid = true; lblFeedback.Text += "Error: Must enter a valid zip code.\n"; } // Email Validation if (!Validators.IsValidEmail(txtEmail.Text)) { isValid = true; lblFeedback.Text += "Error: Please enter a valid email address.\n"; } // Home Phone Validator if (!Validators.IsValidPhoneNumber(txtHomePhone.Text)) { isValid = false; lblFeedback.Text += "Error: Please enter a valid home phone number.\n"; } // Work Phone Validator if (!Validators.IsValidPhoneNumber(txtWorkPhone.Text)) { isValid = false; lblFeedback.Text += "Error: Please enter a valid work phone number.\n"; } // Cell Phone Validator if (!Validators.IsValidPhoneNumber(txtCellPhone.Text)) { isValid = false; lblFeedback.Text += "Error: Please enter a valid cell phone number.\n"; } /** Relationship Validator * * if (cmbRelationship.SelectedIndex == 0) * { * isValid = false; * lblFeedback.Text += "Error: Please select a relationship from the dropdown menu.\n"; * } * */ }