Advertisement
Guest User

Add Contact Button (Address Book)

a guest
Jun 3rd, 2021
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.34 KB | None | 0 0
  1. /*
  2.  * Created by SharpDevelop.
  3.  * User: Admin
  4.  * Date: 26.5.2021 г.
  5.  * Time: 10:10
  6.  *
  7.  * To change this template use Tools | Options | Coding | Edit Standard Headers.
  8.  */
  9. using System;
  10. using System.Drawing;
  11. using System.Windows.Forms;
  12. using System.Text.RegularExpressions;
  13. using System.IO;
  14. using System.Linq;
  15.  
  16. namespace addressBook_11a
  17. {
  18.     /// <summary>
  19.     /// Description of AddContact.
  20.     /// </summary>
  21.     public partial class AddContact : Form
  22.     {
  23.         public AddContact()
  24.         {
  25.             //
  26.             // The InitializeComponent() call is required for Windows Forms designer support.
  27.             //
  28.             InitializeComponent();
  29.            
  30.             //
  31.             // TODO: Add constructor code after the InitializeComponent() call.
  32.             //
  33.         }
  34.         void ButtonAddClick(object sender, EventArgs e)
  35.         {  
  36.            
  37.             string name=textBoxName.Text;
  38.             string phone=textBoxPhone.Text;
  39.             string email=textBoxEmail.Text;
  40.            
  41.             if (name=="")
  42.             {
  43.                 MessageBox.Show("Въведи име на контакт");
  44.                 return;
  45.             }
  46.            
  47.            
  48.             string phoneString=@"^(0|\+359)[0-9]{9}$";
  49.             Regex rPhone=new Regex(phoneString);
  50.             if (phone != "" && !rPhone.IsMatch(phone))
  51.             {
  52.                 MessageBox.Show("Въведете валиден телефон");
  53.                 return;
  54.             }
  55.            
  56.            
  57.             string emailString=@"^[a-z0-9_ \.]+@[a-z0-9_ \.]+\.[a-z]{2,}$";
  58.             Regex rEmail=new Regex(emailString);
  59.             if (email != "" && !rEmail.IsMatch(email))
  60.             {
  61.                 MessageBox.Show("Въведете валиден e-mail");
  62.                 return;
  63.             }
  64.            
  65.             var contact = new Contact(name, phone, email);     
  66.            
  67.            
  68.             //check for the file
  69.             if (!File.Exists("contacts.csv"))
  70.             {
  71.                 var f = File.Create("contacts.csv");
  72.                 f.Close();
  73.             }
  74.            
  75.             //add contact to a file
  76.             var allContacts=File.ReadAllLines("contacts.csv");
  77.             if (allContacts.Any(c => c.Contains(name)))
  78.             {
  79.                 var userAnswer = MessageBox
  80.                     .Show("Вече съществува контакт с това има. Искате ли да го добавите като нов?",
  81.                     "Внимание !", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
  82.                
  83.                 if (userAnswer==DialogResult.No)
  84.                 {
  85.                     return;
  86.                 }
  87.             }
  88.            
  89.             File.AppendAllText("contacts.csv", contact.Print());
  90.            
  91.             MessageBox.Show("Контакта е добавен");
  92.             textBoxName.Text="";
  93.             textBoxPhone.Text="";
  94.             textBoxEmail.Text="";
  95.            
  96.         }
  97.     }
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement