public frmEditAchievement(List<string> categories, Achievement achievement = null) { InitializeComponent(); if (achievement != null) { txtName.Text = achievement.Name; cmbCategory.Text = achievement.Category; numPoints.Value = achievement.Points; numLimit.Value = achievement.MaxAllowed; chkPointsEachTime.Checked = achievement.PointsEarnedEachTime; chkMustEarnAll.Checked = achievement.MustEarnAllToGetPoints; } cmbCategory.Items.AddRange(categories.ToArray()); }
/// <summary> /// Return a shallow clone of this Achievement object. /// </summary> /// <returns>An Achievement object containing a copy of the information within this Achievement object.</returns> public Achievement Clone() { var copy = new Achievement() { Earned = this.Earned, Category = this.Category, MaxAllowed = this.MaxAllowed, MustEarnAllToGetPoints = this.MustEarnAllToGetPoints, Name = this.Name, Points = this.Points, PointsEarnedEachTime = this.PointsEarnedEachTime }; return copy; }
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; } }