本文介绍了如何找到自动关闭表单的代码(或其他东西)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 大家好。 我有一个带有一些文本框和一个保存按钮的表格。这个按钮单击事件有2个if语句,当其中一个为true时,另一个为false。在if语句的内部我有一些命令在数据库中做一些工作。当最后一个代码执行结束时,调试器(或编译器)转到事件之外(并且我没有使用任何close()函数来关闭表单表格内部表格自动关闭。有没有办法找到代码或什么东西关闭表格。 谢谢各位朋友。解决方案 检查你的按钮! 按钮可以有一个DialogResult属性,如果你设置它将导致窗体关闭:在VS设计器的Properties窗格中查看按钮,并确保DialogResult设置为None。 请注意,这仅影响使用ShowDialog显示的表单。 要控制表单关闭事件,您应该只管理这个事件在你的代码中,所以你必须为FormClosing添加一个事件处理程序,然后把你的代码用于管理结束那里。 所以不允许在某些条件下你应该这样做: private void YourForm_FormClosing( object sender,FormClosingEventArgs e) { if (yourDontCloseCondtion) { e.Cancel = true ; return ; } } 我假设在项目MainForm和DialogForm中有两个表单。主窗体在单击某个按钮后显示DialogForm: private void btnOpenDialogForm_Click( object sender,EventArgs e) { using ( var dialogForm = new FrmDialogForm()) { if (dialogForm.ShowDialog()== DialogResult.OK) { // 代码,如果用户在DialogForm上单击btnSave } } } 在DialogForm上有两个按钮(btnSave和btnCancel)。这些按钮设置了属性: btnSave.DialogResult = DialogResult.OK btnCancel.DialogResult = DialogResult.Cancel 你的DialogForm设置了属性: DialogForm.AcceptButton = btnSave DialogForm.CancelButton = btnCancel 你有一些btnSave ClickEvent的代码: private void btnSave_Click( object sender,EventArgs e) { // 保存表单数据的代码 } 执行按钮单击代码后单击其中一个按钮(btnSave或btnCancel)时,DialogForm会自动关闭,因为您的表单显示为Dialog窗口。 如果你想阻止自动关闭表单你应该设置你的Dia的属性logForm: DialogForm.AcceptButton =(none) DialogForm.CancelButton =(none) 和按钮属性: btnSave.DialogResult = DialogResult.None btnCancel.DialogResult = DialogResult.None 但是当用户单击WindowClose按钮(表单'X'的右上角)时,您的表单仍将关闭(并返回DialogResult.Cancel)。 您可以使用FormClosing事件控制DialogForm关闭: private void FrmDialogForm_FormClosing( object sender,FormClosingEventArgs e) { // 如果你想阻止表格结束集: e.Cancel = true ; } 几个链接: 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 [ ^ ] 我希望它可以帮助你:) 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. 解决方案 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.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:private void YourForm_FormClosing(object sender, FormClosingEventArgs e) { if (yourDontCloseCondtion) { e.Cancel = true; return; }}Hi,I'm assuming that you have two forms in your project MainForm and DialogForm. Main form shows DialogForm after some button is clicked: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:btnSave.DialogResult = DialogResult.OKbtnCancel.DialogResult = DialogResult.CancelAnd your DialogForm has set properties:DialogForm.AcceptButton = btnSaveDialogForm.CancelButton = btnCancelYou have some code for btnSave ClickEvent: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:DialogForm.AcceptButton = (none)DialogForm.CancelButton = (none)And buttons properties:btnSave.DialogResult = DialogResult.NonebtnCancel.DialogResult = DialogResult.NoneBut 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: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 :) 这篇关于如何找到自动关闭表单的代码(或其他东西)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-13 05:28