Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. [VS2008 - Compact Framework] How to prevent a closed form from been disposed

[VS2008 - Compact Framework] How to prevent a closed form from been disposed

Scheduled Pinned Locked Moved C#
csharphardwarehelptutorialquestion
20 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    steve_9496613
    wrote on last edited by
    #1

    Hello everybody. My application runs in Windows Embedded Compact 7,is written in C# and it uses .NET Compact Framework 3.5. In the application there is a form, I call it FormMain, two UserControlls, Page01_UC and SetupHome_UC, another form, FormDateTime. In FormMain i declared:

    public static Page01_UC uc1 = new Page01_UC();
    public static SetupHome_UC uc2 = new SetupHome_UC();
    public static FormDateTime uc3 = new FormDateTime();

    In the FormMain load event:

    uc1.Parent = this;
    uc1.Location = new Point(0, 0);
    uc1.Page02_UC_Load(); //something to do at startup
    uc1.Show();

    In the uc1 UserControl there is a checkbox used to show SetupHome_UC:

    private void SetupCB\_CheckStateChanged(object sender, EventArgs e)
    {
      if (SetupCB.Checked)
      {
        FormMain.uc1.Hide();
        FormMain.uc2.Parent = FormMainRef; //reference to FormMain
        FormMain.uc2.Location = new Point(0, 0);
        FormMain.uc2.Page02\_UC\_Load(); //something to do at startup
        FormMain.uc2.Show();
    
        SetupCB.Checked = false;
      }
    }
    

    In uc2 UserControl ther is a button to show FormDateTime:

    private void DateTimeBt\_Click(object sender, EventArgs e)
    {
      FormMain.uc3.ShowDialog();
    }
    

    ...at the end... in uc3 there is a button to close the form:

    private void AbortBt\_Click(object sender, EventArgs e)
    {
      Close();
    }
    

    I can show/close uc3 (FormDateTime) many times without problems but sometimes it happens that I get an ObjectDisposedException when I push the button to show uc3. I thought that uc3 should not be disposed but it seems that sometimes it happens. Have I made any mistakes that I don't see? There is a way to make uc3 UNdisposable until the application is closed? ...or any other way to avoid this problem... Thanks in advance.

    R D 2 Replies Last reply
    0
    • S steve_9496613

      Hello everybody. My application runs in Windows Embedded Compact 7,is written in C# and it uses .NET Compact Framework 3.5. In the application there is a form, I call it FormMain, two UserControlls, Page01_UC and SetupHome_UC, another form, FormDateTime. In FormMain i declared:

      public static Page01_UC uc1 = new Page01_UC();
      public static SetupHome_UC uc2 = new SetupHome_UC();
      public static FormDateTime uc3 = new FormDateTime();

      In the FormMain load event:

      uc1.Parent = this;
      uc1.Location = new Point(0, 0);
      uc1.Page02_UC_Load(); //something to do at startup
      uc1.Show();

      In the uc1 UserControl there is a checkbox used to show SetupHome_UC:

      private void SetupCB\_CheckStateChanged(object sender, EventArgs e)
      {
        if (SetupCB.Checked)
        {
          FormMain.uc1.Hide();
          FormMain.uc2.Parent = FormMainRef; //reference to FormMain
          FormMain.uc2.Location = new Point(0, 0);
          FormMain.uc2.Page02\_UC\_Load(); //something to do at startup
          FormMain.uc2.Show();
      
          SetupCB.Checked = false;
        }
      }
      

      In uc2 UserControl ther is a button to show FormDateTime:

      private void DateTimeBt\_Click(object sender, EventArgs e)
      {
        FormMain.uc3.ShowDialog();
      }
      

      ...at the end... in uc3 there is a button to close the form:

      private void AbortBt\_Click(object sender, EventArgs e)
      {
        Close();
      }
      

      I can show/close uc3 (FormDateTime) many times without problems but sometimes it happens that I get an ObjectDisposedException when I push the button to show uc3. I thought that uc3 should not be disposed but it seems that sometimes it happens. Have I made any mistakes that I don't see? There is a way to make uc3 UNdisposable until the application is closed? ...or any other way to avoid this problem... Thanks in advance.

      R Offline
      R Offline
      realJSOP
      wrote on last edited by
      #2

      Make it modeless. Instead of using form.ShowDialog(), use form.Show(). Whatever button you use to close the ford should hide it instead, using form.Hide().

      ".45 ACP - because shooting twice is just silly" - JSOP, 2010
      -----
      You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
      -----
      When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

      S 1 Reply Last reply
      0
      • R realJSOP

        Make it modeless. Instead of using form.ShowDialog(), use form.Show(). Whatever button you use to close the ford should hide it instead, using form.Hide().

        ".45 ACP - because shooting twice is just silly" - JSOP, 2010
        -----
        You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
        -----
        When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

        S Offline
        S Offline
        steve_9496613
        wrote on last edited by
        #3

        Thank you #realJSOP for your answer. I can't apply your solution because the form I show must stay over the others and must be closed to let user access to the window below, so I need a ShowModal().

        R 1 Reply Last reply
        0
        • S steve_9496613

          Thank you #realJSOP for your answer. I can't apply your solution because the form I show must stay over the others and must be closed to let user access to the window below, so I need a ShowModal().

          R Offline
          R Offline
          realJSOP
          wrote on last edited by
          #4

          ShowModal will ALWAYS dispose your form when you close it. If you need data shown in the window to be retained, you're going to have to store it in a static class so you can recall it whenever you need it.

          ".45 ACP - because shooting twice is just silly" - JSOP, 2010
          -----
          You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
          -----
          When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

          D 1 Reply Last reply
          0
          • S steve_9496613

            Hello everybody. My application runs in Windows Embedded Compact 7,is written in C# and it uses .NET Compact Framework 3.5. In the application there is a form, I call it FormMain, two UserControlls, Page01_UC and SetupHome_UC, another form, FormDateTime. In FormMain i declared:

            public static Page01_UC uc1 = new Page01_UC();
            public static SetupHome_UC uc2 = new SetupHome_UC();
            public static FormDateTime uc3 = new FormDateTime();

            In the FormMain load event:

            uc1.Parent = this;
            uc1.Location = new Point(0, 0);
            uc1.Page02_UC_Load(); //something to do at startup
            uc1.Show();

            In the uc1 UserControl there is a checkbox used to show SetupHome_UC:

            private void SetupCB\_CheckStateChanged(object sender, EventArgs e)
            {
              if (SetupCB.Checked)
              {
                FormMain.uc1.Hide();
                FormMain.uc2.Parent = FormMainRef; //reference to FormMain
                FormMain.uc2.Location = new Point(0, 0);
                FormMain.uc2.Page02\_UC\_Load(); //something to do at startup
                FormMain.uc2.Show();
            
                SetupCB.Checked = false;
              }
            }
            

            In uc2 UserControl ther is a button to show FormDateTime:

            private void DateTimeBt\_Click(object sender, EventArgs e)
            {
              FormMain.uc3.ShowDialog();
            }
            

            ...at the end... in uc3 there is a button to close the form:

            private void AbortBt\_Click(object sender, EventArgs e)
            {
              Close();
            }
            

            I can show/close uc3 (FormDateTime) many times without problems but sometimes it happens that I get an ObjectDisposedException when I push the button to show uc3. I thought that uc3 should not be disposed but it seems that sometimes it happens. Have I made any mistakes that I don't see? There is a way to make uc3 UNdisposable until the application is closed? ...or any other way to avoid this problem... Thanks in advance.

            D Offline
            D Offline
            dan sh
            wrote on last edited by
            #5

            Can you post the complete error message? You say it works sometimes and sometimes does not, makes it tough to respond to.

            "It is easy to decipher extraterrestrial signals after deciphering Javascript and VB6 themselves.", ISanti[^]

            S 1 Reply Last reply
            0
            • R realJSOP

              ShowModal will ALWAYS dispose your form when you close it. If you need data shown in the window to be retained, you're going to have to store it in a static class so you can recall it whenever you need it.

              ".45 ACP - because shooting twice is just silly" - JSOP, 2010
              -----
              You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
              -----
              When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

              D Offline
              D Offline
              dan sh
              wrote on last edited by
              #6

              MSDN states the opposite.

              When a form is displayed as a modal dialog box, clicking the Close button (the button with an X at the upper-right corner of the form) causes the form to be hidden and the DialogResult property to be set to DialogResult.Cancel. Unlike non-modal forms, the Close method is not called by the .NET Framework when the user clicks the close form button of a dialog box or sets the value of the DialogResult property. Instead the form is hidden and can be shown again without creating a new instance of the dialog box. Because a form displayed as a dialog box is hidden instead of closed, you must call the Dispose method of the form when the form is no longer needed by your application.

              Moreover, if the object is marked as static, I would expect them to be alive and healthy throughout the parent form's life.

              "It is easy to decipher extraterrestrial signals after deciphering Javascript and VB6 themselves.", ISanti[^]

              R 1 Reply Last reply
              0
              • D dan sh

                Can you post the complete error message? You say it works sometimes and sometimes does not, makes it tough to respond to.

                "It is easy to decipher extraterrestrial signals after deciphering Javascript and VB6 themselves.", ISanti[^]

                S Offline
                S Offline
                steve_9496613
                wrote on last edited by
                #7

                Thank you lw@zi, here is the complete error message:

                Error
                sa7.exe
                ObjectDisposedException
                at Microsoft.AGL.Common.MISC.HandleAr(PAL_ERROR ar)
                at System.Windows.Forms.Control.get_Visible()
                at System.Windows.Forms.Form.ShowDialog()
                at SA7.SetupHome_UC.DateTimeBt_Click(Object senter, EventArgs e)
                at System.Windows.Forms.Control.OnClick(EventArgs e)
                at BeeMobile.TransparentControls.TImageButton.OnClick(EventArgs e)
                at System.Windows.Forms.Control.WnProc(WM wm, Int32 wParam, Int32 IParam)
                at System.Windows.Forms.ContainerControl.WnProc(WM wm, Int32 wParam, Int32 IParam)
                at System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 IParam)
                at Microsoft.AGL.Form.EVL.EnterMainLoop(IntPtr hwnMain)
                at System.Windows.Forms.Application.Run(Form fm)
                at SAT.Program.Main()

                D 1 Reply Last reply
                0
                • D dan sh

                  MSDN states the opposite.

                  When a form is displayed as a modal dialog box, clicking the Close button (the button with an X at the upper-right corner of the form) causes the form to be hidden and the DialogResult property to be set to DialogResult.Cancel. Unlike non-modal forms, the Close method is not called by the .NET Framework when the user clicks the close form button of a dialog box or sets the value of the DialogResult property. Instead the form is hidden and can be shown again without creating a new instance of the dialog box. Because a form displayed as a dialog box is hidden instead of closed, you must call the Dispose method of the form when the form is no longer needed by your application.

                  Moreover, if the object is marked as static, I would expect them to be alive and healthy throughout the parent form's life.

                  "It is easy to decipher extraterrestrial signals after deciphering Javascript and VB6 themselves.", ISanti[^]

                  R Offline
                  R Offline
                  realJSOP
                  wrote on last edited by
                  #8

                  No, an EXTERNAL static class. I have such a class called Globals that I use for things that have to stick around for the life of the app.

                  ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                  -----
                  You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                  -----
                  When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                  S D 2 Replies Last reply
                  0
                  • R realJSOP

                    No, an EXTERNAL static class. I have such a class called Globals that I use for things that have to stick around for the life of the app.

                    ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                    -----
                    You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                    -----
                    When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                    S Offline
                    S Offline
                    steve_9496613
                    wrote on last edited by
                    #9

                    Hi #realJSOP, I have declared that object STATIC inside a form that is alive untill the app is closed, why it is not enough? Thanks

                    1 Reply Last reply
                    0
                    • S steve_9496613

                      Thank you lw@zi, here is the complete error message:

                      Error
                      sa7.exe
                      ObjectDisposedException
                      at Microsoft.AGL.Common.MISC.HandleAr(PAL_ERROR ar)
                      at System.Windows.Forms.Control.get_Visible()
                      at System.Windows.Forms.Form.ShowDialog()
                      at SA7.SetupHome_UC.DateTimeBt_Click(Object senter, EventArgs e)
                      at System.Windows.Forms.Control.OnClick(EventArgs e)
                      at BeeMobile.TransparentControls.TImageButton.OnClick(EventArgs e)
                      at System.Windows.Forms.Control.WnProc(WM wm, Int32 wParam, Int32 IParam)
                      at System.Windows.Forms.ContainerControl.WnProc(WM wm, Int32 wParam, Int32 IParam)
                      at System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 IParam)
                      at Microsoft.AGL.Form.EVL.EnterMainLoop(IntPtr hwnMain)
                      at System.Windows.Forms.Application.Run(Form fm)
                      at SAT.Program.Main()

                      D Offline
                      D Offline
                      dan sh
                      wrote on last edited by
                      #10

                      What controls do you have on the form? Ones which are directly on the Form i.e controls whose parent is the form. And you are disposing anything on the form? Perhaps posting the code on the form may help.

                      "It is easy to decipher extraterrestrial signals after deciphering Javascript and VB6 themselves.", ISanti[^]

                      S 1 Reply Last reply
                      0
                      • R realJSOP

                        No, an EXTERNAL static class. I have such a class called Globals that I use for things that have to stick around for the life of the app.

                        ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                        -----
                        You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                        -----
                        When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                        D Offline
                        D Offline
                        dan sh
                        wrote on last edited by
                        #11

                        I am not sure if I understand this. What I meant was that if I create a static object on the form, it has to be alive until that form (not application) is.

                        "It is easy to decipher extraterrestrial signals after deciphering Javascript and VB6 themselves.", ISanti[^]

                        R S 2 Replies Last reply
                        0
                        • D dan sh

                          I am not sure if I understand this. What I meant was that if I create a static object on the form, it has to be alive until that form (not application) is.

                          "It is easy to decipher extraterrestrial signals after deciphering Javascript and VB6 themselves.", ISanti[^]

                          R Offline
                          R Offline
                          realJSOP
                          wrote on last edited by
                          #12

                          When the modal form goes out of scope (closes), you can't access anything declared inside it because the form is no longer available/in scope.

                          ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                          -----
                          You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                          -----
                          When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                          S D 2 Replies Last reply
                          0
                          • D dan sh

                            I am not sure if I understand this. What I meant was that if I create a static object on the form, it has to be alive until that form (not application) is.

                            "It is easy to decipher extraterrestrial signals after deciphering Javascript and VB6 themselves.", ISanti[^]

                            S Offline
                            S Offline
                            steve_9496613
                            wrote on last edited by
                            #13

                            Sorry, I was not clear. I understood what you said, FormMain, in wich I declared a FormDateTime instance, is always in the background and is never closed.

                            R 1 Reply Last reply
                            0
                            • R realJSOP

                              When the modal form goes out of scope (closes), you can't access anything declared inside it because the form is no longer available/in scope.

                              ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                              -----
                              You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                              -----
                              When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                              S Offline
                              S Offline
                              steve_9496613
                              wrote on last edited by
                              #14

                              Clear. The form is used only to update date and time, it doesn't store any information.

                              R 1 Reply Last reply
                              0
                              • S steve_9496613

                                Clear. The form is used only to update date and time, it doesn't store any information.

                                R Offline
                                R Offline
                                realJSOP
                                wrote on last edited by
                                #15

                                Declare an EXTERNAL static class that the form can access

                                public static class Globals
                                {
                                public static MyDateTime { get; set; }
                                }

                                In your form, when you close it: Globals.MyDateTime = DateTime.Now; When your form closes, examine Globals.MyDateTime to ensure it's the expected value. The next time you open the form, the value will be the same as the last time you opened the form, and when you close it agaim, set Globals.MyDateTime again. The Globals.DateTime will be available everywhere else in the app as well.

                                ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                                -----
                                You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                                -----
                                When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                                S 1 Reply Last reply
                                0
                                • S steve_9496613

                                  Sorry, I was not clear. I understood what you said, FormMain, in wich I declared a FormDateTime instance, is always in the background and is never closed.

                                  R Offline
                                  R Offline
                                  realJSOP
                                  wrote on last edited by
                                  #16

                                  I have no idea what you really want. I told you to make the form modelss so it never disposes, and you said it can't be modelss. Then I suggested that you make abn external statuic class because you wanted the form to be model. WTF do you actually want to do?

                                  ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                                  -----
                                  You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                                  -----
                                  When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                                  S 1 Reply Last reply
                                  0
                                  • R realJSOP

                                    When the modal form goes out of scope (closes), you can't access anything declared inside it because the form is no longer available/in scope.

                                    ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                                    -----
                                    You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                                    -----
                                    When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                                    D Offline
                                    D Offline
                                    dan sh
                                    wrote on last edited by
                                    #17

                                    Yes, that is what I said as well.

                                    "It is easy to decipher extraterrestrial signals after deciphering Javascript and VB6 themselves.", ISanti[^]

                                    1 Reply Last reply
                                    0
                                    • D dan sh

                                      What controls do you have on the form? Ones which are directly on the Form i.e controls whose parent is the form. And you are disposing anything on the form? Perhaps posting the code on the form may help.

                                      "It is easy to decipher extraterrestrial signals after deciphering Javascript and VB6 themselves.", ISanti[^]

                                      S Offline
                                      S Offline
                                      steve_9496613
                                      wrote on last edited by
                                      #18

                                      In the form there are: 3 BeeMobile.TransparentControls.TImageButton 2 BeeMobile.TransparentControls.TLabel 2 BeeMobile.RoundTextBox.RoundTextBox 1 BeeMobile.MonthCalendar.MonthCalendar 1 BeeMobile.iWheel.iWheel 2 System.Windows.Forms.Timer For what I know, I do not dispose any control voluntarily... Here is the code on the form:

                                      using System;
                                      using System.Linq;
                                      using System.Collections.Generic;
                                      using System.ComponentModel;
                                      using System.Data;
                                      using System.Drawing;
                                      using System.Text;
                                      using System.Windows.Forms;
                                      using System.Runtime.InteropServices;

                                      //alias
                                      using LangD = SA7.Cl_LangData;
                                      using Gr = SA7.Cl_Graphics;
                                      using Globals = SA7.Cl_Globals;

                                      namespace SA7
                                      {
                                      public partial class FormDateTime : Form
                                      {

                                      private bool FormDateTime\_FirstShow = true;     // flag: first call to FormDateTime\_Load
                                      
                                      Cl\_DateTime Tempo = new Cl\_DateTime();
                                      
                                      
                                      public struct SYSTEMTIME
                                      {
                                        public short wYear;
                                        public short wMonth;
                                        public short wDayOfWeek;
                                        public short wDay;
                                        public short wHour;
                                        public short wMinute;
                                        public short wSecond;
                                        public short wMilliseconds;
                                      }
                                      
                                      //functions to read and to set time
                                      \[DllImport("coredll.dll")\]
                                      private extern static void GetSystemTime(ref SYSTEMTIME lpSystemTime);
                                      
                                      \[DllImport("coredll.dll")\]
                                      private extern static uint SetSystemTime(ref SYSTEMTIME lpSystemTime);
                                      
                                      //---------------------------------------------------------------------------------
                                      
                                      public FormDateTime()
                                      {
                                        InitializeComponent();
                                        this.Location = new Point((800 - this.Width) / 2, (480 - this.Height) / 2);
                                      }
                                      //---------------------------------------------------------------------------------.
                                      
                                      private void FormDateTime\_Load(object sender, EventArgs e)
                                      {
                                        if (FormDateTime\_FirstShow) {
                                      
                                          FormDateTime\_FirstShow = false;
                                        }
                                      
                                        CtrlsDesc();
                                        UpdateDateTime();
                                        ExitTr.Enabled = true;
                                      }
                                      //---------------------------------------------------------------------------------
                                      
                                      private void FormDateTime\_Paint(object sender, PaintEventArgs e)
                                      {
                                        //draw window border
                                        Gr.DrawFrContour(e, 0, 1, 1, this.Width - 1, this.Height - 1);
                                      }
                                      //---------------------------------------------------------------------------------
                                      
                                      //update strings
                                      public void CtrlsDesc()
                                      {
                                        AbortBt.Text = FormMain.LF.GetStr(LangD.LANG\_Win, LangD.LANG\_WinExit);
                                        UpdateBt.Text = F
                                      
                                      1 Reply Last reply
                                      0
                                      • R realJSOP

                                        I have no idea what you really want. I told you to make the form modelss so it never disposes, and you said it can't be modelss. Then I suggested that you make abn external statuic class because you wanted the form to be model. WTF do you actually want to do?

                                        ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                                        -----
                                        You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                                        -----
                                        When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                                        S Offline
                                        S Offline
                                        steve_9496613
                                        wrote on last edited by
                                        #19

                                        I want to understand. I have made what you suggested and now my app is running with the form instance declared in a static class, sometime I push buttons to see if I get the exception. But I wanted to know why this is better considering that I had declared the form static instance inside a form that is never closed until the application is running. WTF...

                                        1 Reply Last reply
                                        0
                                        • R realJSOP

                                          Declare an EXTERNAL static class that the form can access

                                          public static class Globals
                                          {
                                          public static MyDateTime { get; set; }
                                          }

                                          In your form, when you close it: Globals.MyDateTime = DateTime.Now; When your form closes, examine Globals.MyDateTime to ensure it's the expected value. The next time you open the form, the value will be the same as the last time you opened the form, and when you close it agaim, set Globals.MyDateTime again. The Globals.DateTime will be available everywhere else in the app as well.

                                          ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                                          -----
                                          You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                                          -----
                                          When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                                          S Offline
                                          S Offline
                                          steve_9496613
                                          wrote on last edited by
                                          #20

                                          Thanks for the sample #realJSOP. Actually I don't need to save the exact moment the form is closed. This form is used to give the user the possibility to change the time and/or the date of the device (Windows CE based) on which the app is running and it is not used to change a variable used somewere else in the app. So I declared an external static class and in this class I declared my form instance:

                                          public static class Cl_Forms
                                          {
                                          public static FormDateTime Suc3 = new FormDateTime();
                                          }

                                          As you suggest, in this way the form shown in a modal way should be static and should not be disposed until the app is running. Now I'm testing this solution. Unfortunately the exception occurred rarely and randomly and it was not easy to reproduce it.

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups