using trakker.Models; namespace trakker.Forms { /// /// Form used to view and edit a model. Fields on the form /// are data-bound to the provided client instance and basic validation is /// performed on required fields. /// public partial class ClientForm : Form { /// /// The client instance being edited by this form. /// private readonly Client _client; /// /// Binding source that connects the client model to the form controls. /// private BindingSource bindingSource = new BindingSource(); /// /// Error provider used to display validation errors next to input controls. /// private ErrorProvider errorProvider = new ErrorProvider(); /// /// Initializes a new instance of the class bound to /// the provided . Sets up data bindings for all /// visible input controls and configures dialog button behavior. /// /// The instance to edit. Must not be null. /// The binding source for state values. Must not be null. public ClientForm(Client client, BindingSource states) { _client = client; InitializeComponent(); Text = "Client Details"; // Bind model properties to controls so the UI reflects and updates the model. bindingSource.DataSource = _client; textBoxName.DataBindings.Add("Text", bindingSource, "Name", true); textBoxCompany.DataBindings.Add("Text", bindingSource, "Company", true); textBoxEmail.DataBindings.Add("Text", bindingSource, "Email", true); maskedTextBox_Phone.DataBindings.Add("Text", bindingSource, "Phone", true); textBoxAddressStreet.DataBindings.Add("Text", bindingSource, "AddressStreet", true); textBoxAddressCity.DataBindings.Add("Text", bindingSource, "AddressCity", true); comboBoxAddressState.DataSource = states; comboBoxAddressState.DisplayMember = "Display"; comboBoxAddressState.ValueMember = "Value"; comboBoxAddressState.DataBindings.Add("SelectedValue", bindingSource, "AddressState", true); maskedTextBoxAddressPostal.DataBindings.Add("Text", bindingSource, "AddressPostal", true); richTextBoxNotes.DataBindings.Add("Text", bindingSource, "Notes", true); // Configure dialog buttons and window behavior. buttonOkay.DialogResult = DialogResult.OK; buttonCancel.DialogResult = DialogResult.Cancel; this.CancelButton = CancelButton; this.StartPosition = FormStartPosition.CenterParent; } /// /// Gets the instance edited by the form. /// public Client Client { get => _client; private set { } } /// /// Validates the Name field. If the name is empty or whitespace, an error is set /// on the and the event is canceled to prevent the /// form from closing. /// private void textBoxName_Validating(object sender, System.ComponentModel.CancelEventArgs e) { if (string.IsNullOrWhiteSpace(textBoxName.Text)) { errorProvider.SetError(textBoxName, "Name is required."); errorProvider.SetIconAlignment(textBoxName, ErrorIconAlignment.MiddleRight); errorProvider.SetIconPadding(textBoxName, 2); e.Cancel = true; } else { errorProvider.SetError(textBoxName, ""); } } /// /// Validates the Email field. If the email is empty or whitespace, an error is set /// on the and the event is canceled to prevent the /// form from closing. Note: this validation only checks presence, not format. /// private void textBoxEmail_Validating(object sender, System.ComponentModel.CancelEventArgs e) { if (string.IsNullOrWhiteSpace(textBoxEmail.Text)) { errorProvider.SetError(textBoxEmail, "Email is required."); errorProvider.SetIconAlignment(textBoxEmail, ErrorIconAlignment.MiddleRight); errorProvider.SetIconPadding(textBoxEmail, 2); e.Cancel = true; } else { errorProvider.SetError(textBoxEmail, ""); } } } }