c#子窗体在其中一个函数完成后自动关闭

c#子窗体在其中一个函数完成后自动关闭,c#,winforms,C#,Winforms,具有名称的子窗体(由父窗体调用code 1),在按钮单击功能完成后自动关闭。在button click函数中,我使用数据库并设置一个局部变量Get\u Resultreturn to parent form。我想这与我的代码无关,问题一定在其他地方,但我找不到它,若你们有想法的话。同样在call_order的keydown功能中,我检查了如果键是escape那么this.close() code1: in parent form if (e.KeyCode == Keys.F12) { ca

具有名称的子窗体(由父窗体调用
code 1
),在按钮单击功能完成后自动关闭。在button click函数中,我使用数据库并设置一个局部变量
Get\u Result
return to parent form。我想这与我的代码无关,问题一定在其他地方,但我找不到它,若你们有想法的话。同样在
call_order
keydown
功能中,我检查了如果键是
escape
那么
this.close()escape
时,假定关闭窗体的code>

code1: in parent form
if (e.KeyCode == Keys.F12) {
  call_order call_order = new call_order(today_sale);
  call_order.ShowDialog();
  today_sale = Convert.ToInt64(call_order.Get_Result);
}
在call_订单的按钮点击功能中,我有:

if (shomare_eshterake_user != "")
        {
            int success = save_one_sale();
            if (success == 1)
            {
                MessageBox.Show("something");
                shomare_moshtari_fish.Text = Convert.ToString(shomare_fish_phone_user);
                docPrint.Print();
                shomare_moshtari_fish.Text = Convert.ToString(shomare_fish_phone_user + 1);
            }
            else if (success == 0)
            {
                MessageBox.Show("something");
            }
            else if (success == 3)
            {
                nan_barbari_count.Text = "0";
                nan_tafton_count.Text = "0";
                nan_mahali_count.Text = "0";
                nan_barbari_count.Focus();
                nan_barbari_count.SelectAll();
            }
            else if (success == 5)
            {
                MessageBox.Show("something");
            }
  }
  else MessageBox.Show("something");
@Hans Passant,在调用堆栈中这样做我有这些(来自子窗体的第一次调用),但我不理解窗体为什么是关闭的,它的所有程序集引用。你能理解为什么吗

        System.Windows.Forms.dll!System.Windows.Forms.Control.OnKeyDown(System.Windows.Forms.KeyEventArgs e) + 0x6d bytes   
System.Windows.Forms.dll!System.Windows.Forms.Control.ProcessKeyEventArgs(ref System.Windows.Forms.Message m) + 0x3f3 bytes 
System.Windows.Forms.dll!System.Windows.Forms.Form.ProcessKeyPreview(ref System.Windows.Forms.Message m) + 0x43 bytes   
System.Windows.Forms.dll!System.Windows.Forms.Control.ProcessKeyPreview(ref System.Windows.Forms.Message m) + 0x16 bytes    
System.Windows.Forms.dll!System.Windows.Forms.Control.ProcessKeyMessage(ref System.Windows.Forms.Message m) + 0x1c bytes    
System.Windows.Forms.dll!System.Windows.Forms.Control.WmKeyChar(ref System.Windows.Forms.Message m) + 0x15 bytes    
System.Windows.Forms.dll!System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message m) + 0x7dd bytes 
System.Windows.Forms.dll!System.Windows.Forms.TextBoxBase.WndProc(ref System.Windows.Forms.Message m) + 0x37 bytes  
System.Windows.Forms.dll!System.Windows.Forms.TextBox.WndProc(ref System.Windows.Forms.Message m) + 0x36 bytes  
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.OnMessage(ref System.Windows.Forms.Message m) + 0x10 bytes    
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m) + 0x31 bytes  
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr hWnd, int msg, System.IntPtr wparam, System.IntPtr lparam) + 0x57 bytes 
[Native to Managed Transition]  
[Managed to Native Transition]  
System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(int dwComponentID, int reason, int pvLoopData) + 0x24e bytes  
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int reason, System.Windows.Forms.ApplicationContext context) + 0x177 bytes  
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int reason, System.Windows.Forms.ApplicationContext context) + 0x61 bytes    
System.Windows.Forms.dll!System.Windows.Forms.Application.Run(System.Windows.Forms.Form mainForm) + 0x31 bytes  


    Nan_Behesht_winform.exe!Nan_Behesht_winform.call_order.call_order_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e) Line 1308 C#
System.Windows.Forms.dll!System.Windows.Forms.Form.OnFormClosing(System.Windows.Forms.FormClosingEventArgs e) + 0x77 bytes
System.Windows.Forms.dll!System.Windows.Forms.Form.CheckCloseDialog(bool closingOnly) + 0x8c bytes  
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.System.Windows.Forms.UnsafeNativeMethods.IMsoComponent.FContinueMessageLoop(int reason, int pvLoopData, System.Windows.Forms.NativeMethods.MSG[] msgPeeked) + 0x160 bytes   
System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(int dwComponentID, int reason, int pvLoopData) + 0x1ae bytes  
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int reason, System.Windows.Forms.ApplicationContext context) + 0x177 bytes  
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int reason, System.Windows.Forms.ApplicationContext context) + 0x61 bytes    
System.Windows.Forms.dll!System.Windows.Forms.Application.RunDialog(System.Windows.Forms.Form form) + 0x33 bytes    
System.Windows.Forms.dll!System.Windows.Forms.Form.ShowDialog(System.Windows.Forms.IWin32Window owner) + 0x370 bytes    
System.Windows.Forms.dll!System.Windows.Forms.Form.ShowDialog() + 0x7 bytes 
Nan_Behesht_winform.exe!Nan_Behesht_winform.Main_Form.Main_Form_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) Line 260 + 0xa bytes    C#

您在该按钮上设置了DialogResult属性了吗

从MSDN的第页:

If the DialogResult for this property is set to anything other than None, and if
the parent form was displayed through the ShowDialog method, clicking the button
closes the parent form without your having to hook up any events.

在对话框中重写OnFormClosing并在其上设置断点。当调用堆栈出现时,看看它是如何关闭对话框的。非常好@Hans Passant,谢谢,但我仍然感到困惑,看看调用堆栈,看看你能找到什么。感谢Shard想象第一个片段是准确的,它会立即关闭对话框。在我看来,表单对象以前使用过,而您实际上没有使用new call\u order()。在调用ShowDialog()之前,显式地将窗体的DialogResult属性设置为None。好的,我用这段代码做到了这一点,但同样发生了
call\u order.DialogResult=DialogResult.None;调用_order.ShowDialog()我从显示调用订单表单的主窗体按键添加调用堆栈。看一看;)我用
.show()
而不是
.ShowDialog()
解决了所有问题!不,我没有更改子窗体中的
对话框结果
。我使用了
.show()
而不是
。ShowDialog()
子窗体在单击按钮后不会关闭。您知道原因吗?为了澄清,可以在表单设计器的“属性”窗格中找到此属性(或者
Form1.designer.cs
,如果您愿意的话)。通过代码进行设置不会自动关闭表单。