Sign in to follow this  

[.net] Trouble with Form Closing in Visual C++

This topic is 3660 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi I am using Visual C++ 2005 Express Edition and I am having trouble with form closing because before the application exit, I must save the listView items and treeView nodes. I have omitted the control box of the form (3 controls on the top right corner of window:Minimize, Maximize and Close), instead I use the Exit menu and I write my code there. It's working without any problem at all. I attached the codes below BUT The problem is my trainer does not allow me to do that and he asked me to use the general form with the control box. How to handle my codes below? And in what event I should put those lines? Thank you very much. private: System::Void closeToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) { //treeView serialization String^ xmlfile = "\\xmltreenode.xml"; //String^ xmlpath = Path::GetFullPath(xmlfile); String^ xmlpath = String::Concat(Application::StartupPath,xmlfile); if (treeView1->Nodes->Count != 0) { if (File::Exists(xmlpath)) File::Delete(xmlpath); SerializeTreeView(treeView1,xmlpath); } //treeView serialization //saving treeView to text file //if(listView1->Items->Count == 0) // { // String ^fileName = "\\data.txt"; // //String ^path = Path::GetFullPath(fileName); // String ^app = String::Concat(Application::StartupPath,filename); // //replace the old file with this new one. // if (File::Exists(path)) // File::Delete(path); // StreamWriter ^sw = File::CreateText(path); // try // { // TreeNodeCollection ^treecoll = treeView1->Nodes; // for each(TreeNode ^savednode in treecoll) // { // sw->WriteLine(savednode->Text); // } // } // finally // { // if ( sw ) // delete (IDisposable^)sw; // } // } //saving treeView to text file //------------------------------------------------------------------- String ^fileName = "\\data.txt"; //String ^path = Path::GetFullPath(fileName); String^ path = String::Concat(Application::StartupPath,fileName); //replace the old file with this new one. if (listView1->Items->Count != 0) { //pengecekan listView kosong atau tidak. if (File::Exists(path)) File::Delete(path); //Saving the last state of the listView with a text file// /*ListView::ListViewItemCollection^ pil = this->listView1->Items; System::Collections::IEnumerator^ myEnum = pil->GetEnumerator(); while ( myEnum->MoveNext() ) { ListViewItem^ item = safe_cast<ListViewItem^>(myEnum->Current); idxlist = listView1->Items->IndexOf(item); } */ //StreamWriter ^sw = File::CreateText(path); // //String ^jum = listView1->Items->Count.ToString(); //MessageBox::Show(jum); StreamWriter ^sw = File::CreateText(path); try { for (int i=0; i <= listView1->Items->Count-1; i++) { /* ListView::ListViewItemCollection^ pil = this->listView1->Items System::Collections::IEnumerator^ myEnum = pil->GetEnumerator(); */ ListViewItem^ lviEmployee = listView1->Items[i]; //lviEmployee->Serialize(listView1->Items[i], bcrStream); //int idxlastitem = listView1->Items->Count-1; //if (i == 0) //MessageBox::Show(i.ToString()); String ^pcompany = ""; pcompany = lviEmployee->Text; //MessageBox::Show(pcompany); sw->Write(pcompany); sw->Write(","); //MessageBox::Show(empl->ecompany); //else if (i == 1) String ^pname = ""; pname = lviEmployee->SubItems[1]->Text; //MessageBox::Show(pname); sw->Write(pname); sw->Write(","); //else if (i == 2) String ^tage = ""; tage = lviEmployee->SubItems[2]->Text; //MessageBox::Show(tage); sw->Write(tage); sw->Write(","); //else if (i == 3) String ^pgender = ""; pgender = lviEmployee->SubItems[3]->Text; //MessageBox::Show(pgender); sw->Write(pgender); sw->Write(","); //else if (i == 4) String ^paddress = ""; paddress = lviEmployee->SubItems[4]->Text; //MessageBox::Show(paddress); sw->Write(paddress); sw->Write(","); //else if (i == 5) String ^pcity = ""; pcity = lviEmployee->SubItems[5]->Text; //MessageBox::Show(pcity); sw->Write(pcity); sw->Write(","); //else if (i == 6) String ^pzip = ""; pzip = lviEmployee->SubItems[6]->Text; //MessageBox::Show(pzip); sw->Write(pzip); sw->Write(","); //else if (i == 7) String ^pphone = ""; pphone = lviEmployee->SubItems[7]->Text; //MessageBox::Show(pphone); sw->WriteLine(pphone); } // for i = 0 to listView1->Items->Count } // try finally { if ( sw ) delete (IDisposable^)sw; } } //pengecekan listView kosong atau tidak. closed = true; listView1->Visible = false; treeView1->Visible = false; toolStrip1->Enabled = false; infoIToolStripMenuItem->Enabled = false; printToolStripMenuItem->Enabled = false; closeToolStripMenuItem->Enabled = false; this->Close(); //--------------------------------------------------------- }

Share this post


Link to post
Share on other sites
I'm not sure what you're asking. You're saying you need to execute that code when the user closes the window via the box in the upper right corner?

Just catch the window's Close event, the same way you were catching the menu event.

Share this post


Link to post
Share on other sites
Is it possible? Because I read in the IDE help which says:

The Form.Closed and Form.Closing events are not raised when the Application.Exit method is called to exit your application. If you have validation code in either of these events that must be executed, you should call the Form.Close method for each open form individually before calling the Exit method.

What I am going to do with my application is that the user can do both ways of closing the application. Whether they go to the Exit menu or just click the Cross button on the top right corner of the window. The problem is when the user click the Cross button, I just do not know how to handle that event because I must do a few things beforer the application exits, if I don't do anything then it just simple.

My trainer wants me to use the standard windows button (Min, Max and Close) so I must think of another way to handle the commands before the application exits.

I don't really understand the meaning of these words:
"you should call the Form.Close method for each open form individually before
calling the Exit method."
What I get from these words is I should write my code then in the last line I call the Form.Close or how?


Thank you very much.

[Edited by - chrisliando on December 4, 2007 6:45:42 PM]

Share this post


Link to post
Share on other sites

This topic is 3660 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this