Visual Studio 2005 Express Edition C++を利用して、あるボタンが押下されたときに新ウィンドウを立ち上げ、新ウィンドウ内のボタンを押したときに新ウィンドウを消去させたいのですが、どういう記述をすればよいのでしょうか?
アドバイスのほど、よろしくお願い致します。m(__)m
以下がソースです。
#pragma once
namespace My777 {
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 ref class Form1 : public System::Windows::Forms::Form
{
private: System::Windows::Forms::Button^ ButtonXXX;
public:
Form1(void)
{
InitializeComponent();
}
protected:
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
protected:
private:
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(93, 93);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(102, 45);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// 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
//button1が押下された
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
System::Windows::Forms::Form^ Form2 = gcnew Form;
Form2->Text = L"test";
Form2->Width = 315;
Form2->Height = 145;
Form2->MinimumSize = System::Drawing::Size(315,145);
Form2->MaximumSize = System::Drawing::Size(315,145);
Form2->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
Form2->Activate();
Form2->Show();
Form2->Refresh();
ButtonXXX = gcnew System::Windows::Forms::Button();
ButtonXXX->Parent = Form2;
ButtonXXX->AutoSize = true;
ButtonXXX->Width = 250;
ButtonXXX->Location = System::Drawing::Point(10,30);
ButtonXXX->ForeColor = System::Drawing::Color::Black;
ButtonXXX->Name = L"ButtonXXX";
ButtonXXX->Size = System::Drawing::Size(5, 12);
ButtonXXX->Text = L"ボタン";
}
//ボタンが押下された
private: System::Void ButtonXXX_Click(System::Object^ sender, System::EventArgs^ e) {
//新ウィンドウを消去する。
}
};
}
Close(); では閉じなかったんですか?
Close();ですと、全ての画面が消去されてしまいます。(T_T)
ちゃんと Form2に対してClose()しましたか?
Form2->Close();とし、フォームを閉じようと以下の記述をしましたが、
ボタンを押下してもForm2->Close();でブレイクが止まりません。
何故でしょうか?
#pragma once
namespace My777 {
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 ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
}
protected:
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Button^ ButtonXXX;
protected:
private: System::ComponentModel::Container ^components;
private: System::Windows::Forms::Form^ Form2;
#pragma region Windows Form Designer generated code
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(93, 93);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(102, 45);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// 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);
this->Form2 = (gcnew System::Windows::Forms::Form());
}
#pragma endregion
//button1が押下された
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
ButtonXXX = gcnew System::Windows::Forms::Button();
ButtonXXX->Parent = Form2;
ButtonXXX->AutoSize = true;
ButtonXXX->Width = 250;
ButtonXXX->Location = System::Drawing::Point(10,30);
ButtonXXX->ForeColor = System::Drawing::Color::Black;
ButtonXXX->Name = L"ButtonXXX";
ButtonXXX->Size = System::Drawing::Size(5, 12);
ButtonXXX->Text = L"ボタン";
Form2->Text = L"test";
Form2->Width = 315;
Form2->Height = 145;
Form2->MinimumSize = System::Drawing::Size(315,145);
Form2->MaximumSize = System::Drawing::Size(315,145);
Form2->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
Form2->Activate();
Form2->Show();
Form2->Refresh();
}
//ボタンが押下された
private: System::Void ButtonXXX_Click(System::Object^ sender, System::EventArgs^ e) {
//新ウィンドウを消去する。
Form2->Close();
}
};
}
ボタンにイベントハンドラButtonXXX_Clickをアタッチしてますか?
επιστημηさん
ありがとうございます。
お返事が遅くなりすみません。
ButtonXXX->Click += gcnew System::EventHandler(this, &Form1::ButtonXXX_Click);
が抜けていたようですね。
上記の文を追加し実行したところ、button1を押下し、testフォームが表示され、testフォーム内のボタンを押下したところ、testフォームが消去され、ここまではよいのですが、再度、Form1内のbutto1ボタンを押下したところ、
Form2->Refresh()で以下のエラーメッセージが表示されました。
-------------------------------------------------------------
'System.ObjectDisposedException' のハンドルされていない例外が System.Windows.Forms.dll で発生しました。
追加情報: 破棄されたオブジェクトにアクセスできません。
-------------------------------------------------------------
何が原因なのでしょうか?
Close() したらなくなっちゃう(メモリ解放)から。
ということは、フォーム1であるボタンが押された時にフォーム2が表示され、フォーム2内のあるボタンが押された時にフォーム2をクローズし、
再度、フォーム1のボタンが押された時の処理はできないということでしょうか?
Hide() すればメモリ解放することなく、非表示になります。
マニュアル↓を読んでください。
http://msdn.microsoft.com/ja-jp/library/system.windows.forms.control.hide(VS.80).aspx
επιστημηさん
勉強になります。ありがとうございました。
Hide()を使用したらうまくいきました。
ツイート | ![]() |