Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在以模态方式显示的嵌套表单上设置DialogResult属性会导致模态对话框的隐藏传播回表单树中_C#_Winforms_Modal Dialog - Fatal编程技术网

C# 在以模态方式显示的嵌套表单上设置DialogResult属性会导致模态对话框的隐藏传播回表单树中

C# 在以模态方式显示的嵌套表单上设置DialogResult属性会导致模态对话框的隐藏传播回表单树中,c#,winforms,modal-dialog,C#,Winforms,Modal Dialog,我有两个嵌套表单(form1和form2)以模式显示,即从我的主表单中,我通过调用(在按钮单击事件中)form1.ShowDialog()以模式显示form1,然后从form1的代码中调用(在另一个按钮单击事件中)form2.ShowDialog() 通过设置表单2的DialogResult属性(例如,单击表单标题栏上的X close按钮),表单2被隐藏(如预期),但表单1也被隐藏(意外)。主线程在form2.ShowDialog()行之后执行代码,然后继续执行form1.ShowDialog(

我有两个嵌套表单(form1和form2)以模式显示,即从我的主表单中,我通过调用(在按钮单击事件中)
form1.ShowDialog()
以模式显示form1,然后从form1的代码中调用(在另一个按钮单击事件中)
form2.ShowDialog()

通过设置表单2的DialogResult属性(例如,单击表单标题栏上的X close按钮),表单2被隐藏(如预期),但表单1也被隐藏(意外)。主线程在
form2.ShowDialog()
行之后执行代码,然后继续执行
form1.ShowDialog()
行之后的代码

我在一个新项目中尝试了相同的嵌套模式,结果表明隐藏不会传播到第一个调用表单。因此,我的代码一定有问题,但我找不到。有人知道这是什么原因吗


如果这有帮助的话,我也在单击form2的close按钮后立即停止了调试模式下的两个项目。两个项目的调用堆栈在对上的程序.Main()的调用中完全相同。调用堆栈仅在此调用之前不同。我试图强调不同之处:

不工作的项目

[Native to Managed Transition]  
[Managed to Native Transition]  
mscorlib.dll!System.AppDomain.nExecuteAssembly(System.Reflection.RuntimeAssembly assembly, string[] args) + 0x9 bytes   
mscorlib.dll!System.Runtime.Hosting.ManifestRunner.Run(bool checkAptModel) + 0x6e bytes 
mscorlib.dll!System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly() + 0x90 bytes 
mscorlib.dll!System.Runtime.Hosting.ApplicationActivator.CreateInstance(System.ActivationContext activationContext, string[] activationCustomData) + 0x65 bytes 
mscorlib.dll!System.Runtime.Hosting.ApplicationActivator.CreateInstance(System.ActivationContext activationContext) + 0xd bytes 
mscorlib.dll!System.Activator.CreateInstance(System.ActivationContext activationContext) + 0x44 bytes   
Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone() + 0x23 bytes
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state) + 0x63 bytes   
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool ignoreSyncCtx) + 0xb0 bytes    
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x2c bytes    
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() + 0x44 bytes   
[Native to Managed Transition]
[Native to Managed Transition]  
[Managed to Native Transition]  
mscorlib.dll!System.AppDomain.ExecuteAssembly(string assemblyFile, System.Security.Policy.Evidence assemblySecurity, string[] args) + 0x6d bytes    
Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() + 0x2a bytes  
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state) + 0x63 bytes   
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool ignoreSyncCtx) + 0xb0 bytes    
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x2c bytes    
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() + 0x44 bytes   
    [Native to Managed Transition]
工作项目

[Native to Managed Transition]  
[Managed to Native Transition]  
mscorlib.dll!System.AppDomain.nExecuteAssembly(System.Reflection.RuntimeAssembly assembly, string[] args) + 0x9 bytes   
mscorlib.dll!System.Runtime.Hosting.ManifestRunner.Run(bool checkAptModel) + 0x6e bytes 
mscorlib.dll!System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly() + 0x90 bytes 
mscorlib.dll!System.Runtime.Hosting.ApplicationActivator.CreateInstance(System.ActivationContext activationContext, string[] activationCustomData) + 0x65 bytes 
mscorlib.dll!System.Runtime.Hosting.ApplicationActivator.CreateInstance(System.ActivationContext activationContext) + 0xd bytes 
mscorlib.dll!System.Activator.CreateInstance(System.ActivationContext activationContext) + 0x44 bytes   
Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone() + 0x23 bytes
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state) + 0x63 bytes   
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool ignoreSyncCtx) + 0xb0 bytes    
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x2c bytes    
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() + 0x44 bytes   
[Native to Managed Transition]
[Native to Managed Transition]  
[Managed to Native Transition]  
mscorlib.dll!System.AppDomain.ExecuteAssembly(string assemblyFile, System.Security.Policy.Evidence assemblySecurity, string[] args) + 0x6d bytes    
Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() + 0x2a bytes  
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state) + 0x63 bytes   
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool ignoreSyncCtx) + 0xb0 bytes    
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x2c bytes    
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() + 0x44 bytes   
    [Native to Managed Transition]
编辑 以下是我打开表单的方式:

private void button1_Click(object sender, EventArgs e)
{
    using (Form2 form2 = new Form2())
    {
        form2.ShowDialog();
    }
}

我只能假设您的form1将CancelButton或AcceptButton属性设置为button1

我可以想象form1上的button1控件将DialogResult属性设置为除DialogResult之外的其他属性。无

您可以发布显示如何打开表单的代码吗?谢谢您发布代码来打开form2,但是form1呢?您是否有表单关闭事件等?@BlueFoots form1在属于我的主表单的按钮的单击事件上以相同的方式显示。不,我在form1和Form2中都没有关闭或任何其他事件。代码中似乎缺少一些东西,我只是用一个mainform尝试了你的代码,用你的代码打开了另外两个表单,对我来说效果很好。当我关闭时,form1不会被隐藏form2@bluefeet谢谢你的努力。我也尝试了另一个项目,一切都正常。肯定有什么东西,但我也不知道怎么找到它。也许是一些愚蠢的事情,也许是一些项目设置。我不知道。它们已经设置好了,但我已经删除了这两个属性,没有任何更改。我没有想到这一点。实际上DialogResult属性没有设置。但是在阅读了你的文章后,我手动将属性设置为“DialogResult.None”,现在问题解决了!然而,这真的很奇怪,因为我已经搜索了DialogResult的所有引用,但没有。因此,我仍然不明白问题出在哪里,但至少有一个简单的方法可以避免它。你的回答不是100%正确,但帮助我找到了解决办法。“我应该怎么做,我应该接受它吗?”甘斯威尔说。根据我的经验,只有当(a)form2.DialogResult通过编程设置为None以外的值,或者(b)button1.DialogResult设置为None以外的值时,表单才会自动关闭。当您说“DialogResult属性未设置”时,您是指form2.DialogResult还是button1.DialogResult?如果你有足够的兴趣,你应该仔细比较你的新(工作)项目中的代码和你现有项目中的代码。由你来决定是否接受这个答案。好吧,我发现了这个愚蠢的错误。当您进行成像时,它是button1.DialogResult属性。Windows窗体设计器自动生成的代码将属性设置为Cancel,很久以前我从form1属性中选择该按钮作为CancelButton。后来,当我再次从设计器中将form1的CancelButton设置为“(无)”时,设置button1.DialogResult属性的代码行没有自动删除。在测试项目中,我也能重现同样的行为。