Advertisement
Guest User

BrowseContactsForm

a guest
Jun 4th, 2021
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.87 KB | None | 0 0
  1. /*
  2.  * Created by SharpDevelop.
  3.  * User: Admin
  4.  * Date: 4.6.2021 г.
  5.  * Time: 9:11
  6.  *
  7.  * To change this template use Tools | Options | Coding | Edit Standard Headers.
  8.  */
  9. using System;
  10. using System.Drawing;
  11. using System.Reflection;
  12. using System.Windows.Forms;
  13. using System.IO;
  14. using System.Collections.Generic;
  15. using System.Linq;
  16. using System.Text.RegularExpressions;
  17.  
  18.  
  19. namespace addressBook_11a
  20. {
  21.     /// <summary>
  22.     /// Description of BrowseContacts.
  23.     /// </summary>
  24.     public partial class BrowseContacts : Form
  25.     {
  26.        
  27.         public List<Contact> allContacts = new List<Contact>();
  28.         public int index = 0;
  29.        
  30.         public BrowseContacts()
  31.         {
  32.             //
  33.             // The InitializeComponent() call is required for Windows Forms designer support.
  34.             //
  35.             InitializeComponent();
  36.            
  37.             //
  38.             // TODO: Add constructor code after the InitializeComponent() call.
  39.             //
  40.         }
  41.        
  42.         void BrowseContactsLoad(object sender, EventArgs e)
  43.         {
  44.             var contactsInfo=File.ReadAllLines("contacts.csv");
  45.             foreach (var element in contactsInfo)
  46.             {
  47.                 string name = element.Split(',')[0];
  48.                 string phone = element.Split(',')[1];
  49.                 string email = element.Split(',')[2];
  50.                
  51.                 var newContact = new Contact(name, phone, email);
  52.                
  53.                 allContacts.Add(newContact);
  54.             }
  55.            
  56.            
  57.             textBoxName.Text = allContacts[0].Name;
  58.             textBoxPhone.Text = allContacts[0].Phone;
  59.             textBoxEmail.Text = allContacts[0].Email;
  60.            
  61.         }
  62.        
  63.        
  64.         void ButtonPreviousClick(object sender, EventArgs e)
  65.         {
  66.             if (index-1 < 0)
  67.             {
  68.                 MessageBox.Show("Няма повече контакти");
  69.                 return;
  70.             }
  71.            
  72.             index--;
  73.             textBoxName.Text = allContacts[index].Name;
  74.             textBoxPhone.Text = allContacts[index].Phone;
  75.             textBoxEmail.Text = allContacts[index].Email;
  76.         }
  77.        
  78.         void ButtonNextClick(object sender, EventArgs e)
  79.         {
  80.             if (index+1 == allContacts.Count)
  81.             {
  82.                 MessageBox.Show("Няма повече контакти");
  83.                 return;
  84.             }
  85.            
  86.             index++;
  87.             textBoxName.Text = allContacts[index].Name;
  88.             textBoxPhone.Text = allContacts[index].Phone;
  89.             textBoxEmail.Text = allContacts[index].Email;
  90.         }
  91.        
  92.        
  93.         void ButtonDeleteClick(object sender, EventArgs e)
  94.         {
  95.             var userAnswer = MessageBox
  96.                 .Show("Сигурни ли сте, че искате да изтриете контакта?",
  97.                 "Внимание !", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
  98.                
  99.                 if (userAnswer==DialogResult.No)
  100.                 {
  101.                     return;
  102.                 }
  103.                
  104.             allContacts.RemoveAt(index);
  105.             if (index > 0) index--;
  106.             textBoxName.Text = "";
  107.             textBoxPhone.Text = "";
  108.             textBoxEmail.Text = "";
  109.             MessageBox.Show("Контакта е изтрит");
  110.         }
  111.        
  112.         void ButtonUpdateClick(object sender, EventArgs e)
  113.         {
  114.             string name=textBoxName.Text;
  115.             string phone=textBoxPhone.Text;
  116.             string email=textBoxEmail.Text;
  117.            
  118.             if (name=="")
  119.             {
  120.                 MessageBox.Show("Въведи име на контакт");
  121.                 return;
  122.             }
  123.            
  124.            
  125.             string phoneString=@"^(0|\+359)[0-9]{9}$";
  126.             Regex rPhone=new Regex(phoneString);
  127.             if (phone != "" && !rPhone.IsMatch(phone))
  128.             {
  129.                 MessageBox.Show("Въведете валиден телефон");
  130.                 return;
  131.             }
  132.            
  133.            
  134.             string emailString=@"^[a-z0-9_ \.]+@[a-z0-9_ \.]+\.[a-z]{2,}$";
  135.             Regex rEmail=new Regex(emailString);
  136.             if (email != "" && !rEmail.IsMatch(email))
  137.             {
  138.                 MessageBox.Show("Въведете валиден e-mail");
  139.                 return;
  140.             }
  141.            
  142.            
  143.             allContacts[index].Name = name;
  144.             allContacts[index].Phone = phone;
  145.             allContacts[index].Email = email;
  146.             MessageBox.Show("Данните са записани");
  147.            
  148.         }
  149.        
  150.         void BrowseContactsFormClosing(object sender, FormClosingEventArgs e)
  151.         {
  152.             string result="";
  153.            
  154.             foreach (var element in allContacts)
  155.             {
  156.                 result += element.Print();
  157.             }
  158.            
  159.             File.WriteAllText("contacts.csv", result);
  160.         }
  161.    
  162.        
  163.        
  164.    
  165.        
  166.     }
  167. }
  168.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement