Hello Everybody,

Need abit of help in my code:

Basically I have two forms, namely form1 for Login and form2 is the actual application.

The first issue I had was hiding the form1(login-screen) after a successful login which I was able to do by declaring

Hide();

Now the issue is when I exit the application(form2), which I am able to but the form(login) is still running.

So, my question is could somebody please advice on how to overcome this issue?

N.B: Please P.S.A

Attachments
#pragma once
#include "Form2.h"

namespace Window {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Button^  button1;
	private: System::Windows::Forms::TextBox^  textBox1;
	private: System::Windows::Forms::TextBox^  textBox2;
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::Label^  label2;
	protected: 

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
			this->textBox2 = (gcnew System::Windows::Forms::TextBox());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->SuspendLayout();
			// 
			// button1
			// 
			this->button1->Font = (gcnew System::Drawing::Font(L"Tahoma", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->button1->Location = System::Drawing::Point(281, 155);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(58, 24);
			this->button1->TabIndex = 0;
			this->button1->Text = L"Login";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
			// 
			// textBox1
			// 
			this->textBox1->Location = System::Drawing::Point(191, 52);
			this->textBox1->Name = L"textBox1";
			this->textBox1->Size = System::Drawing::Size(148, 20);
			this->textBox1->TabIndex = 1;
			this->textBox1->TextChanged += gcnew System::EventHandler(this, &Form1::textBox1_TextChanged);
			// 
			// textBox2
			// 
			this->textBox2->Location = System::Drawing::Point(191, 105);
			this->textBox2->Name = L"textBox2";
			this->textBox2->PasswordChar = '*';
			this->textBox2->Size = System::Drawing::Size(148, 20);
			this->textBox2->TabIndex = 2;
			this->textBox2->TextChanged += gcnew System::EventHandler(this, &Form1::textBox2_TextChanged);
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Font = (gcnew System::Drawing::Font(L"Tahoma", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->label1->Location = System::Drawing::Point(191, 33);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(65, 13);
			this->label1->TabIndex = 3;
			this->label1->Text = L"Username";
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Font = (gcnew System::Drawing::Font(L"Tahoma", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->label2->Location = System::Drawing::Point(191, 89);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(61, 13);
			this->label2->TabIndex = 4;
			this->label2->Text = L"Password";
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(548, 212);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->label1);
			this->Controls->Add(this->textBox2);
			this->Controls->Add(this->textBox1);
			this->Controls->Add(this->button1);
			this->Name = L"Form1";
			this->Text = L"Login";
			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
			 {
				  if ( textBox1->Text->Equals( "" ) )
      {
         MessageBox::Show( "You must enter the details.", "Name Entry Error",
         MessageBoxButtons::OK, MessageBoxIcon::Exclamation );
      }
	  if ( textBox1->Text->Equals( "admin" ) & textBox2->Text->Equals( "password" ) )
      {
         MessageBox::Show( "Detail's correct.", "Correct",
         MessageBoxButtons::OK, MessageBoxIcon::Information );

		
		Form2^ myForm2 = gcnew Form2();


		myForm2->Show();
		myForm2->Activate();
		
		
		Hide();

		        
			

	}
	  	  
      else
      {
		 MessageBox::Show( "Enter detail's again.", "Name Entry Error",
         MessageBoxButtons::OK, MessageBoxIcon::Exclamation );
	
      }
			 }
	private: System::Void textBox1_TextChanged(System::Object^  sender, System::EventArgs^  e) 
			 {
	
   }
			 private: System::Void textBox2_TextChanged(System::Object^  sender, System::EventArgs^  e) 
					  {
					  }

	private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
			 }
};
}
#pragma once

namespace Window {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for Form2
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class Form2 : public System::Windows::Forms::Form
	{
	public:
		Form2(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form2()
		{
			if (components)
			{
				delete components;
			}
		}


	private: System::Windows::Forms::OpenFileDialog^  openFileDialog1;

	private: System::Windows::Forms::MenuStrip^  menuStrip1;
	private: System::Windows::Forms::ToolStripMenuItem^  fileToolStripMenuItem;


	private: System::Windows::Forms::ToolStripMenuItem^  aboutToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  aboutToolStripMenuItem1;

	private: System::Windows::Forms::ToolStripMenuItem^  exitToolStripMenuItem;
	private: System::Windows::Forms::RichTextBox^  richTextBox1;

	private: System::Windows::Forms::SaveFileDialog^  saveFileDialog1;

	private: System::Windows::Forms::Button^  button1;
	private: System::Windows::Forms::Button^  button2;





	protected: 

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->openFileDialog1 = (gcnew System::Windows::Forms::OpenFileDialog());
			this->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip());
			this->fileToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->exitToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->aboutToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->aboutToolStripMenuItem1 = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->richTextBox1 = (gcnew System::Windows::Forms::RichTextBox());
			this->saveFileDialog1 = (gcnew System::Windows::Forms::SaveFileDialog());
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->button2 = (gcnew System::Windows::Forms::Button());
			this->menuStrip1->SuspendLayout();
			this->SuspendLayout();
			// 
			// openFileDialog1
			// 
			this->openFileDialog1->FileName = L"openFileDialog1";
			// 
			// menuStrip1
			// 
			this->menuStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(2) {this->fileToolStripMenuItem, 
				this->aboutToolStripMenuItem});
			this->menuStrip1->Location = System::Drawing::Point(0, 0);
			this->menuStrip1->Name = L"menuStrip1";
			this->menuStrip1->Size = System::Drawing::Size(697, 24);
			this->menuStrip1->TabIndex = 3;
			this->menuStrip1->Text = L"menuStrip1";
			// 
			// fileToolStripMenuItem
			// 
			this->fileToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(1) {this->exitToolStripMenuItem});
			this->fileToolStripMenuItem->Name = L"fileToolStripMenuItem";
			this->fileToolStripMenuItem->Size = System::Drawing::Size(35, 20);
			this->fileToolStripMenuItem->Text = L"File";
			this->fileToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form2::fileToolStripMenuItem_Click);
			// 
			// exitToolStripMenuItem
			// 
			this->exitToolStripMenuItem->Name = L"exitToolStripMenuItem";
			this->exitToolStripMenuItem->Size = System::Drawing::Size(103, 22);
			this->exitToolStripMenuItem->Text = L"Exit";
			this->exitToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form2::exitToolStripMenuItem_Click);
			// 
			// aboutToolStripMenuItem
			// 
			this->aboutToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(1) {this->aboutToolStripMenuItem1});
			this->aboutToolStripMenuItem->Name = L"aboutToolStripMenuItem";
			this->aboutToolStripMenuItem->Size = System::Drawing::Size(40, 20);
			this->aboutToolStripMenuItem->Text = L"Help";
			this->aboutToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form2::aboutToolStripMenuItem_Click);
			// 
			// aboutToolStripMenuItem1
			// 
			this->aboutToolStripMenuItem1->Name = L"aboutToolStripMenuItem1";
			this->aboutToolStripMenuItem1->Size = System::Drawing::Size(114, 22);
			this->aboutToolStripMenuItem1->Text = L"About";
			this->aboutToolStripMenuItem1->Click += gcnew System::EventHandler(this, &Form2::aboutToolStripMenuItem1_Click);
			// 
			// richTextBox1
			// 
			this->richTextBox1->Location = System::Drawing::Point(319, 40);
			this->richTextBox1->Name = L"richTextBox1";
			this->richTextBox1->Size = System::Drawing::Size(334, 112);
			this->richTextBox1->TabIndex = 4;
			this->richTextBox1->Text = L"";
			// 
			// button1
			// 
			this->button1->Font = (gcnew System::Drawing::Font(L"Tahoma", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->button1->Location = System::Drawing::Point(41, 42);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(160, 34);
			this->button1->TabIndex = 5;
			this->button1->Text = L"Search/Open File";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &Form2::button1_Click);
			// 
			// button2
			// 
			this->button2->Font = (gcnew System::Drawing::Font(L"Tahoma", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->button2->Location = System::Drawing::Point(41, 115);
			this->button2->Name = L"button2";
			this->button2->Size = System::Drawing::Size(160, 34);
			this->button2->TabIndex = 6;
			this->button2->Text = L"Save File";
			this->button2->UseVisualStyleBackColor = true;
			this->button2->Click += gcnew System::EventHandler(this, &Form2::button2_Click);
			// 
			// Form2
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(697, 179);
			this->Controls->Add(this->button2);
			this->Controls->Add(this->button1);
			this->Controls->Add(this->richTextBox1);
			this->Controls->Add(this->menuStrip1);
			this->MainMenuStrip = this->menuStrip1;
			this->Name = L"Form2";
			this->Text = L"Catalogue v1.0";
			this->Load += gcnew System::EventHandler(this, &Form2::Form2_Load);
			this->menuStrip1->ResumeLayout(false);
			this->menuStrip1->PerformLayout();
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	
			private: System::Void aboutToolStripMenuItem1_Click(System::Object^  sender, System::EventArgs^  e) {
						 // Display Message Box
						 MessageBox::Show("Catalogue v1.0","About");
					 }
			private: System::Void exitToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
						// Exit the window
						 this->Close();
					 }
			private: System::Void openToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
				 
				 // Open the Dialog Box
				 openFileDialog1->ShowDialog();
				 
				 // Open file from the C directory
				 openFileDialog1->InitialDirectory = "C:\Users\KJSINGH\Documents";

				 // Restore directory
				 openFileDialog1->RestoreDirectory = "True";

				 // Display File tit
				 openFileDialog1->Title = "File Browser";

				 // Only Pictures to be selected
				 openFileDialog1->Filter="txt files (*.txt)|*.txt|All files (*.*)|*.*";

				 // If user has clicked on the OK button
				 if ( openFileDialog1->ShowDialog() == Windows::Forms::DialogResult::OK ) {
					 richTextBox1->Text = IO::File::ReadAllText( openFileDialog1->FileName );
				 }

			 }
			private: System::Void fileToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
					}
			private: System::Void Save_Click(System::Object^  sender, System::EventArgs^  e) {
					 
						 SaveFileDialog^ saveFileDialog1 = gcnew SaveFileDialog;

						  // Initialize the SaveFileDialog to specify the RTF extention for the file.
						  saveFileDialog1->DefaultExt = "*.txt";
						  saveFileDialog1->Filter = "TXT Files|*.txt";

						  // Determine whether the user selected a file name from the saveFileDialog.
						  if ( saveFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK &&
							 saveFileDialog1->FileName->Length > 0 )
						  {
							 // Save the contents of the RichTextBox into the file.
							 richTextBox1->SaveFile( saveFileDialog1->FileName );
						  }
				 }
