郑德才博客 记录学习,记录工作,学习知识分享!

C# WinForm中添加窗体大小变化切换和退出前确认功能

在C# WinForm中添加窗体大小变化和退出前确认功能,就要自行添加Resize、FormClosing事件。方法如下:
 
在frmMain.Designer.cs中的InitializeComponent()添加:
this.Resize += new System.EventHandler(this.frmMain_Resize);
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.frmMain_FormClosing);
 
在frmMain.cs中添加:
private void frmMain_Resize(object sender, EventArgs e)
{
    this.Text = "...";  //使窗体名称显示为...
}
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{
    if (MessageBox.Show("你确定要退出?", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.Yes)   //BoxButton按钮的Yes or No ,如果是Yes退出
        e.Cancel = false;
    else
        e.Cancel = true;
}
2012年6月2日 | 发布:郑德才博客 | 分类:学习之路 | 评论:0

发表留言: