private void OnFieldValidating(object sender, CancelEventArgs e) { var s = ((Control)sender).Text; var name = ((Control)sender).Name; if (s.Length > 0) { switch (name) { case "txtCost": const string costMsg = "Expected value must be a number greater than zero"; if (!double.TryParse(s, out double v)) { e.Cancel = true; } else { e.Cancel = v <= 0; } if (e.Cancel) { MessageBox.Show(costMsg, "Validation error", MessageBoxButtons.OK, MessageBoxIcon.Information); } break; case "cboExpenseItem": case "cboSource": var cbo = (ComboBox)sender; if (!cbo.Items.Contains(s)) { var msg = $"The list does not contain '{s}' \r\nDo you want to add a new item?"; if (MessageBox.Show(msg, "Item not found", MessageBoxButtons.OK, MessageBoxIcon.Information) == DialogResult.OK) { switch (name) { case "cboExpenseItem": Gears.AddExpense(s); break; case "cboSource": Gears.AddPaymentSource(s); break; } cbo.Items.Add(s); } else { e.Cancel = true; } } break; } } }