如何防止表单出现在构造函数中?

发布于 2024-08-22 13:18:00 字数 170 浏览 3 评论 0原文

我有一个 C# 应用程序,我试图阻止表单显示在构造函数中。

我像这样启动表单:

Form1 f = new Form1();
f.ShowDialog();

我必须在构造函数中做什么,以便 f.ShowDialog 不应该启动并继续执行代码。

I have a C# application and I am trying to prevent a form to show up in the constructor.

I launch the form like this:

Form1 f = new Form1();
f.ShowDialog();

what do I have to do in the Constructor so the f.ShowDialog should not launch and continue code execution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

画▽骨i 2024-08-29 13:18:00

如果您想调用 <代码>f.ShowDialog

Form1 f = new Form1();
if(f.ShowTheDialog) {
  f.ShowDialog();
}

Can't you add a public property (ShowTheDialog in this example) in the constructor for f and set to true if you want to call f.ShowDialog

Form1 f = new Form1();
if(f.ShowTheDialog) {
  f.ShowDialog();
}
远山浅 2024-08-29 13:18:00

我认为 Pentium10 希望能够通过构造函数指定稍后 ShowDialog 是否允许实际显示对话框。换句话说,他确实希望能够重写 ShowDialog,以便在他自己的 ShowDialog 中他可以检查这个神奇的权限变量,然后释放或调用基本 ShowDialog。

我不确定这在技术上是否正确,但它似乎确实有效。 Pentium10,在你的Window类中,创建另一个名为ShowDialog的公共方法,它隐藏继承的ShowDialog。然后在里面检查你的变量,只有在允许的情况下,调用基础的 ShowDialog 方法,如下所示:

public partial class Window3 : Window
{
    bool _allowed { get; set; }
    public Window3( bool allowed)
    {
        _allowed = allowed;
        InitializeComponent();
    }

    public void ShowDialog()
    {
        if( !_allowed)
            return;
        else
            base.ShowDialog();
    }
}

I think Pentium10 wants to be able to specify via the constructor whether, at a later time, ShowDialog is allows to actually display a dialog. In other words, he really wants to be able to override ShowDialog, so that in his own ShowDialog he can check this magic permission variable and either bail, or call the base ShowDialog.

I'm not sure if this is technically correct, but it does seem to work. Pentium10, in your Window class, create another public method called ShowDialog that hides the inherited ShowDialog. Then inside, check your variable and only if it's allowed, call the base's ShowDialog method, like this:

public partial class Window3 : Window
{
    bool _allowed { get; set; }
    public Window3( bool allowed)
    {
        _allowed = allowed;
        InitializeComponent();
    }

    public void ShowDialog()
    {
        if( !_allowed)
            return;
        else
            base.ShowDialog();
    }
}
缺⑴份安定 2024-08-29 13:18:00

(我不是 Windows 窗体专家,但是)您不能在构造函数中设置一个标志,无论窗体是否可以显示,然后重写 OnLoad() 方法,如果您的标志为 false,则立即隐藏窗体,例如:

private bool _canShow = true;
public Form1()
{
  _canShow = ...;
}

protected override OnLoad(EventArgs e)
{
  if (!_canShow) Close();
  base.OnLoad(e);
}

(I'm no windows forms expert, but) couldn't you set a flag in your constructor, whether the form can be shown or not, then override the OnLoad() method, and if your flag is false, hide the form immediately, e.g:

private bool _canShow = true;
public Form1()
{
  _canShow = ...;
}

protected override OnLoad(EventArgs e)
{
  if (!_canShow) Close();
  base.OnLoad(e);
}
仅冇旳回忆 2024-08-29 13:18:00

如果需要显示的话,在构造函数本身中调用 ShowDialog 怎么样?

然后你只需要做:

Form1 f = new Form1();

How about calling ShowDialog in the constructor itself if it needs to be shown ?

And then you only need to do:

Form1 f = new Form1();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文