private void btnAdd_Click(object sender, EventArgs e) { List<string> categories = new List<string>(); foreach (ListViewItem item in lstAchievements.Items) if (!categories.Contains(item.SubItems[1].Text)) categories.Add(item.SubItems[1].Text); categories.Sort(); Achievement achievement = null; while (true) { using (frmEditAchievement dialog = new frmEditAchievement(categories, achievement)) { if (dialog.ShowDialog() == DialogResult.OK) { achievement = dialog.Achievement; // Make sure we don't already have it. bool found = false; foreach (Achievement a in League.Achievements) { if (a.Name.ToLower() == dialog.Achievement.Name.ToLower()) { MessageBox.Show("You already have an achievement of that name. Either edit the existing one or use a different name.", "Achievement Already Exists", MessageBoxButtons.OK, MessageBoxIcon.Information); found = true; break; } } if (found) continue; else { _League.Achievements.Add(achievement.Clone()); // If we just added a new category, put it at the top of the list for convenience sake. if (!categories.Contains(dialog.Achievement.Category)) categories.Insert(0, dialog.Achievement.Category); var item = new ListViewItem { Name = achievement.ToString(), Text = achievement.Name, Tag = achievement }; item.SubItems.Add(achievement.Category); item.SubItems.Add(achievement.MaxAllowed.ToString()); item.SubItems.Add(achievement.Points.ToString()); item.SubItems.Add(achievement.PointsEarnedEachTime ? "Yes" : "No"); item.SubItems.Add(achievement.MustEarnAllToGetPoints ? "Yes" : "No"); item.Selected = true; lstAchievements.Items.Add(item); lstAchievements.Sort(); // If we're looping, reset everything but the category to let the user reenter fast. achievement = new Achievement() { Category = "", Earned = 0, MaxAllowed = 1, MustEarnAllToGetPoints = false, Name = "", Points = 1, PointsEarnedEachTime = true }; } } else return; } if (!chkContinuousAdd.Checked) return; } }
private void lstAchievements_DoubleClick(object sender, EventArgs e) { if (lstAchievements.SelectedItems.Count > 0) { ListViewItem selected = lstAchievements.SelectedItems[0]; Achievement currentAchievement = (Achievement)selected.Tag; List<string> categories = new List<string>(); foreach (ListViewItem item in lstAchievements.Items) if (!categories.Contains(currentAchievement.Category)) categories.Add(currentAchievement.Category); using (frmEditAchievement dialog = new frmEditAchievement(categories, currentAchievement)) { if (dialog.ShowDialog() == DialogResult.OK) { // See if this achievement was earned before being edited. bool answered = false; foreach (MatchResult match in _League.MatchesPlayed) { foreach (Achievement a in match.Achievements) { if (a.Name.ToLower() == currentAchievement.Name.ToLower()) { if (!answered) { if (MessageBox.Show("This achievement has already been earned by at least one player. Existing achievements " + "will be updated with the new information. Continue?", "Achievement In Use", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.No) return; answered = true; } a.Name = dialog.Achievement.Name; a.Category = dialog.Achievement.Category; a.MaxAllowed = dialog.Achievement.MaxAllowed; if (a.Earned > dialog.Achievement.MaxAllowed) a.Earned = dialog.Achievement.MaxAllowed; a.Points = dialog.Achievement.Points; a.PointsEarnedEachTime = dialog.Achievement.PointsEarnedEachTime; a.MustEarnAllToGetPoints = dialog.Achievement.MustEarnAllToGetPoints; } } } foreach (Achievement a in _League.Achievements) { if (a.Name.ToLower() == selected.Text.ToLower()) { a.Name = dialog.Achievement.Name; a.Category = dialog.Achievement.Category; a.MaxAllowed = dialog.Achievement.MaxAllowed; a.Points = dialog.Achievement.Points; a.PointsEarnedEachTime = dialog.Achievement.PointsEarnedEachTime; a.MustEarnAllToGetPoints = dialog.Achievement.MustEarnAllToGetPoints; break; } } selected.Name = dialog.Achievement.ToString(); selected.Text = dialog.Achievement.Name; selected.Tag = dialog.Achievement; selected.SubItems[1].Text = dialog.Achievement.Category; selected.SubItems[2].Text = dialog.Achievement.MaxAllowed.ToString(); selected.SubItems[3].Text = dialog.Achievement.Points.ToString(); selected.SubItems[4].Text = dialog.Achievement.PointsEarnedEachTime ? "Yes" : "No"; selected.SubItems[5].Text = dialog.Achievement.MustEarnAllToGetPoints ? "Yes" : "No"; } } } }