Advertisement
Guest User

SearchContactForm

a guest
Jun 9th, 2021
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.23 KB | None | 0 0
  1. /*
  2.  * Created by SharpDevelop.
  3.  * User: Admin
  4.  * Date: 9.6.2021 г.
  5.  * Time: 9:58
  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.Linq;
  13. using System.Collections.Generic;
  14. using System.IO;
  15.  
  16. namespace addressBook_11a
  17. {
  18.     /// <summary>
  19.     /// Description of SearchForm.
  20.     /// </summary>
  21.     public partial class SearchForm : Form
  22.     {
  23.        
  24.         List<Contact> allContacts = new List<Contact>();
  25.        
  26.         public SearchForm()
  27.         {
  28.             //
  29.             // The InitializeComponent() call is required for Windows Forms designer support.
  30.             //
  31.             InitializeComponent();
  32.            
  33.             //
  34.             // TODO: Add constructor code after the InitializeComponent() call.
  35.             //
  36.         }
  37.         void SearchFormLoad(object sender, EventArgs e)
  38.         {
  39.             var contactsInfo=File.ReadAllLines("contacts.csv");
  40.             foreach (var element in contactsInfo)
  41.             {
  42.                 string name = element.Split(',')[0];
  43.                 string phone = element.Split(',')[1];
  44.                 string email = element.Split(',')[2];
  45.                
  46.                 var newContact = new Contact(name, phone, email);
  47.                
  48.                 allContacts.Add(newContact);
  49.             }
  50.         }
  51.         void TextBoxSearchTextChanged(object sender, EventArgs e)
  52.         {
  53.             string userSearch = textBoxSearch.Text;        
  54.             string searchType = comboBoxSearchType.Text;
  55.             string result = "";
  56.             List<Contact> filtered=new List<Contact>();
  57.            
  58.             if (searchType == "")
  59.             {
  60.                 MessageBox.Show("Изберете тип на търсене");
  61.                 return;
  62.             }
  63.            
  64.             if (searchType == "по име")
  65.             {
  66.                 filtered = allContacts
  67.                     .Where(c => c.Name.ToLower()
  68.                            .Contains(userSearch.ToLower()))
  69.                     .ToList();
  70.             }
  71.            
  72.             if (searchType == "по e-mail")
  73.             {
  74.                 filtered = allContacts
  75.                     .Where(c => c.Email.Contains(userSearch))
  76.                     .ToList();
  77.             }
  78.            
  79.             if (searchType == "по телефон")
  80.             {
  81.                 filtered = allContacts
  82.                     .Where(c => c.Phone.StartsWith(userSearch))
  83.                     .ToList();
  84.             }
  85.            
  86.            
  87.             foreach (var element in filtered)
  88.             {
  89.                 result += element.ToString();
  90.             }
  91.            
  92.             richTextBoxResult.Text = result;
  93.            
  94.         }
  95.         void ComboBoxSearchTypeSelectedIndexChanged(object sender, EventArgs e)
  96.         {
  97.             richTextBoxResult.Text="";
  98.         }
  99.     }
  100. }
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement