0

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Apoorv
  • 2,023
  • 1
  • 19
  • 42
  • 1
    Have you bind this event to form "FormClosing" event or not? – sowjanya attaluri Jul 08 '16 at 09:42
  • 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 Jul 08 '16 at 09:44
  • 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 Jul 08 '16 at 09:48
  • wanted to know that I did what @sowjanyaattaluri has told.but its still not working for me – Apoorv Jul 08 '16 at 09:53
  • 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. –  Jul 08 '16 at 09:55
  • thats set to false only – Apoorv Jul 08 '16 at 10:03
  • If you run this on Win7 then you need to check the Output window. If you see a "first-chance exception" message then you have [this problem](http://stackoverflow.com/questions/4933958/vs2010-does-not-show-unhandled-exception-message-in-a-winforms-application-on-a). – Hans Passant Jul 08 '16 at 10:05
  • First of all: could you set up a breakpoint (or debug printout) at `frmMain_FormClosing()` and check whether it actually gets executed? – miroxlav Jul 08 '16 at 10:29
  • yes it is getting executed – Apoorv Jul 08 '16 at 10:41
  • perhaps its taking too long to complete the tasks when closing, try adding one of those to the end of the closing `Application.Exit(); Environment.Exit(1);` – user1234433222 Jul 08 '16 at 12:05
  • Check the Immediate Window when you attempt to close the form, make sure no exception is thrown. Also, what on earth are these supposed to do: `bool Cancel = eventArgs.Cancel;` and `eventArgs.Cancel = Cancel;`? You never change the `Cancel` variable, and even if you would it would still be better to call just `eventArgs.Cancel = true/false;` instead of declaring _another_ variable for it. – Visual Vincent Jul 08 '16 at 12:07

1 Answers1

0

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     
}
sowjanya attaluri
  • 903
  • 2
  • 9
  • 27