C#/ WinForms:ShowDialog和随后在表单上显示 [英] C#/WinForms: ShowDialog and subsequent Show on Form

查看:202
本文介绍了C#/ WinForms:ShowDialog和随后在表单上显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为我认为这里不相关的原因,我有一个或多个线程与单个实例UI(一个表单)通信。



在我需要的工作线程中报告进度或输入数据或简单选择。所有这些都来自用户与UI的交互,当然,对于M $ .NET,所有UI都在主线程中运行。



显然我需要处理线程同步跨UI(主)线程和工作线程。我通过正确验证 InvokeRequired 和公司来做到这一点。



这里有一个文章和文章,讨论不一致和 InvokeRequired IsHandleCreated IsDisposed 等等的细微之处,所以我不会说这个。



我只需要说我的UI只是一个Form,应该被显示为一个模式无模式形式,具体取决于来电者的愿望。



一个可以只是 UI.Warn / code,而其他可以 UI.Question(做出选择:,选项...)



现在考虑以下摘录从M $ DN文档:



Form.ShowDialog方法:


与非-modal表单,当用户点击关闭表单按钮时,关闭方法不被.NET Framework调用...




我从来没有参加过一个表示为模态的表单,但是他们的实现者说不会被破坏,可能会在不可用的状态之后关闭(隐藏)。



但是它确实!



当表单从 ShowDialog()它的句柄只是被丢弃,相信当再次需要时, ShowDialog()将被调用,并且将重新创建句柄。



我不知道为什么M $的东西需要这样做,但我只是以为我没有任何问题,可以使用与模式或无模式完全相同的表单。 M $ DN docs一无所知,被禁止(或者我太醉了找不到)!



嗯,最后它是一个相对简单(和脏)

  var r = ShowDialog(); 

//处理丢弃aftrShowDialog()假设
//下一个将重新创建它。
如果(!this.IsHandleCreated)
{
//强制处理这里的娱乐,而在主线程
//之前任何Show()发生。
CreateHandle();
}

return r;

它的作品,但我想知道是否应该不会有任何体面的方式来达到同一目的。
(可能在不执行任何限制祖先兼容性行李的情况下编写,因为.NET仍然是...)

解决方案

你确定你没有这个向后吗?我创建了一个简单的空白表单,用作模态对话框,然后用一个简单的表单进行测试,只需一个按钮即可显示对话框。

 code> public partial class Form1:Form 
{
private MyDialog theDialog;
public Form1()
{
InitializeComponent();
theDialog = new MyDialog();
}

private void button1_Click(object sender,EventArgs e)
{
theDialog.ShowDialog();
}
}

我可以重复显示对话,没有任何麻烦。 / p>

现在,如果我调用 theDialog.Show(),关闭它,然后尝试再次显示,我得到一个 ObjectDisposedException



所以,文档是正确的: ShowDialog 不调用 Form.Close ,而显示显然是。



编辑



Form.Close 告诉您如果要防止表单被销毁,您需要做什么:


您可以通过处理关闭
事件并设置取消属性$ b来阻止运行时关闭窗体
$ b的CancelEventArgs作为
参数传递给您的事件处理程序。


使用该信息几分钟的想法,您可以使用您可以以模态或非模态显示的表单是微不足道的:

  public partial Form1:Form 
{
private MyDialog theDialog;
public Form1()
{
InitializeComponent();
theDialog = new MyDialog();
theDialog.FormClosing + = new FormClosingEventHandler(theDialog_FormClosing);
}

void theDialog_FormClosing(object sender,FormClosingEventArgs e)
{
e.Cancel = true;
theDialog.Hide();
}

private void button1_Click(object sender,EventArgs e)
{
if(theDialog.Visible)
{
theDialog.BringToFront ();
}
else
{
theDialog.ShowDialog();
}
}

private void button2_Click(object sender,EventArgs e)
{
if(theDialog.Visible)
{
theDialog.BringToFront();
}
else
{
theDialog.Show();
}
}
}


For reasons I think are not relevant here I have one or more Threads that comunicate with a single instance UI (a Form).

In the worker threads I need to report progress or input data or simple choices. All of those come from user interaction with the UI and, of course, for M$ .NET, all the UI runs in the main thread.

Obviously I need to handle the thread synchronization across the UI (main) thread and the worker ones. I do it by properly verifying InvokeRequired and company.

There is a miriad of posts and articles around there discussing incoherences and subtleties in InvokeRequired, IsHandleCreated, IsDisposed and so on, so I won't speak about it.

I just have to say that my UI, that is just a Form, was supposed to be shown as a modal or modeless form, depending on the caller wish.

One could just UI.Warn( "Warning!" ) while other could UI.Question( "Make a choice:", options... ).

Consider now the following excerpt from M$DN documentation:

Form.ShowDialog Method:

Unlike non-modal forms, the Close method is not called by the .NET Framework when the user clicks the close form button...

I have never attended that a form shown as modal, nevertheless their implementors say it will not be destroyed, could fall in a non-usable state after closing (hiding).

But it does!

When the form returns from ShowDialog( ) its Handle is just trashed away in the belief that when it is needed again, ShowDialog( ) will be called and the handle will be recreated.

I have no idea why M$ stuff needed to do this way, but I just thought that I'd be able to have the very same form acting perfectly as modal or modeless without a problem. M$DN docs say nothing about it being forbidden (or I am too drunk to find it)!

Well, in the end it is a relativelly simple (and dirty) way to fix it.

var r = ShowDialog( );

// Handle thrown away aftr "ShowDialog()" supposing the
// next one will recreate it.
if ( !this.IsHandleCreated )
{
    // Force "Handle" recreation here, while in the main thread,
    // before any "Show()" happens.
    CreateHandle( );
}

return r;

It works, but I wonder if it shouldn't be any decent way to reach the same end. (Maybe prgramming in something that does not carry any limitting ancestor compatibility baggage as .NET still does...)

解决方案

Are you sure you don't have this backward? I created a simple blank form to use as a modal dialog, and then tested it with a simple form that just has a button that shows the dialog.

public partial class Form1 : Form
{
    private MyDialog theDialog;
    public Form1()
    {
        InitializeComponent();
        theDialog = new MyDialog();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        theDialog.ShowDialog();
    }
}

I can show the dialog repeatedly with no trouble.

Now, if I call theDialog.Show(), close it, and then try to show it again, I get a ObjectDisposedException.

So, the documentation is correct: ShowDialog does not call Form.Close, whereas Show apparently does.

EDIT:

The documentation for Form.Close tells you what you have to do if you want to prevent the form from being destroyed:

You can prevent the closing of a form at run time by handling the Closing event and setting the Cancel property of the CancelEventArgs passed as a parameter to your event handler.

With that information and a few minutes' thought, it's trivial to have a form that you can show as modal or non-modal:

public partial class Form1 : Form
{
    private MyDialog theDialog;
    public Form1()
    {
        InitializeComponent();
        theDialog = new MyDialog();
        theDialog.FormClosing += new FormClosingEventHandler(theDialog_FormClosing);
    }

    void theDialog_FormClosing(object sender, FormClosingEventArgs e)
    {
        e.Cancel = true;
        theDialog.Hide();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (theDialog.Visible)
        {
            theDialog.BringToFront();
        }
        else
        {
            theDialog.ShowDialog();
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        if (theDialog.Visible)
        {
            theDialog.BringToFront();
        }
        else
        {
            theDialog.Show();
        }
    }
}

这篇关于C#/ WinForms:ShowDialog和随后在表单上显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