Application.ApplicationExit Event (WindowsFormApplicaiton)


Jam dinding menunjukan Angka 10:30 padahal saya mantengin ini layar notebook sejak dari jam 5 sore, tapi Belum nemu juga nemu pencerahan tentang bagaimana cara menggabunggan Dua atau lebih form menjadi satu,.

Sebenernya si Pada materi kuliah Algoritma sudah pernah di bahas caranya nggabungin dua form, tapi pas pada waktu itu komputer yang saya hadapi mengalami kerusakan pada keybord sehingga terpaksa menggunakan keybord virtual, kebayang dong gimana ribetnya,. so alhasil saya ketinggalan terus tahap demi tahapnya,.

Ah curhatnya cukup sampai disini, kembali ke masalah,.

Tadi waktu searching di google ga sengaja nemu ini koding

 

private MyApplicationContext() {
    formCount = 0;

    // Handle the ApplicationExit event to know when the application is exiting.
    Application.ApplicationExit += new EventHandler(this.OnApplicationExit);

    try {
        // Create a file that the application will store user specific data in.
        userData = new FileStream(Application.UserAppDataPath + "\\appdata.txt", FileMode.OpenOrCreate);

    } catch(IOException e) {
        // Inform the user that an error occurred.
        MessageBox.Show("An error occurred while attempting to show the application." + 
                        "The error is:" + e.ToString());

        // Exit the current thread instead of showing the windows.
        ExitThread();
    }

    // Create both application forms and handle the Closed event 
    // to know when both forms are closed.
    form1 = new AppForm1();
    form1.Closed += new EventHandler(OnFormClosed);            
    form1.Closing += new CancelEventHandler(OnFormClosing);            
    formCount++;

    form2 = new AppForm2();
    form2.Closed += new EventHandler(OnFormClosed);            
    form2.Closing += new CancelEventHandler(OnFormClosing);            
    formCount++;

    // Get the form positions based upon the user specific data. 
    if (ReadFormDataFromFile()) {
        // If the data was read from the file, set the form 
        // positions manually.
        form1.StartPosition = FormStartPosition.Manual;
        form2.StartPosition = FormStartPosition.Manual;

        form1.Bounds = form1Position;
        form2.Bounds = form2Position;
    }

    // Show both forms.
    form1.Show();
    form2.Show();
}

private void OnApplicationExit(object sender, EventArgs e) {
    // When the application is exiting, write the application data to the 
    // user file and close it.
    WriteFormDataToFile();

    try {
        // Ignore any errors that might occur while closing the file handle.
        userData.Close();
    } catch {}
}

Mungkin ada yang bisa mencernanya,. ini saya ambil dari http://msdn.microsoft.com/en-us/library/system.windows.forms.application.applicationexit.aspx iya, ini koding saya ambil langsung dari situs kakek nenek moyang nya si windows..

Dengan sintaq 
public static event EventHandler ApplicationExit

You must attach the event handlers to the Exit event to perform unhandled, required tasks before the application stops running. You can close files opened by this application, or dispose of objects that garbage collection did not reclaim.

Because this is a static event, you must detach any event handlers attached to this event in the ApplicationExit event handler itself. If you do not detach these handlers, they will remain attached to the event and continue to consume memory.

exmpl

The following code example displays two forms and exits the application when both forms are closed. When the application starts and exits, the position of each form is remembered. This example demonstrates using the ApplicationExit event to know when the form positions should be persisted to the file, and when the FileStream should be closed.

The class MyApplicationContext inherits from ApplicationContext and keeps track of when each form is closed, and exits the current thread when they both are. The class remembers the position of each form when it is closed. When the ApplicationExit event occurs, the class writes the positions of each for the user to the file. The form position data is stored in a file titled appdata.txt that is created in the location determined by UserAppDataPath. The Main method calls Application.Run(context) to start the application given the ApplicationContext.

 

Saya kopasin ajalah, di postingan sebelume si aku translitin, tapi berhubung malam sudah larut, dan mata ini juga sudah menunjukan sinyal -sinyal kantuk, jadi saya cukupkan dulu, mengocehnya,.

Semoga Bisa Membantu, Walau sedikit berantakan,.:D

One thought on “Application.ApplicationExit Event (WindowsFormApplicaiton)

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s