[英]EventHandler with FormClosingEventArgs - C#
I have this closing form code in my Form.cs 我的Form.cs中有此结束表单代码
public void label7_Click(object sender, FormClosingEventArgs e)
{
MessageBox.Show("Are you sure you want to exit?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (MessageBox.Show("Are you sure you want to exit?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
e.Cancel = true;
}
else {
Application.Exit();
}
}
and this code in my Form.designer.cs 和这段代码在我的Form.designer.cs中
this.label7.Click += new System.EventHandler(this.label7_Click);
However it keeps showing error 但是它一直显示错误
"No overload for 'label7_Click' matches delegate 'System.EventHandler'"
“'label7_Click'的重载不匹配委托'System.EventHandler'”
What should I do? 我该怎么办?
It seems label7_Click method dose not exist 似乎label7_Click方法不存在
this.label7.Click += new System.EventHandler(this.label7_Click);
void label7_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Are you sure you want to exit?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
//
}
else {
Application.Exit();
}
}
No overload for 'label7_Click' matches delegate 'label7_Click'没有重载匹配委托
public void label7_Click(object sender, FormClosingEventArgs e)//this method de is incorrect
Event with FormClosingEventArgs
is only suitable for Form.FormClosing
event. 具有
FormClosingEventArgs
事件仅适用于Form.FormClosing
事件。 Change your code to this: 将代码更改为此:
public void label7_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Are you sure you want to exit?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
this.Close(); //this closes the form
}
}
You are trying to set an event handler for a FormClosing event to the click event of a label. 您试图将FormClosing事件的事件处理程序设置为标签的click事件。
The two events have different signatures and thus are incompatible. 这两个事件具有不同的签名,因此不兼容。
label_click(object sender, EventArgs e)
MyForm_FormClosing(object sender, FormClosingEventArgs e)
If your intention is to close the application when the user clicks on the label then you need to change your event handler to 如果您打算在用户单击标签时关闭应用程序,则需要将事件处理程序更改为
label_click(object sender, EventArgs e)
{
if (MessageBox.Show("Are you sure you want to exit?", "Confirmation",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
this.Close();
}
}
if your intention is to ask for a confirmation whenever the user tries to exit the application then you need to add a FormClosing event handler to your form and then write 如果您打算在用户尝试退出应用程序时要求进行确认,则需要向表单中添加一个FormClosing事件处理程序,然后编写
MyForm_FormClosing(object sender, FormClosingEventArgs e)
{
// NOTE, you don't want to abort closing when Windows shutdown, right?
if(e.CloseReason == CloseReason.UserClosing)
{
if (MessageBox.Show("Are you sure you want to exit?", "Confirmation",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) ==
DialogResult.No)
{
e.Cancel = true;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.