private void LoadListBox(bool showInactive, long selectedProjectID = 0) { var projects = showInactive ? TimeKeeperData.GetAllProjects() : TimeKeeperData.GetActiveProjects(); projects.Sort(); var previousSelectedIndex = this.lvProjects.SelectedIndices.Count > 0 ? this.lvProjects.SelectedIndices[0] : 0; this.lvProjects.Items.Clear(); this.lvProjects.Items.AddRange(projects.Select(a => new ListViewItem(new string[] { a.Name, a.Department, a.IsActive ? "X" : "" }) { Tag = a }).ToArray()); this.lvProjects.SelectedIndices.Clear(); if (this.lvProjects.Items.Count > previousSelectedIndex) { this.lvProjects.Items[previousSelectedIndex].Selected = true; } else if (this.lvProjects.Items.Count > 0) { this.lvProjects.Items[0].Selected = true; } }
public DialogResult LoadAndShowDialog(IWin32Window owner, Project editProject = null) { // Get the department list and update the combo box List <string> departments = TimeKeeperData.GetAllProjects().Select(a => a.Department).Distinct().ToList(); departments.Sort((a, b) => a.CompareTo(b)); departments.Insert(0, ""); this.cmboDepartment.DataSource = departments; if (editProject != null) { this.Text = "Edit Project"; this.txtProjectName.Text = editProject.Name; this.cmboDepartment.Text = editProject.Department; this.cbIsActive.Checked = editProject.IsActive; this.Tag = editProject; } else { this.Text = "Add Project"; // Reset the form fields this.txtProjectName.Text = string.Empty; this.cmboDepartment.Text = string.Empty; this.cbIsActive.Checked = true; this.Tag = null; } this.txtProjectName.Focus(); return(this.ShowDialog(owner)); }