简体   繁体   English

禁用关闭按钮

[英]Disable Close Button

I have a form that a user uses to select the level when a Game starts. 我有一个表格,供用户在游戏开始时用来选择等级。 I want to disable the close button such that a user cannot close the form (The user will click some buttons to select the level). 我想禁用关闭按钮,以使用户无法关闭表单(用户将单击某些按钮来选择级别)。

I have been able to stop the user from closing the form if a button is not clicked using 如果未使用来单击按钮,我已经能够阻止用户关闭表单

    bool _Next = false;
    public Form1()
    {
        InitializeComponent();
        button1.Click += new EventHandler(button_Click);
        button2.Click += new EventHandler(button_Click);
        button3.Click += new EventHandler(button_Click);

    }

    void button_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;

        if (btn == button1)
        {
            Level(1);
        }
        else if (btn == button2)
        {
            Level(2);
        }
        else if (btn == button3)
        {
            Level(3);
        }
        _Next = true;
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (_Next == true)
        {
        }
        else
        {
            e.Cancel = true;
        }
    }

This is quite long. 这很长。 I want to know if there is any way i can just disable or hide the form close button 我想知道是否有什么方法可以禁用或隐藏表单关闭按钮

You're already disabling the close button. 您已经禁用了关闭按钮。 In your FormClosing event you cancel the event: 在您的FormClosing事件中,您可以取消事件:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    e.Cancel = true;
}

This code works perfectly; 该代码可以完美运行; I just tested it to make sure. 我只是对其进行测试以确保。

You can hide the Controls of the form with: 您可以使用以下方法隐藏表单的控件:

this.ControlBox = false;

The above will remove the maximize , minimize and close button from the right upper corner. 上面的代码将从右上角删除maximizeminimizeclose button

The above is also accessible on the Visual Studio when you select the form itself at the properties tab. 当您在属性选项卡上选择窗体本身时,也可以在Visual Studio上访问以上内容。

You can override the OnClosing method: 您可以重写OnClosing方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM