• 締切済み

C++でのwindows フォームアプリケーションについて

最近フォームアプリケーションでの開発を始めたのですが、 public ref class Form1 : public System::Windows::Forms::Form から #pragma region Windows Form Designer generated code の間に記述されている内容がサンプルコードを見たりしても理解できません。 どのような時に呼び出されて、どのような意味があるのでしょうか 以下、参考にさせてもらったサンプルコード public ref class Form1 : public System::Windows::Forms::Form { protected: msCDXMain* m_pDXMain; BOOL m_bStart; public: Form1(void) { InitializeComponent(); InitDX(); m_bStart = FALSE; // //TODO: ここにコンストラクタ コードを追加します // } protected: /// <summary> /// 使用中のリソースをすべてクリーンアップします。 /// </summary> ~Form1() { if (components) { delete components; } } private: System::Windows::Forms::Button^ button1; private: System::Windows::Forms::Timer^ PaintTimer; protected: private: System::ComponentModel::IContainer^ components; private: /// <summary> /// 必要なデザイナ変数です。 /// </summary> BOOL InitDX(void) { m_pDXMain = NULL; try{ m_pDXMain = new msCDXMain; if(!m_pDXMain->Init((HWND)this->Handle.ToInt32())){ SAFE_DELETE(m_pDXMain); throw 0; } } catch(...){ return FALSE; } return TRUE; }

みんなの回答

  • redfox63
  • ベストアンサー率71% (1325/1856)
回答No.1

protected: // アクセス指定子 このクラスの継承時に公開するレベルの設定 // msCDXMain型のポインターのメンバー変数 msCDXMain* m_pDXMain; // 何かのフラグ BOOL m_bStart; public: Form1(void) { // コントロールなどの初期化ルーチン InitializeComponent(); // たぶんダイレクトXの初期化ルーチン InitDX(); // フラグの初期化 m_bStart = FALSE; // //TODO: ここにコンストラクタ コードを追加します // } protected: /// <summary> /// 使用中のリソースをすべてクリーンアップします。 /// </summary> ~Form1() {   // 後始末ルーチン   if (components)   {     delete components;   } } // フォームのボタンコントロールの宣言 private: System::Windows::Forms::Button^ button1; // タイマーコントロールの宣言 private: System::Windows::Forms::Timer^ PaintTimer; protected: // デザイナで使う記述 private: System::ComponentModel::IContainer^ components; private: /// <summary> /// 必要なデザイナ変数です。 /// </summary> BOOL InitDX(void) {   // メンバー変数の初期化   m_pDXMain = NULL;   try{     // msCDXMainクラスの生成     m_pDXMain = new msCDXMain;     // 生成したクラスの初期化ルーチン呼び出し     if(!m_pDXMain->Init((HWND)this->Handle.ToInt32())){       // 初期化失敗なら 作成したオブジェクトを破棄       // 例外を送出して終了       SAFE_DELETE(m_pDXMain);       throw 0;     }   }   catch(...){     // その他の例外処理 ... 初期化失敗でリターン     return FALSE;   }   // 初期化成功   return TRUE; といった具合かと ...

noname#142252
質問者

お礼

しばらく考えたら何となく分かってきました。 まだ完全に理解したわけではありませんが、解説した下さったことに感謝します。

noname#142252
質問者

補足

最初のprotectedは変数の宣言、publicはコンストラクタ、2番目のprotectedはデストラクタで良いのですかね? privateにある変数の初期化はコンストラクタでは出来ないのでしょうか?

全文を見る
すると、全ての回答が全文表示されます。

関連するQ&A

  • VC2010プログラミング

    formの中に自作のクラスを定義して使用したいのですが、どこに定義すればよいのかわかりません。 #pragma once #include "XXX.h" namespace FileRename { 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> /// Form1 の概要 /// </summary> public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); // //TODO: ここにコンストラクター コードを追加します // } protected: /// <summary> /// 使用中のリソースをすべてクリーンアップします。 /// </summary> ~Form1() { if (components) { delete components; } } private: System::Windows::Forms::Button^ button1; XXX^ instance = gcnew XXX(); protected: private: /// <summary> /// 必要なデザイナー変数です。 /// </summary> System::ComponentModel::Container ^components;   ・   ・   ・ } XXXクラスはこの場所で定義するのは間違いなのでしょうか?

  • C#のタイマーについて

    タイマーのプログラムを作っています。 namespace WindowsApplication3 { public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.Label label4; protected System.Windows.Forms.Label label5; protected System.Windows.Forms.Label label6; protected System.Windows.Forms.Button button1; protected System.Windows.Forms.Button button2; protected DateTime dt; protected DateTime recTime; private System.Windows.Forms.GroupBox groupBox1; protected System.Windows.Forms.RadioButton radioButton1; protected System.Windows.Forms.RadioButton radioButton2; private System.Windows.Forms.Timer timer1; private System.ComponentModel.IContainer components; public Form1() { // // Windows フォーム デザイナ サポートに必要です。 // InitializeComponent(); DateTime dt; dt=DateTime.Now; string a="00"; string b=a; string c=a; label4.Text = string.Format("{0}時{1}分{2}秒", dt.Hour, dt.Minute,dt.Second); label5.Text = string.Format("{0}時{1}分{2}秒",a,b,c); label6.Text = string.Format("{0}時間{1}分{2}秒",a,b,c); // // TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。 // dt=DateTime.Now; } /// <summary> /// 使用されているリソースに後処理を実行します。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows フォーム デザイナで生成されたコード /// <summary> /// デザイナ サポートに必要なメソッドです。このメソッドの内容を /// コード エディタで変更しないでください。 <<  文字数オーバーのため 省略>> /// <summary> /// アプリケーションのメイン エントリ ポイントです。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void button2_Click(object sender, System.EventArgs e) { Application.Exit(); } protected void timer1_Tick(object sender, System.EventArgs e) { DateTime dt; dt=DateTime.Now; label4.Text = string.Format("{0}時{1}分{2}秒", dt.Hour, dt.Minute,dt.Second); } protected void button1_Click(object sender, System.EventArgs e) { DateTime recTime; recTime=DateTime.Now; TimeSpan k; k=dt-recTime; label5.Text=string.Format("{0}時{1}分{2}秒",recTime.Hour,recTime.Minute,recTime.Second); label6.Text=string.Format("{0}秒",k.Seconds); } } } このプログラムでは ボタン1をクリックするとlabel6に経過時間 label5に記録時間を出力させたいのですが、TimeSpanクラスが上手く使えず、経過時間がマイナスになってしまいます。アドバイスお願いします。

  • フォームのテキストコントロールへの出力

    Cは分かりますが、C++初心者です。 よろしくお願いします。 namespace sample1 外にある関数から、フォームのコントロールにアクセスするには、 どうしたらいいのでしょうか? ========================================================== #pragma once void DspData( ReadData *readdata ); //コールバック関数宣言 namespace sample1 { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; public __gc class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); } protected: void Dispose(Boolean disposing) { if (disposing && components) { components->Dispose(); } __super::Dispose(disposing); } private: System::Windows::Forms::Label * label1; private: System::Windows::Forms::Button * start; private: System::Windows::Forms::Button * end; public: System::Windows::Forms::TextBox * textBox1; private: System::ComponentModel::Container * components; void InitializeComponent(void) { } private: System::Void start_Click(System::Object * sender, System::EventArgs * e) { } }; } //------------------------------------------------ // DspData //------------------------------------------------ void DspData( ReadData *readdata ) { //***↓ここでフォームのテキストボックスへの表示を行いたい。***** textBox1->set_text("ここで出力") return; } ==========================================================

  • VisualStudio 2008 Express

    無料でネットからダウンロードしたVisualStudio 2008 Express Edition のフォームをつかってGUIの勉強をしています。 非情報系学生のための C/C++ 入門 というサイトにある コースの 「第三回  GUI と画像処理プログラミング 」をやってみましたが、フォーム上のボタンを押せばメッセージボックスが出てくるはずなのに、何も起こりません。 何が原因なのかわかりません。詳しい方、どうか知恵を貸してください。  ちなみにそのサイトのアドレスは以下のとおりです。 http://brain.cc.kogakuin.ac.jp/~kanamaru/lecture/prog1/index.html ボタンをフォームに貼り付けると自動的に生成されるイベントハンドラーに、自分の追加した部分も含めたヘッダーファイルの内容を下に乗せておきます。 // *************************** // Form1.h file #pragma once namespace MyGUI_NET { 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> /// Form1 の概要 /// /// 警告: このクラスの名前を変更する場合、このクラスが依存するすべての .resx ファイルに関連付けられた /// マネージ リソース コンパイラ ツールに対して 'Resource File Name' プロパティを /// 変更する必要があります。この変更を行わないと、 /// デザイナと、このフォームに関連付けられたローカライズ済みリソースとが、 /// 正しく相互に利用できなくなります。 /// </summary> public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); // //TODO: ここにコンストラクタ コードを追加します // } protected: /// <summary> /// 使用中のリソースをすべてクリーンアップします。 /// </summary> ~Form1() { if (components) { delete components; } } private: System::Windows::Forms::Button^ button1; protected: protected: private: /// <summary> /// 必要なデザイナ変数です。 /// </summary> System::ComponentModel::Container ^components; #pragma region Windows Form Designer generated code /// <summary> /// デザイナ サポートに必要なメソッドです。このメソッドの内容を /// コード エディタで変更しないでください。 /// </summary> void InitializeComponent(void) { this->button1 = (gcnew System::Windows::Forms::Button()); this->SuspendLayout(); // // button1 // this->button1->BackColor = System::Drawing::Color::PeachPuff; this->button1->Location = System::Drawing::Point(34, 35); this->button1->Name = L"button1"; this->button1->Size = System::Drawing::Size(185, 55); this->button1->TabIndex = 0; this->button1->Text = L"click me"; this->button1->UseVisualStyleBackColor = false; // // Form1 // this->AutoScaleDimensions = System::Drawing::SizeF(6, 12); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(292, 266); this->Controls->Add(this->button1); this->Name = L"Form1"; this->Text = L"Form1"; this->ResumeLayout(false); } #pragma endregion // event handler for button 1 private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { MessageBox::Show("こんにちは"); // 自分が追加した一行 } }; } //****************** どうぞよろしくお願いします。

  • C#

    form2で入力した文字を form1で表示させたいのですが、上手くいきません。プログラムの(文字制限の為)一部だけ のせます。アドバイス よろしくお願いします。 まず、form1 public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.MainMenu mainMenu1; private System.Windows.Forms.MenuItem menuItem1; private System.Windows.Forms.MenuItem menuItem2; private System.Windows.Forms.Label label1; private Form2 form2; /// <summary> /// 必要なデザイナ変数です。 /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Windows フォーム デザイナ サポートに必要です。 // InitializeComponent(); form2= new Form2(); label1.Text=string.Format("{0}",form2.s); // // TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。 // } static void Main() { Application.Run(new Form1()); } private void menuItem2_Click(object sender, System.EventArgs e) { if (form2.ShowDialog(this) == DialogResult.OK) this.Refresh(); } } 次にform2です private void button1_Click(object sender, System.EventArgs e) { string s; s=(string)textBox1.Text; } public string s { get { return s; } }

  • C言語について質問・・・

    柴田望洋氏の著書である明解C言語(入門編)をもとに visual C++を使ってC言語の勉強をしているのですが まず最初に15+37の計算をしようと書いてあり、とりあえず わからない文法だらけだと思うが、下の通りそのまま入力してみよう と書いてあるのですが、その文章をそのまま入力してみたところ いきなりビルドエラーがでてきました。 ↓が実際の文章です。 /* 整数値15と37の和を表示する */ #include <stdio.h> int main(void) { printf("%d",15+37); /*整数値15と37の和を10進数で表示*/ return(0); } ※本書そのままを入力しており、全角半角のミスはないと思います。 しかし、実際新しいプロジェクトを作り立ち上げた瞬間に意味の分からない 文字の羅列が出てきてそのあとに上の文章を書いてあります。 ↓がその文章 #pragma once namespace lesson01 { 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> /// Form1 の概要 /// </summary> public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); // //TODO: ここにコンストラクター コードを追加します // } protected: /// <summary> /// 使用中のリソースをすべてクリーンアップします。 /// </summary> ~Form1() { if (components) { delete components; } } private: /// <summary> /// 必要なデザイナー変数です。 /// </summary> System::ComponentModel::Container ^components; #pragma region Windows Form Designer generated code /// <summary> /// デザイナー サポートに必要なメソッドです。このメソッドの内容を /// コード エディターで変更しないでください。 /// </summary> void InitializeComponent(void) { this->SuspendLayout(); // // Form1 // this->AutoScaleDimensions = System::Drawing::SizeF(6, 12); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(284, 262); this->Name = L"Form1"; this->Text = L"Form1"; this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load); this->ResumeLayout(false); } #pragma endregion private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { } }; } /* 整数値15と37の和を表示する */ #include <stdio.h> int main(void) { printf("%d",15+37); /*整数値15と37の和を10進数で表示*/ return(0); } となっております。 ※lesson01はプロジェクト名です。 ビルドエラーは 1>------ ビルド開始: プロジェクト: lesson01, 構成: Debug Win32 ------ 1> lesson01.cpp 1>lesson01.cpp(10): error C2731: 'main' : 関数はオーバーロードできません。 1> lesson01.cpp(9) : 'main' の宣言を確認してください。 ========== ビルド: 0 正常終了、1 失敗、0 更新不要、0 スキップ ========== 保存先はwindows フォームアプリケーションを選び保存しています

  • C++ CLI のデストラクタについて

    VisualStudioにてWindowsフォームアプリを作成する際、自動で以下コードが記載されています。 デストラクタ protected: ~Form1( ) { if (components) { delete components; } } private: System::ComponentModel::Container ^components; 変数componentsは、定義されてはいるのですが、componentsにAddされているものが無く、デストラクタに削除される為だけに存在している様に見えます。 このcomponentsは何の為に作成されているのでしょうか?

  • Windowsフォームの表示

    Visual C++ 2005(Express Edition)で、windowsフォームを3個作りました。 1個目をForm1.h、2個目をForm2.h、3個目をForm3.h、メインプロジェクトは、Sample.cpp, ファイルとします。 Form1にボタンbutton1を作成し、ボタンをクリックしたイベントが発生したとき、Form2を表示させます。 Form2には、ボタンbutton2があります。 Form1と同様に、Form2のボタンをクリックするとForm3の画面が表示されるようにします。 ところが、以下のソースファイルで書いたところ、Form2とForm3は、名前空間に存在しませんという意味のエラーが生じます。 Form2とForm3をインスタンス化し、Show()メッソドで、画面を表示させる方法です。 しかし、Form1とForm2だけのファイルですと、先のソースの書き方で上手くいきます。 何か、書き方が異なるのでしょうか?正しい書き方を教えてくださいますとありがたいです。 なお、C++/CLIの文法に沿った、ソースコードでお願いします。 <ソースファイル> ----Form1.h----------------------------- #pragma once #include "stdafx.h" #include "Form2.h" #include "Form3.h" namespace Sample { 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> /// Form1 の概要 /// /// 警告: このクラスの名前を変更する場合、このクラスが依存するすべての .resx ファイルに関連付けられた /// マネージ リソース コンパイラ ツールに対して 'Resource File Name' プロパティを /// 変更する必要があります。この変更を行わないと、 /// デザイナと、このフォームに関連付けられたローカライズ済みリソースとが、 /// 正しく相互に利用できなくなります。 /// </summary> public ref class Form1 : public System::Windows::Forms::Form { //button1を作成 //省略 #pragma endregion private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { Form2^ a = gcnew Form2(); a->Show(); } }; } [EOF] --------------------------------------- ----Form2.h---------------------------- #pragma once #include "Form1.h" #include "Form3.h" using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; namespace Sample { /// <summary> /// IsotopeInput の概要 /// /// 警告: このクラスの名前を変更する場合、このクラスが依存するすべての .resx ファイルに関連付けられた /// マネージ リソース コンパイラ ツールに対して 'Resource File Name' プロパティを /// 変更する必要があります。この変更を行わないと、 /// デザイナと、このフォームに関連付けられたローカライズ済みリソースとが、 /// 正しく相互に利用できなくなります。 /// </summary> public ref class Form2 : public System::Windows::Forms::Form { //button2を作成 //省略 } #pragma endregion private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) { Form3^ b = gcnew Form3(); b->Show(); } }; } [EOF] --------------------------------------- -------Form3.h-------------------------- #pragma once #include "stdafx.h" #include "Form1.h" #include "Form2.h" using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; namespace Sample { /// <summary> /// Result の概要 /// /// 警告: このクラスの名前を変更する場合、このクラスが依存するすべての .resx ファイルに関連付けられた /// マネージ リソース コンパイラ ツールに対して 'Resource File Name' プロパティを /// 変更する必要があります。この変更を行わないと、 /// デザイナと、このフォームに関連付けられたローカライズ済みリソースとが、 /// 正しく相互に利用できなくなります。 /// </summary> public ref class Form3 : public System::Windows::Forms::Form { //省略 } #pragma endregion }; } [EOF] --------------------------------------------------------- Sample.cpp----------------------------------------------- // Sample.cpp : メイン プロジェクト ファイルです。 #include "stdafx.h" #include "Form1.h" #include "Form2.h" #include "Form3.h" using namespace Sample; [STAThreadAttribute] int main(array<System::String ^> ^args) { // コントロールが作成される前に、Windows XP ビジュアル効果を有効にします Application::EnableVisualStyles(); Application::SetCompatibleTextRenderingDefault(false); // メイン ウィンドウを作成して、実行します Application::Run(gcnew Form1()); return 0; } [EOF] ------------------------------------------------------------

  • フォーム間のデータ受け渡し

    現在VIsual Studio 2005のフォームアプリケーションを使ってプログラミングしています。ボタンを押すことで新たな子フォームを作成し、親フォームから子フォームへグローバル関数で宣言しているbmp[],picture[],red[]などのデータを渡したいのですがどうすればいいのかわからず困っております。子フォームから親フォームへテキストボックスなどの値を渡す方法などはわかったのですが、それをどう応用していいのかもわからない状況です。最終的には親フォームのbmp[0]におけるred[0]が1(画像処理されている)なら子フォームでbmp[0]を表示させたいと思っています。わかる方がいましたらどうかご教授ください。よろしくお願いします。以下がプログラムとなっております。 *** 親フォーム *** #pragma once #include "pic2.h" namespace pic { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; using namespace System::IO; // 省略 // public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); // //TODO: ここにコンストラクタ コードを追加します // bmp = nullptr; Array::Resize( bmp, 20 ); Array::Resize( picture, 20 ); Array::Resize( bmpr, 20 ); this->red = gcnew array<int>(20); } // 省略 // private: System::Windows::Forms::PictureBox^ pictureBox1; private:array< Bitmap^>^ bmp; // 原画像格納 // private:array< Bitmap^>^ bmpr; // 処理画像格納 // private:array< PictureBox^>^ picture; private:array< int>^ red; // 処理:1 不処理:0 // // 省略 // #pragma endregion private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { bmp[0] = gcnew Bitmap("ファイル名",true); } private: System::Void button1_Click_1(System::Object^ sender, System::EventArgs^ e) { pic2 ^p2 = gcnew pic2(); p2->ShowDialog();     /* ボタンを押すことで新たなフォーム作成 */ } private: System::Void pictureBox1_Click(System::Object^ sender, System::EventArgs^ e) { int x,y; int w = bmpr[0]->Width; int h = bmpr[0]->Height; if(red[0] == 1){ bmp[0] = gcnew Bitmap("ファイル名",true); pictureBox1->Image = bmp[0]; red[0] = 0; return; } if(red[0] == 0){ // 画像処理 // pictureBox1->Image = bmpr[0]; red[0] = 1; } } }; *** 子フォーム *** #pragma once //#include "Form1.h" #include "pic3.h" using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; namespace pic { /// <summary> /// pic2 の概要 /// public ref class pic2 : public System::Windows::Forms::Form { public: pic2(void) { InitializeComponent(); // //TODO: ここにコンストラクタ コードを追加します // } // 省略 // } #pragma endregion private: System::Void pic2_Load(System::Object^ sender, System::EventArgs^ e) { if(親フォームのred[0]==1ならば){ pictureBox1->Image = bmp[0] } } private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {   // 新たな子フォームpic3作成 // } private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) { this->Close(); } }; }

  • C# フォームを閉じてもプログラムが終了しない

    前略 ・C#の初心者です。 ・下記のようなプログラムを作りたいと思っています。プログラムは<作りたいプログラム>の仕様どうりに動作していますが、Form1のFormClosingイベントに Application.Exit()を追加しないと フォーム1で "X"(閉じる)をクリックしても(フォームは非表示になりますが)プログラムが終了しません。プログラムでどこかおかしな部分があると思っています。Application.Exit()を追加しないでもプログラムを終了する方法を教えてください。 <作りたいプログラム> (1)Form1 のbutton1をクリックすると新しいForm2が作成され表示される。Form2が表示されるとForm1は非表示となる。 (2)Form2 のbutton1をクリックするとForm2が非表示となりForm1が表示される。 (3)Form1 の "X"(閉じる)をクリックしてプログラムを終了する。 //Form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace formClose { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form2 form2 = new Form2(); form2.Show(); //フォーム2を表示 this.Hide(); //フォーム1を非表示 } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { Application.Exit(); //アプリケーション終了 } } } //Form2.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace formClose { public partial class Form2 : Form { Form1 form1 = new Form1(); public Form2() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { this.Close(); //フォーム2を閉じる } private void Form2_FormClosing(object sender, FormClosingEventArgs e) { form1.Show(); //フォーム1を表示する } } } 以上