Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将formclosing事件放入按钮中_C#_Winforms - Fatal编程技术网

C# 如何将formclosing事件放入按钮中

C# 如何将formclosing事件放入按钮中,c#,winforms,C#,Winforms,我有一个名为btnChallenge的按钮。所需的操作是当单击时,无法关闭表单 以下是我到目前为止的情况: public void btnChallenge_Click(object sender, EventArgs e) { } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { // not sure on this if statement if (btnChallenge.Cl

我有一个名为
btnChallenge
的按钮。所需的操作是当单击时,无法关闭表单

以下是我到目前为止的情况:

public void btnChallenge_Click(object sender, EventArgs e) { }

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    // not sure on this if statement
    if (btnChallenge.Click)
    {
        e.Cancel = true;
    }
}

您可以这样尝试:

在窗体内声明私有变量:

private bool _closedFromMyButton;
然后在
FormClosing
事件上检查该属性:

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
    if (_closedFromMyButton) // If closed from MyButton, don't do anything. let the form close.
        return;
    Hide(); // Hide the form (or not, it's up to you; this is useful if application has an icon in the tray)
    e.Cancel = true; // Cancel form closing
}
然后在某个按钮上单击(如果需要),输入此代码以仅从该按钮(或菜单项或工具栏按钮等)关闭表单:


您可以这样尝试:

在窗体内声明私有变量:

private bool _closedFromMyButton;
然后在
FormClosing
事件上检查该属性:

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
    if (_closedFromMyButton) // If closed from MyButton, don't do anything. let the form close.
        return;
    Hide(); // Hide the form (or not, it's up to you; this is useful if application has an icon in the tray)
    e.Cancel = true; // Cancel form closing
}
然后在某个按钮上单击(如果需要),输入此代码以仅从该按钮(或菜单项或工具栏按钮等)关闭表单:


您可以定义一个变量,该变量在按下按钮时变为true,并在关闭时检查该变量是否为true

e、 g


您可以定义一个变量,该变量在按下按钮时变为true,并在关闭时检查该变量是否为true

e、 g


如果您想通过在用户按下其他按钮后关闭表单来阻止他们,那么此代码将帮助您

private bool close_state=false;    // hold the button state

// method to change the close_state by button click
private void Button1(object sender, EventArgs e)
{
    close_state = true;
    // if required you can toggle the close_state using an if statement
}

// Then on FormClosing event check that property:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (close_state) {// If the button is pressed
        e.Cancel = true; // Cancel form closing
    }
}

您可以实现其他方法来关闭表单……

如果您想通过在用户按下其他按钮后关闭表单来阻止用户,则此代码将帮助您

private bool close_state=false;    // hold the button state

// method to change the close_state by button click
private void Button1(object sender, EventArgs e)
{
    close_state = true;
    // if required you can toggle the close_state using an if statement
}

// Then on FormClosing event check that property:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (close_state) {// If the button is pressed
        e.Cancel = true; // Cancel form closing
    }
}

您可以实现其他方法来关闭表单……

您可以调用
this.close()
方法,这将调用
Form1\u FormClosing
事件:

public void btnChallenge_Click(object sender, EventArgs e)
{
    this.Close();
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    //code here...
}

您只需调用
this.Close()
方法,这将调用
Form1\u FormClosing
事件:

public void btnChallenge_Click(object sender, EventArgs e)
{
    this.Close();
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    //code here...
}

我不知道你在问这个问题。你想要一个按钮,当你点击它时,它会阻止表单关闭?我想要一个按钮(btnChallenge)使表单关闭事件e.cancel=true那么表单1不能关闭我想要如果用户点击chellange按钮,表单就不能关闭你在问这个问题。你想要一个按钮,当你点击它时,阻止表单关闭?我希望按钮(btnChallenge)使表单关闭事件e.cancel=true,则表单1无法关闭我希望如果用户单击chellange按钮,表单无法关闭