Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Created by SharpDevelop.
- * User: Admin
- * Date: 26.5.2021 г.
- * Time: 10:10
- *
- * To change this template use Tools | Options | Coding | Edit Standard Headers.
- */
- using System;
- using System.Drawing;
- using System.Windows.Forms;
- using System.Text.RegularExpressions;
- using System.IO;
- using System.Linq;
- namespace addressBook_11a
- {
- /// <summary>
- /// Description of AddContact.
- /// </summary>
- public partial class AddContact : Form
- {
- public AddContact()
- {
- //
- // The InitializeComponent() call is required for Windows Forms designer support.
- //
- InitializeComponent();
- //
- // TODO: Add constructor code after the InitializeComponent() call.
- //
- }
- void ButtonAddClick(object sender, EventArgs e)
- {
- string name=textBoxName.Text;
- string phone=textBoxPhone.Text;
- string email=textBoxEmail.Text;
- if (name=="")
- {
- MessageBox.Show("Въведи име на контакт");
- return;
- }
- string phoneString=@"^(0|\+359)[0-9]{9}$";
- Regex rPhone=new Regex(phoneString);
- if (phone != "" && !rPhone.IsMatch(phone))
- {
- MessageBox.Show("Въведете валиден телефон");
- return;
- }
- string emailString=@"^[a-z0-9_ \.]+@[a-z0-9_ \.]+\.[a-z]{2,}$";
- Regex rEmail=new Regex(emailString);
- if (email != "" && !rEmail.IsMatch(email))
- {
- MessageBox.Show("Въведете валиден e-mail");
- return;
- }
- var contact = new Contact(name, phone, email);
- //check for the file
- if (!File.Exists("contacts.csv"))
- {
- var f = File.Create("contacts.csv");
- f.Close();
- }
- //add contact to a file
- var allContacts=File.ReadAllLines("contacts.csv");
- if (allContacts.Any(c => c.Contains(name)))
- {
- var userAnswer = MessageBox
- .Show("Вече съществува контакт с това има. Искате ли да го добавите като нов?",
- "Внимание !", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
- if (userAnswer==DialogResult.No)
- {
- return;
- }
- }
- File.AppendAllText("contacts.csv", contact.Print());
- MessageBox.Show("Контакта е добавен");
- textBoxName.Text="";
- textBoxPhone.Text="";
- textBoxEmail.Text="";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement