private void addNew_Click(object sender, RoutedEventArgs e) { Friend friend = new Friend(); if (MessageBox.Show("Are you sure you'd like to add a new friend?", "Confirm Exit", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes) { friend.First = firstName.Text; friend.Last = lastName.Text; if (unknown.IsChecked == true) { friend.BirthYear = 1800; friend.BirthMonth = "Jan"; friend.BirthDay = 1; } else { friend.BirthYear = Convert.ToInt32(year.Text); friend.BirthMonth = month.Text; friend.BirthDay = Convert.ToInt32(day.Text); } if (religion.Text != null && religion.Text != "") { friend.Religion = religion.Text; } else { friend.Religion = "None Applied"; } friend.Rating = ratingSlider.Value; friend.HowMet = howWeMet.Text; friend.Details = details.Text; friend.FunnyStory = story.Text; friend.BornIn = bornIn.Text; friend.LivesIn = livesIn.Text; friend.MetIn = met.Text; if (friend.First != "" && friend.First != null && friend.Last != "" && friend.Last != null && friend.Details != null && friend.Details != "" && friend.LivesIn != "" && friend.LivesIn != null) { friend.Header = friend.First + " " + friend.Last; friendsTreeItem.Items.Add(friend); } else { MessageBox.Show("Some of your data is not filled in", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } } MessageBox.Show(friend.First + " " + friend.Last + " has been successfully added!", "Success!", MessageBoxButton.OK, MessageBoxImage.Information); }
private void save_Click(object sender, RoutedEventArgs e) { Friend selected = (Friend)friends.SelectedValue; Friend friend = new Friend(); if (selected.Header == "Friends") { MessageBox.Show("Please select a friend in the tree view before saving.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } if (MessageBox.Show("Are you sure you'd like to update " + selected.First + " " + selected.Last + " with the data in the interface?", "Confirm Save Friend", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes) { friend.First = firstName.Text; friend.Last = lastName.Text; if (unknown.IsChecked == true) { friend.BirthYear = 1800; friend.BirthMonth = "Jan"; friend.BirthDay = 1; } else { friend.BirthYear = Convert.ToInt32(year.Text); friend.BirthMonth = month.Text; friend.BirthDay = Convert.ToInt32(day.Text); } if (religion.Text != null && religion.Text != "") { friend.Religion = religion.Text; } else { friend.Religion = "None"; } friend.Rating = ratingSlider.Value; friend.HowMet = howWeMet.Text; friend.Details = details.Text; friend.FunnyStory = story.Text; friend.BornIn = bornIn.Text; friend.LivesIn = livesIn.Text; friend.MetIn = met.Text; if (friend.First != "" && friend.First != null && friend.Last != "" && friend.Last != null && friend.Details != null && friend.Details != "" && friend.LivesIn != "" && friend.LivesIn != null) { friend.Header = friend.First + " " + friend.Last; friendsTreeItem.Items.Add(friend); } else { MessageBox.Show("Some of your data is not filled in", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } } Friend friendContainer = (Friend)friends.SelectedValue; int index = -1; index = friendsTreeItem.Items.IndexOf(friends.SelectedValue); friendsTreeItem.Items.RemoveAt(index); MessageBox.Show(friend.First + " " + friend.Last + " has been successfully updated!", "Success!", MessageBoxButton.OK, MessageBoxImage.Information); }
//logic that loads treeview from file, highly contingent on data being in proper format private void loadTreeView(string sortBy) { int counter = 0; string line; List<Friend> friendList = new List<Friend>(); friendsTreeItem.Header = "Friends"; friends.Items.Add(friendsTreeItem); // Read the file and display it line by line. try { FileStream fs = new FileStream("ListOfFriends.txt", FileMode.OpenOrCreate); StreamReader s = new StreamReader(fs); //stopping variable for outer while bool breakWhile = false; //create new task object for each iteration of inner while loop while (!breakWhile) { Friend friend = new Friend(); while ((line = s.ReadLine()) != null) { if (counter == 0 && line == "") { break; } if (counter == 0) { friend.First = line; } else if (counter == 1) { friend.Last = line; } else if (counter == 2) { friend.BirthYear = Convert.ToInt32(line); } else if (counter == 3) { friend.BirthMonth = line; } else if (counter == 4) { friend.BirthDay = Convert.ToInt32(line); } else if (counter == 5) { friend.BornIn = line; } else if (counter == 6) { friend.LivesIn = line; } else if (counter == 7) { friend.MetIn = line; } else if (counter == 8) { friend.HowMet = line; } else if (counter == 9) { friend.Details = line; } else if (counter == 10) { friend.FunnyStory = line; } else if (counter == 11) { friend.Rating = Convert.ToDouble(line); } else if (counter == 12) { friend.Religion = line; } counter++; if (line == "") { friendList.Add(friend); counter = 0; break; } }//end inner while if (line == null && counter == 0) { breakWhile = true; } else if (line == null && counter == 12) { breakWhile = true; friendList.Add(friend); } }//end outer while s.Close(); fs.Close(); if (friendList.Count != 0) { if (sortBy == "last") friendList.Sort((Friend a, Friend b) => a.Last.CompareTo(b.Last)); else if (sortBy == "birth") friendList.Sort((Friend a, Friend b) => a.BirthYear.CompareTo(b.BirthYear)); else if (sortBy == "bornIn") friendList.Sort((Friend a, Friend b) => a.BornIn.CompareTo(b.BornIn)); else if (sortBy == "lives") friendList.Sort((Friend a, Friend b) => a.LivesIn.CompareTo(b.LivesIn)); else if (sortBy == "met") friendList.Sort((Friend a, Friend b) => a.MetIn.CompareTo(b.MetIn)); else if (sortBy == "religion") friendList.Sort((Friend a, Friend b) => a.Religion.CompareTo(b.Religion)); else if (sortBy == "rating") friendList.Sort((Friend a, Friend b) => a.Rating.CompareTo(b.Rating)); } //placing objects in list into appropriate treeview sections for (int i = 0; i < friendList.Count(); i++) { friendList[i].Header = friendList[i].First + " " + friendList[i].Last; friendsTreeItem.Items.Add(friendList[i]); }//end for loop } catch { } // Suspend the screen. Console.ReadLine(); }