0
votes

I have a WinForms application written in VB originally and converted to C#. I am trying to debug my C# application by comparing with the VB counterpart.

What I notice, currently, is that the Topmost Right red color Cancel button in My C# application doesn't close the Form but it does in VB.

The VB code is here

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 

The C# code is here

 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();
        }

I think I am missing some event handler that would call this function on clicking the Close button in the form.

1
Have you bind this event to form "FormClosing" event or not? - sowjanya attaluri
how to bind this . Its WinForms not WPF else I would have directly added the function name in the form closing event. It would have been a cake walk - Apoorv
You use the Properties window in the designer to create or select an event handler, which you can do in VB as well. There's no Handles clause so you must register the event handler explicitly, which is equivalent to AddHandler in VB. - jmcilhinney
wanted to know that I did what @sowjanyaattaluri has told.but its still not working for me - Apoorv
Take a look at the line "eventArgs.Cancel = ...", if you set it to true, then your form will not be closed. Default is false to close the form. - user6522773

1 Answers

0
votes

You have to bind "frmMain_FormClosing" to FormClosing event like this, click on form => properties => events,then

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     
}