Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Created by SharpDevelop.
- * User: Admin
- * Date: 4.6.2021 г.
- * Time: 9:11
- *
- * To change this template use Tools | Options | Coding | Edit Standard Headers.
- */
- using System;
- using System.Drawing;
- using System.Reflection;
- using System.Windows.Forms;
- using System.IO;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text.RegularExpressions;
- namespace addressBook_11a
- {
- /// <summary>
- /// Description of BrowseContacts.
- /// </summary>
- public partial class BrowseContacts : Form
- {
- public List<Contact> allContacts = new List<Contact>();
- public int index = 0;
- public BrowseContacts()
- {
- //
- // The InitializeComponent() call is required for Windows Forms designer support.
- //
- InitializeComponent();
- //
- // TODO: Add constructor code after the InitializeComponent() call.
- //
- }
- void BrowseContactsLoad(object sender, EventArgs e)
- {
- var contactsInfo=File.ReadAllLines("contacts.csv");
- foreach (var element in contactsInfo)
- {
- string name = element.Split(',')[0];
- string phone = element.Split(',')[1];
- string email = element.Split(',')[2];
- var newContact = new Contact(name, phone, email);
- allContacts.Add(newContact);
- }
- textBoxName.Text = allContacts[0].Name;
- textBoxPhone.Text = allContacts[0].Phone;
- textBoxEmail.Text = allContacts[0].Email;
- }
- void ButtonPreviousClick(object sender, EventArgs e)
- {
- if (index-1 < 0)
- {
- MessageBox.Show("Няма повече контакти");
- return;
- }
- index--;
- textBoxName.Text = allContacts[index].Name;
- textBoxPhone.Text = allContacts[index].Phone;
- textBoxEmail.Text = allContacts[index].Email;
- }
- void ButtonNextClick(object sender, EventArgs e)
- {
- if (index+1 == allContacts.Count)
- {
- MessageBox.Show("Няма повече контакти");
- return;
- }
- index++;
- textBoxName.Text = allContacts[index].Name;
- textBoxPhone.Text = allContacts[index].Phone;
- textBoxEmail.Text = allContacts[index].Email;
- }
- void ButtonDeleteClick(object sender, EventArgs e)
- {
- var userAnswer = MessageBox
- .Show("Сигурни ли сте, че искате да изтриете контакта?",
- "Внимание !", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
- if (userAnswer==DialogResult.No)
- {
- return;
- }
- allContacts.RemoveAt(index);
- if (index > 0) index--;
- textBoxName.Text = "";
- textBoxPhone.Text = "";
- textBoxEmail.Text = "";
- MessageBox.Show("Контакта е изтрит");
- }
- void ButtonUpdateClick(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;
- }
- allContacts[index].Name = name;
- allContacts[index].Phone = phone;
- allContacts[index].Email = email;
- MessageBox.Show("Данните са записани");
- }
- void BrowseContactsFormClosing(object sender, FormClosingEventArgs e)
- {
- string result="";
- foreach (var element in allContacts)
- {
- result += element.Print();
- }
- File.WriteAllText("contacts.csv", result);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement