115 lines
4.7 KiB
C#
115 lines
4.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using trakker.Models;
|
|
|
|
namespace trakker.Forms
|
|
{
|
|
|
|
public partial class TaskForm : Form
|
|
{
|
|
/// <summary>
|
|
/// The task instance being edited by this form.
|
|
/// </summary>
|
|
private readonly trakker.Models.PTask _task;
|
|
|
|
/// <summary>
|
|
/// Binding source that connects the task status to the form controls.
|
|
/// </summary>
|
|
private BindingSource? _status;
|
|
|
|
/// <summary>
|
|
/// Binding source that connects the task priority to the form controls.
|
|
/// </summary>
|
|
private BindingSource? _priority;
|
|
|
|
/// <summary>
|
|
/// Binding source that connects the task model to the form controls.
|
|
/// </summary>
|
|
private BindingSource bindingSource = new BindingSource();
|
|
|
|
/// <summary>
|
|
/// Error provider used to display validation errors next to input controls.
|
|
/// </summary>
|
|
private ErrorProvider errorProvider = new ErrorProvider();
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="ProjectForm"/> class bound to
|
|
/// the provided <paramref name="project"/>. Sets up data bindings for all
|
|
/// visible input controls and configures dialog button behavior.
|
|
/// </summary>
|
|
/// <param name="task">The <see cref="Task"/> instance to edit. Must not be null.</param>
|
|
/// <param name="status">The binding source for status values. Must not be null.</param>
|
|
/// <param name="priority">The binding source for priority values. Must not be null.</param>
|
|
public TaskForm(trakker.Models.PTask task, BindingSource status, BindingSource priority)
|
|
{
|
|
InitializeComponent();
|
|
_task = task ?? throw new ArgumentNullException(nameof(task));
|
|
_status = status ?? throw new ArgumentNullException(nameof(status));
|
|
_priority = priority ?? throw new ArgumentNullException(nameof(priority));
|
|
|
|
Text = "Task Details";
|
|
|
|
comboBoxStatus.DataSource = _status;
|
|
comboBoxStatus.DisplayMember = "Display";
|
|
comboBoxStatus.ValueMember = "Value";
|
|
|
|
comboBoxPriority.DataSource = _priority;
|
|
comboBoxPriority.DisplayMember = "Display";
|
|
comboBoxPriority.ValueMember = "Value";
|
|
|
|
dateTimePickerDueDate.Format = DateTimePickerFormat.Custom;
|
|
dateTimePickerDueDate.CustomFormat = "yyyy-MM-dd";
|
|
|
|
// // Bind model properties to controls so the UI reflects and updates the model.
|
|
bindingSource.DataSource = _task;
|
|
textBoxTitle.DataBindings.Add("Text", bindingSource, "Title", true);
|
|
richTextBoxDescription.DataBindings.Add("Text", bindingSource, "Description", true);
|
|
dateTimePickerDueDate.DataBindings.Add("Value", bindingSource, "DueDate", true);
|
|
textBoxHoursEst.DataBindings.Add("Text", bindingSource, "EstimatedHours", true);
|
|
textBoxHoursActual.DataBindings.Add("Text", bindingSource, "ActualHours", true);
|
|
textBoxRate.DataBindings.Add("Text", bindingSource, "HourlyRate", true);
|
|
comboBoxStatus.DataBindings.Add("SelectedValue", bindingSource, "Status", true);
|
|
comboBoxPriority.DataBindings.Add("SelectedValue", bindingSource, "Priority", true);
|
|
|
|
// Configure dialog buttons and window behavior.
|
|
buttonOkay.DialogResult = DialogResult.OK;
|
|
buttonCancel.DialogResult = DialogResult.Cancel;
|
|
this.CancelButton = buttonCancel;
|
|
this.StartPosition = FormStartPosition.CenterParent;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the <see cref="Task"/> instance edited by the form.
|
|
/// </summary>
|
|
public trakker.Models.PTask Task { get => _task; private set { } }
|
|
|
|
/// <summary>
|
|
/// Validates the Name field. If the name is empty or whitespace, an error is set
|
|
/// on the <see cref="errorProvider"/> and the event is canceled to prevent the
|
|
/// form from closing.
|
|
/// </summary>
|
|
private void textBoxTitle_Validating(object sender, System.ComponentModel.CancelEventArgs e)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(textBoxTitle.Text))
|
|
{
|
|
errorProvider.SetError(textBoxTitle, "Title is required.");
|
|
errorProvider.SetIconAlignment(textBoxTitle, ErrorIconAlignment.MiddleRight);
|
|
errorProvider.SetIconPadding(textBoxTitle, 2);
|
|
e.Cancel = true;
|
|
}
|
|
else
|
|
{
|
|
errorProvider.SetError(textBoxTitle, "");
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|