如何创建一个非模态形式,而是阻止? [英] How to create a non-modal form but blocking?

查看:141
本文介绍了如何创建一个非模态形式,而是阻止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果听起来不可思议,但这只是我的业余爱好。我想一个(自定义),消息框弹出了YESNO按钮,理想情况下应该阻止code。不过,我应该可以点击父窗体上,这样我可以关闭该消息框,而不​​必专门点击消息框按钮(等同于在消息框中单击否)。

Should sound weird, but this is just for my hobby. I would want a (custom) messagebox to pop up with a YesNo buttons which should ideally block the code. But I should be able to click on the parent form so that I can dismiss the message box without having to specifically click on the messagebox buttons (equivalent to clicking No on the message box)..

是这样的:

     void Foo()
     {
         CustomMsgBox.Show("do you really wanna delete?", CustomMsgBox.Buttons.YesNo);
         //block the code here, but user should be able to click on form, so that its equivalent to have clicked No;
         //if clicked No, return;

         //delete.
     }

所以,我认为解决方案是使自定义消息框非模式 - 使用户可以点击的形式,但我没能阻挡code ..我如何能做到这一点。

So the solution I thought was make the custom message box non modal - so that user can click on form, but I'm not able to block code.. How can i do that?

这是这样的:

 void Foo()
 {
     NonModalMsgBox.Show("do you really wanna delete?", CustomMsgBox.Buttons.YesNo);
     //block thread till user clicks on form or messagebox buttons.
     //unblock when user clicks.
     //if No, return;

     //delete.
 }

编辑:我知道这是不是一个标准的做法,我知道非模态形式的不阻塞,而模态的形式做。所以,请不要推荐安于两种模态窗体的或者非模态窗体的行为。我的问题是有没有办法来模拟文本菜单与行为窗口的形式。

I know this is not a standard practice and I know non modal forms do not block, while modal forms do. So please do not recommend to be content with either modal form's or non-modal form's behavior. My question would be is there any way to simulate the behaviour of ContextMenu with windows forms.

推荐答案

您可以这样做:

public void ShowMe() {
    Show();
    while (!_receivedDeactivateEvent)
        Application.DoEvents();
}

我不知道我会推荐它,虽然 - 我不知道怎么会是稳定的,我也不是肯定它是否会表现得你想要的方式,如果你点击父窗体上的删除按钮而对话是由(将首先关闭第一个对话框,或离开它?或许是后者,这可能会导致混乱)。

I'm not sure I'd recommend it, though -- I'm not sure how stable it would be, nor am I sure whether it would behave the way you want if you click the Delete button on the parent form while the 'dialog' is up (would it close the first dialog first, or leave it up? might be the latter, which could get messy).

这篇关于如何创建一个非模态形式,而是阻止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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