Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
hi all.

i have a form with some textbox and a button for saving. this buttons click event have 2 if statement that when one of them be true another be false. in the inside of if statements i have some command for doing some work in database.when the last code execution end, and debugger(or compiler) go to outside of event(and i do not used any close() function for closing form in the inside of event) form closes automatically. is there any way to find the code or what thing close the form.

thank you friends.
Posted
Comments
syed shanu 26-Mar-14 3:49am    
Do you want to close a form after some condition.
this.close() or me.close() will close the form
_Starbug_ 26-Mar-14 4:00am    
i before use them but now they are commented.and i tested and see that it close again

Check your buttons!
Buttons can have a DialogResult property, which if you set it will cause the form to close: look in the Properties pane for the button in the VS designer, and make sure the DialogResult is set to "None".
Note that this only affects forms that are displayed with ShowDialog.
 
Share this answer
 
Comments
_Starbug_ 26-Mar-14 4:04am    
i checked it.it set to none. but again it closes.
To have control on form closing event you should just manage this event in your code, so you have to add an event handler for "FormClosing", then put your code for management the closing there.

So to not allow the clossing in some conditions you should do like this:
C#
private void YourForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (yourDontCloseCondtion)
            {
                e.Cancel = true;
                return;
            }
}
 
Share this answer
 
v3
Comments
_Starbug_ 26-Mar-14 4:27am    
thank you.solved
Raul Iloc 26-Mar-14 4:29am    
Welcome!
Hi,

I'm assuming that you have two forms in your project MainForm and DialogForm. Main form shows DialogForm after some button is clicked:
C#
private void btnOpenDialogForm_Click(object sender, EventArgs e)
{
    using (var dialogForm = new FrmDialogForm())
    {
        if (dialogForm.ShowDialog() == DialogResult.OK)
        {
            // Code if user clicks btnSave on DialogForm
        }                
    }
}


On your DialogForm you have two buttons (btnSave and btnCancel). Those buttons has set properties:
C#
btnSave.DialogResult = DialogResult.OK
btnCancel.DialogResult = DialogResult.Cancel


And your DialogForm has set properties:
C#
DialogForm.AcceptButton = btnSave
DialogForm.CancelButton = btnCancel


You have some code for btnSave ClickEvent:
C#
private void btnSave_Click(object sender, EventArgs e)
{
    // Code for saving form data
}


DialogForm is automatically closed when one of buttons is clicked (btnSave or btnCancel) after buttons click code is executed, because your form is showed as Dialog window.

If you want to prevent auto closing form you should set properties of your DialogForm:
VB
DialogForm.AcceptButton = (none)
DialogForm.CancelButton = (none)


And buttons properties:
VB
btnSave.DialogResult = DialogResult.None
btnCancel.DialogResult = DialogResult.None


But your form will still close (and returns DialogResult.Cancel) when user click WindowClose button (upper right corner of your form 'X').

You can control your DialogForm close using FormClosing event:
C#
private void FrmDialogForm_FormClosing(object sender, FormClosingEventArgs e)
{
    // if you want to prevent form closing set:
    e.Cancel = true;
}


Few links:
http://msdn.microsoft.com/en-us/library/39wcs2dh%28v=vs.110%29.aspx[^]
http://msdn.microsoft.com/en-us/library/c7ykbedk%28v=vs.110%29.aspx[^]
http://msdn.microsoft.com/library/system.windows.forms.form.closing%28v=vs.110%29.aspx[^]

I hope it helps you :)
 
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