繁体   English   中英

关闭按钮不关闭表单

[英]Close button not closing the form

我有一个最初用 VB 编写并转换为 C# 的 WinForms 应用程序。 我正在尝试通过与 VB 对应程序进行比较来调试我的 C# 应用程序。

我目前注意到的是,我的 C# 应用程序中最右端的红色取消按钮不会关闭表单,但它会在 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();
        }

我想我缺少一些在单击表单中的关闭按钮时会调用此函数的事件处理程序。

您必须像这样将“frmMain_FormClosing”绑定到 FormClosing 事件,单击表单 => 属性 => 事件,然后

在此处输入图片说明

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     
}

暂无
暂无

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

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