Beispiel #1
0
        private void LoadComponents()
        {
            Contact.Contacts.Clear();

            if (File.Exists(@"..\..\contacts.txt"))
            {
                var reader = new StreamReader(@"..\..\contacts.txt");

                try
                {
                    using (reader)
                    {
                        var serializedPersons = reader.ReadToEnd();
                        var persons           = serializedPersons.Split(new string[] { Environment.NewLine },
                                                                        StringSplitOptions.RemoveEmptyEntries);
                        foreach (var person in persons)
                        {
                            if (person.StartsWith(ContactGroup.Family.ToString()))
                            {
                                Contact contact = new FamillyContact();
                                Contact.AddContact(contact.Deserialize(person));
                            }
                            else if (person.StartsWith(ContactGroup.Business.ToString()))
                            {
                                Contact contact = new BusinessContact();
                                Contact.AddContact(contact.Deserialize(person));
                            }
                            else if (person.StartsWith(ContactGroup.Friends.ToString()))
                            {
                                Contact contact = new FriendContact();
                                Contact.AddContact(contact.Deserialize(person));
                            }
                            else if (person.StartsWith(ContactGroup.Other.ToString()))
                            {
                                Contact contact = new OtherContact();
                                Contact.AddContact(contact.Deserialize(person));
                            }
                            else
                            {
                                continue;
                            }
                        }
                    }
                }
                catch (FileLoadException ex) { MessageBox.Show(ex.Message); }
                catch (IOException ex) { MessageBox.Show(ex.Message); }
            }
            else
            {
                try { File.Create(@"..\..\contacts.txt"); }
                catch (PathTooLongException ex) { MessageBox.Show(ex.Message); }
                catch (DirectoryNotFoundException ex) { MessageBox.Show(ex.Message); }
                catch (UnauthorizedAccessException ex) { MessageBox.Show(ex.Message); }
                catch (IOException ex) { MessageBox.Show(ex.Message); }
            }

            GroupedContacts = new ListCollectionView(Contact.Contacts);
            GroupedContacts.GroupDescriptions.Add(new PropertyGroupDescription("Group"));
            GroupedContacts.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
            preview.ItemsSource = GroupedContacts;
        }
Beispiel #2
0
        private void OnAddBtnClick(object sender, RoutedEventArgs e)
        {
            DateTime date;
            bool     isDate = DateTime.TryParse(this.PersonBirthday.Text, out date);

            try
            {
                if (this.PersonName.Text.Trim() == "")
                {
                    throw new AddressBookException("Name is mandatory!", new InvalidNameException());
                }
                if (this.Mobile.Text.Trim() == "")
                {
                    throw new AddressBookException("Mobile phone is mandatory!", new InvalidPhoneException());
                }
                if (this.PersonBirthday.Text.Trim() != "")
                {
                    if (!isDate)
                    {
                        throw new InvalidDateException("Enter correct date!");
                    }
                }
                if (this.Mobile.Text.Trim() != null)
                {
                    string mobile = this.Mobile.Text.Trim();
                    for (int i = 0; i < mobile.Length; i++)
                    {
                        if (!char.IsDigit(mobile[i]))
                        {
                            throw new InvalidPhoneException("Enter valid mobile number!");
                        }
                    }
                }

                if (GroupType.SelectionBoxItem.ToString() == "Family")
                {
                    Contact newContact = new FamillyContact(this.PersonName.Text.Trim(), this.Mobile.Text.Trim(),
                                                            this.PersonEmail.Text.Trim(), date == DateTime.MinValue ? (DateTime?)null : date);
                    (GroupedContacts as ListCollectionView).AddNewItem(newContact);
                }
                else if (GroupType.SelectionBoxItem.ToString() == "Friends")
                {
                    Contact newContact = new FriendContact(this.PersonName.Text.Trim(), this.Mobile.Text.Trim(),
                                                           this.PersonEmail.Text.Trim(), date == DateTime.MinValue ? (DateTime?)null : date);
                    (GroupedContacts as ListCollectionView).AddNewItem(newContact);
                }
                else if (GroupType.SelectionBoxItem.ToString() == "Business")
                {
                    Contact newContact = new BusinessContact(this.PersonName.Text.Trim(), this.Mobile.Text.Trim(),
                                                             this.PersonEmail.Text.Trim(), date == DateTime.MinValue ? (DateTime?)null : date);
                    (GroupedContacts as ListCollectionView).AddNewItem(newContact);
                }
                else
                {
                    Contact newContact = new OtherContact(this.PersonName.Text.Trim(), this.Mobile.Text.Trim(),
                                                          this.PersonEmail.Text.Trim(), date == DateTime.MinValue ? (DateTime?)null : date);
                    (GroupedContacts as ListCollectionView).AddNewItem(newContact);
                }

                (GroupedContacts as ListCollectionView).CommitNew();

                this.PersonName.Text     = "";
                this.PersonEmail.Text    = "";
                this.PersonBirthday.Text = "";
                this.Mobile.Text         = "";
            }
            catch (InvalidDateException ex) { MessageBox.Show(ex.Message); }
            catch (AddressBookException ex) { MessageBox.Show(ex.Message); }
        }