private: System::Void aboutToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
		 }

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {

				// Open the Dialog Box
				 openFileDialog1->ShowDialog();
				 
				 // Open file from the C directory
				 openFileDialog1->InitialDirectory = "C:\Users\KJSINGH\Documents";

				 // Restore directory
				 openFileDialog1->RestoreDirectory = "True";

				 // Display File tit
				 openFileDialog1->Title = "File Browser";

				 // Only Pictures to be selected
				 openFileDialog1->Filter="txt files (*.txt)|*.txt|All files (*.*)|*.*";

				 // If user has clicked on the OK button
				 if ( openFileDialog1->ShowDialog() == Windows::Forms::DialogResult::OK ) {
					 richTextBox1-

I think I should have posted the code which was causing the problem....

This is the code in Form1.h which opens Form2.h once the credentials entered by the user is correct.........and I am able to hide Form1(login) but when I click on the exit in Form2, Form 1 is till running.....

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
	{
		// If text in textbox equals nothing, bring up message box to enter details.
	  if ( textBox1->Text->Equals( "" ) )
      {
         MessageBox::Show( "You must enter the details.", "Name Entry Error",
         MessageBoxButtons::OK, MessageBoxIcon::Exclamation );
      }
	  // If text in textbox1 and textbox2 equals correct details, 
	  // bring up form 2.
	  if ( textBox1->Text->Equals( "admin" ) & textBox2->Text->Equals( "password" ) )
      {
         MessageBox::Show( "Detail's correct.", "Correct",
         MessageBoxButtons::OK, MessageBoxIcon::Information );

		 
		Form1^ myForm1 = gcnew Form1();
		Form2^ myForm2 = gcnew Form2();

		myForm2->Show();
		myForm2->Activate();
		myForm1->Hide();
      }
	  // Any thing else bring up message box to enter correct details.
      else
      {
		 MessageBox::Show( "Enter detail's again.", "Name Entry Error",
         MessageBoxButtons::OK, MessageBoxIcon::Exclamation );
	
      }
   }

This the click event for the exit button in Form2:

private: System::Void exitToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
						// Exit the window
						 this->Close();
					 }

I am not sure on to close Form1 at the same time?

Please advice

Is it not possible to just close the other one as well?

System::Void exitToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
  // Exit both windows
  [B]myForm1->Close();[/B]
  this->Close();
}

Is it not possible to just close the other one as well?

System::Void exitToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
  // Exit both windows
  [B]myForm1->Close();[/B]
  this->Close();
}

Thanks for your reply William

I had tried the above before but didn't work. As it cant find form1.h!
I also included the Form1.h like this:

#include "Form1.h"

But still no luck

This article has been dead for over six months. Start a new discussion instead.