is there anything else in your btnOk click event?
Here's my guess. There is. It probably looks like:
private void btnOk_Click(object sender, EventArgs e)
{
if (txtBarCode.Text == "" || txtProductName.Text == "" || txtPrice.Text == "" || numericUpDown1 == null)
{
MessageBox.Show("Please Fill it", "warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
txtBarCode.Focus();
}
this.Close();
}
You are checking to make sure that everything has been filled in, and then if it hasn't you are sending a message saying that it hasn't and telling them to fill it in.
But then the rest of your btnOk click event runs and there's probably a
this.Close()
event in there or something that closes the form.
Calling
txtBarCode.Focus()
doesn't halt execution of the rest of your code. You need to add a
return;
after
txtBarCode.Focus();
or put the rest of your code in an
else
block.
When you get an issue like that, put in a breakpoint before the code that is executing and step-through your code and see if you can find why it is closing.