Click here to Skip to main content
15,900,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello people
When messagebo box shows msg and i press ok button form is closing
thi is my c# code

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();

           }
Posted
Comments
Tarun.K.S 26-Oct-10 12:45pm    
strange!!

Make sure your OK button isn't marked as the form's CancelButton in the form's (not the button's) properties.
Make sure your button's DialogResult property isn't set to Cancel.
 
Share this answer
 
v3
Comments
Nish Nishant 26-Oct-10 14:38pm    
Good catch, yeah this seems to be quite likely too.
William Winner 26-Oct-10 14:45pm    
Setting the form's cancelbutton doesn't do what you think it does. From MSDN: "Gets or sets the button control that is clicked when the user presses the ESC key." Setting it as the cancel button doesn't close the form when it is clicked.
Marc A. Brown 26-Oct-10 14:57pm    
William, you're correct. I was mis-remembering an event last week with a co-worker. I've updated my answer to indicate the correct resolution.
William Winner 26-Oct-10 15:04pm    
that's possible...though most likely unlikely...and it doesn't work that way if the form was created using Application.Run()...only if it was shown using Form.ShowDialog().
Marc A. Brown 26-Oct-10 15:19pm    
The OP didn't specify how his form was displayed. Changing this setting from Cancel to None fixed this exact problem last week for a co-worker as I watched. In that case the co-worker was displaying the form using ShowDialog (VB.NET project). Figured it was worth suggesting.
It's more likely that the form loses active focus and it's behind some other window now (which has gained focus).
 
Share this answer
 
is there anything else in your btnOk click event?

Here's my guess. There is. It probably looks like:
C#
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();
    }

    //more code
    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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900