如何从UserControl禁用Windows窗体上的“X”按钮?

时间:2022-08-26 00:06:09

I have tried : ParentForm.ControlBox = false;

我试过了:ParentForm.ControlBox = false;

... but it doesn't seem to work ... the ControlBox is still enabled!

...但它似乎不起作用...... ControlBox仍然启用!

Any ideas?

/I don't want to use the Win32 interop method to disable the 'X'. Disabling the ControlBox is fine.

/我不想使用Win32互操作方法来禁用'X'。禁用ControlBox很好。

2 个解决方案

#1


ParentForm.ControlBox = false, should work

ParentForm.ControlBox = false,应该可以工作

In C# just add

在C#中添加

    protected override void OnClosing(CancelEventArgs e)
    {
        e.Cancel = true;
    }

this will not disable the ControlBox but will prevent window from closing

这不会禁用ControlBox但会阻止窗口关闭

#2


Have you tried using the FindForm() method instead? It seems as if the ParentForm property can return a null reference (not sure under which circumstances yet, though). It did for me now when I created an empty UserControl and added it to a form. Disabling the ControlBox through FindForm worked well though.

您是否尝试过使用FindForm()方法?似乎ParentForm属性可以返回一个空引用(但不确定在哪种情况下)。当我创建一个空的UserControl并将其添加到表单时,它现在为我做了。通过FindForm禁用ControlBox虽然运行良好。

#1


ParentForm.ControlBox = false, should work

ParentForm.ControlBox = false,应该可以工作

In C# just add

在C#中添加

    protected override void OnClosing(CancelEventArgs e)
    {
        e.Cancel = true;
    }

this will not disable the ControlBox but will prevent window from closing

这不会禁用ControlBox但会阻止窗口关闭

#2


Have you tried using the FindForm() method instead? It seems as if the ParentForm property can return a null reference (not sure under which circumstances yet, though). It did for me now when I created an empty UserControl and added it to a form. Disabling the ControlBox through FindForm worked well though.

您是否尝试过使用FindForm()方法?似乎ParentForm属性可以返回一个空引用(但不确定在哪种情况下)。当我创建一个空的UserControl并将其添加到表单时,它现在为我做了。通过FindForm禁用ControlBox虽然运行良好。