新ウィンドウの消去について

解決


777  2009-01-20 22:51:33  No: 69486

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) {
    //新ウィンドウを消去する。
  }

  };
}


επιστημη  URL  2009-01-21 01:49:53  No: 69487

Close(); では閉じなかったんですか?


777  2009-01-23 04:11:49  No: 69488

Close();ですと、全ての画面が消去されてしまいます。(T_T)


επιστημη  URL  2009-01-23 04:50:26  No: 69489

ちゃんと Form2に対してClose()しましたか?


777  2009-01-24 03:42:46  No: 69490

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();
  }

  };
}


επιστημη  URL  2009-01-24 05:27:43  No: 69491

ボタンにイベントハンドラButtonXXX_Clickをアタッチしてますか?


777  2009-01-30 03:18:43  No: 69492

επιστημηさん

ありがとうございます。
お返事が遅くなりすみません。

ButtonXXX->Click += gcnew System::EventHandler(this, &Form1::ButtonXXX_Click);

が抜けていたようですね。

上記の文を追加し実行したところ、button1を押下し、testフォームが表示され、testフォーム内のボタンを押下したところ、testフォームが消去され、ここまではよいのですが、再度、Form1内のbutto1ボタンを押下したところ、
Form2->Refresh()で以下のエラーメッセージが表示されました。

-------------------------------------------------------------
'System.ObjectDisposedException' のハンドルされていない例外が System.Windows.Forms.dll で発生しました。

追加情報: 破棄されたオブジェクトにアクセスできません。
-------------------------------------------------------------

何が原因なのでしょうか?


επιστημη  URL  2009-01-30 08:20:38  No: 69493

Close() したらなくなっちゃう(メモリ解放)から。


777  2009-01-30 08:41:25  No: 69494

ということは、フォーム1であるボタンが押された時にフォーム2が表示され、フォーム2内のあるボタンが押された時にフォーム2をクローズし、
再度、フォーム1のボタンが押された時の処理はできないということでしょうか?


επιστημη  URL  2009-01-30 09:59:59  No: 69495

Hide() すればメモリ解放することなく、非表示になります。
マニュアル↓を読んでください。

http://msdn.microsoft.com/ja-jp/library/system.windows.forms.control.hide(VS.80).aspx


777  2009-02-01 05:28:20  No: 69496

επιστημηさん

勉強になります。ありがとうございました。
Hide()を使用したらうまくいきました。


※返信する前に利用規約をご確認ください。

※Google reCAPTCHA認証からCloudflare Turnstile認証へ変更しました。






  このエントリーをはてなブックマークに追加