'C# Cancel click event if conditions are not met
I am writing a program in windows forms where I have a listbox in Form1 as well as a button. When pressing the button Form2 opens where I have some textboxes that will add things to a list. This list is then transferred to Form1 and printed in the listbox when the user presses OK.
However, I would like to have a condition that ensures that the list has both a name and that it isn't empty. I have a method called CheckValuesOK() in Form2, and am using that one to check the values in Form1 before printing them. The appropriate messageboxes are displayed if everything is not filled out properly, but Form2 still closes. How can I avoid this?
It currently looks like this in Form1:
FoodItemForm foodItemForm = new FoodItemForm();
if(foodItemForm.ShowDialog() == DialogResult.OK)
{
if (foodItemForm.CheckInputOK())
{
lbxFoodItems.Items.Add(foodItemForm.FItem.ToString());
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
