关闭按钮不关闭表单

时间:2016-07-08 09:39:49

标签: c# vb.net winforms

我最初用VB编写的WinForms应用程序转换为C#。我试图通过与VB对应的比较来调试我的C#应用​​程序。

我现在注意到,My C#应用程序中的Topmost Right红色取消按钮并不关闭Form,但它在VB中关闭。

VB 代码在这里

Private Sub frmMain_FormClosing(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Dim Cancel As Boolean = eventArgs.Cancel
        Dim ErrorFlag As ErrorFlagType = InitErrorFlag()
        Dim UnloadMode As System.Windows.Forms.CloseReason = eventArgs.CloseReason
        Dim SavePath As String
        SavePath = System.IO.Path.Combine(ConfigSoftData.DirectoryData.AppPath, "Mold\lib")
        SaveSoftConfig(SavePath, ConfigSoftData, ErrorFlag)
        CheckDirectoryExists(SavePath)
        StatusText = ""
        eventArgs.Cancel = Cancel
        My.Settings.MainScreenLeft = Me.Left
        My.Settings.MainScreenTop = Me.Top
        My.Settings.MainScreenWidth = Me.Width
        My.Settings.Save()
    End Sub 

C#代码在这里

 private void frmMain_FormClosing(System.Object eventSender, System.Windows.Forms.FormClosingEventArgs eventArgs)
        {
            bool Cancel = eventArgs.Cancel;
            Mold_Power_Suite.Model.FrontEndStructures.ErrorFlagType ErrorFlag = FrontEndStructures.InitErrorFlag();
            System.Windows.Forms.CloseReason UnloadMode = eventArgs.CloseReason;
            string SavePath = null;
            SavePath = System.IO.Path.Combine(ModSoftFrontEndGlobalVariables.ConfigSoftData.DirectoryData.AppPath, "Mold\\lib");
            ModSoftConfiguration.SaveSoftConfig(ref SavePath,ref  ModSoftFrontEndGlobalVariables.ConfigSoftData,ref ErrorFlag);
           ModSoftCalculations. CheckDirectoryExists(ref SavePath);
            ModSoftFrontEndGlobalVariables.StatusText = "";
            eventArgs.Cancel = Cancel;
            Properties.Settings.Default.MainScreenLeft = this.Left;
           // My.Settings.MainScreenLeft = this.Left;
            Properties.Settings.Default.MainScreenTop = this.Top;
           // My.Settings.MainScreenTop = this.Top;
            Properties.Settings.Default.MainScreenWidth = this.Width;

            //My.Settings.MainScreenWidth = this.Width;

            Properties.Settings.Default.Save();
        }

我想我错过了一些事件处理程序,它会在单击表单中的关闭按钮时调用此函数。 有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

你必须绑定" frmMain_FormClosing"像这样的FormClosing事件, 点击表格=> properties =>事件,然后

enter image description here

private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{
      e.Cancel = false; // it will close the form
      e.Cancel = true; // it will not close the form     
}