|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TimeApp.DataAccess;
namespace TimeApp.UI {
public partial class ProjectForm : Form {
// Fields.
private Project CurrentProject;
private bool IsProjectNew;
private List<Task> ProjectTasks;
public ProjectForm() {
InitializeComponent();
}
public DialogResult ShowDialog(IWin32Window owner, Project project, bool isNew) {
// Initialize fields and bind the data first.
CurrentProject = project;
IsProjectNew = isNew;
ProjectTasks = new List<Task>(project.Tasks);
BindData();
return ShowDialog(owner);
}
public DialogResult ShowDialog(IWin32Window owner, Project project) {
return ShowDialog(owner, project, false);
}
/// <summary>
/// Binds the current project to the controls on the form.
/// </summary>
private void BindData() {
// Fill the combobox with the estimation comparison types.
cbEstimationComparisonType.DataSource = Enum.GetValues(typeof(EstimationComparisonType));
cbEstimationComparisonType.SelectedIndex = -1;
// Bind the controls to the data.
txtName.DataBindings.Add("Text", CurrentProject, "Name");
txtCode.DataBindings.Add("Text", CurrentProject, "Code");
ctlEstimatedDuration.DataBindings.Add("Duration", CurrentProject, "EstimatedDuration", true, DataSourceUpdateMode.OnValidation, null);
cbEstimationComparisonType.DataBindings.Add("SelectedItem", CurrentProject, "EstimationComparisonType", true, DataSourceUpdateMode.OnValidation, null);
chkDetectAway.DataBindings.Add("Checked", CurrentProject, "DetectAway");
chkIsBillable.DataBindings.Add("Checked", CurrentProject, "IsBillable");
chkIsComplete.DataBindings.Add("Checked", CurrentProject, "IsComplete");
// Bind to the tasks for the project.
lbTasks.DataSource = ProjectTasks;
lbTasks.DisplayMember = "Name";
lbTasks.ValueMember = "TaskId";
// Update the UI.
Text = (IsProjectNew ? "New " : "Edit ") + Text;
UpdateButtons();
}
private void RefreshTaskListData() {
// Refresh the listbox's currency manager.
CurrencyManager Manager = lbTasks.BindingContext[ProjectTasks] as CurrencyManager;
Manager.Refresh();
}
/// <summary>
/// Updates the buttons based on the state of the interface.
/// </summary>
private void UpdateButtons() {
btnEdit.Enabled = (lbTasks.SelectedItem != null);
btnDelete.Enabled = btnEdit.Enabled;
}
/// <summary>
/// Opens a task form for adding a new task.
/// </summary>
private void AddTask() {
// Create a new task for adding.
Task TaskToAdd = new Task();
TaskToAdd.Project = CurrentProject;
// Show the task form.
TaskForm frmTask = new TaskForm();
if (frmTask.ShowDialog(this, TaskToAdd, true) != DialogResult.OK)
// Disassociate the new task from the current project to discard it.
TaskToAdd.Project = null;
else {
// Add the new task to the displayed list.
ProjectTasks.Add(TaskToAdd);
// Refresh the listbox's data.
RefreshTaskListData();
}
}
/// <summary>
/// Opens a task form for editing the currently selected task.
/// </summary>
private void EditTask() {
// Get the task for editing.
Task TaskToEdit = (Task)lbTasks.SelectedItem;
// Show the task form.
TaskForm frmTask = new TaskForm();
frmTask.ShowDialog(this, TaskToEdit);
}
/// <summary>
/// Deletes the currently selected task.
/// </summary>
private void DeleteTask() {
if (MessageBox.Show(this, "Delete this task?", "Delete Task", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) {
// Mark the task for deletion (if it is not new) and delete it from the displayed list.
Task TaskToDelete = (Task)lbTasks.SelectedItem;
if (TaskToDelete.TaskId > 0)
TaskToDelete.IsMarked = true;
ProjectTasks.Remove(TaskToDelete);
// Refresh the listbox's data.
RefreshTaskListData();
}
}
private void lbTasks_SelectedIndexChanged(object sender, EventArgs e) {
UpdateButtons();
}
private void lbTasks_DoubleClick(object sender, EventArgs e) {
EditTask();
}
private void btnAdd_Click(object sender, EventArgs e) {
AddTask();
}
private void btnEdit_Click(object sender, EventArgs e) {
EditTask();
}
private void btnDelete_Click(object sender, EventArgs e) {
DeleteTask();
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I began programming on my Commodore 64 at around the age of 12. After migrating to DOS and then Windows, I decided to take on the Web. Several languages and platforms later, I have settled in with .NET nicely. I am currently the owner of a software consulting company and lead application developer for a learning-based technology consultation company.
The love of a finished application is usually at war with the desire to improve it as soon as it's released (they're never really finished).