#pragma once
#include "Form2.h"
...
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
Form2 ^f2 = gcnew Form2();
f2->Show();
}
};
}
Как правильно создать и работать с немодальной формой?
На Form1 кинул кнопку, чтобы открывать Form2:
Код:
Но так при каждом нажатии создается новый объект. Как показать уже созданную форму?
button2
Код:
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
CreateAnotherForm();
}
CreateAnotherForm();
}
и button3
Код:
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {
this->anotherForm->Show();
}
this->anotherForm->Show();
}
Запускаю, нажимаю button2, затем button3, форма AnotherForm отображается. Затем закрываю AnotherForm и нажимаю button3, чтобы снова увидеть AnotherForm. Тут появляется ошибка:
Цитата:
Доступ к ликвидированному объекту невозможен.
Имя объекта: "AnotherForm".
Имя объекта: "AnotherForm".
При нажатии на крест объект формы удаляется?
Ситуация следующая. При нажатии на крест форме посылается сообщение WM_CLOSE, что, в свою очередь, вызывает DestroyWindow(), и объект удаляется, как вы совершеннно правильно заметили. Поэтому вам нужно в обработчике OnClose() второй формы запретить закрытие формы и подменить его скрытием.
Код:
/* Form1.h */
public ref class Form1 : public System::Windows::Forms::Form {
public:
Form1(void) {
// ...
this->anotherForm = gcnew Form2();
}
// ...
private:
Form2^ anotherForm;
// ...
};
public ref class Form1 : public System::Windows::Forms::Form {
public:
Form1(void) {
// ...
this->anotherForm = gcnew Form2();
}
// ...
private:
Form2^ anotherForm;
// ...
};
Код:
/* Form2.h */
private: System::Void Form2_FormClosing(System::Object^ sender, System::Windows::Forms::FormClosingEventArgs^ e) {
e->Cancel = true;
this->Hide();
}
private: System::Void Form2_FormClosing(System::Object^ sender, System::Windows::Forms::FormClosingEventArgs^ e) {
e->Cancel = true;
this->Hide();
}
Код:
private: System::Void MainForm_FormClosing(System::Object^ sender, System::Windows::Forms::FormClosingEventArgs^ e) {
delete this->anotherForm;
e->Cancel = false;
}
delete this->anotherForm;
e->Cancel = false;
}
Последняя строка лишняя.
Но без последней строки главную форму два раза закрывать приходится.
Это почему? Приведите полный код, пожалуйста.
Код:
#pragma once
#include "AnotherForm.h"
namespace Forms_3 {
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 MainForm : public System::Windows::Forms::Form
{
public:
MainForm(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
void TestMet(void);
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~MainForm()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::CheckBox^ checkBox1;
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::Button^ button3;
AnotherForm ^anotherForm;
void CreateAnotherForm(void);
protected:
private: System::Windows::Forms::Button^ button1;
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->checkBox1 = (gcnew System::Windows::Forms::CheckBox());
this->button1 = (gcnew System::Windows::Forms::Button());
this->button2 = (gcnew System::Windows::Forms::Button());
this->button3 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// checkBox1
//
this->checkBox1->AutoSize = true;
this->checkBox1->Location = System::Drawing::Point(32, 25);
this->checkBox1->Name = L"checkBox1";
this->checkBox1->Size = System::Drawing::Size(80, 17);
this->checkBox1->TabIndex = 0;
this->checkBox1->Text = L"checkBox1";
this->checkBox1->UseVisualStyleBackColor = true;
//
// button1
//
this->button1->Location = System::Drawing::Point(116, 68);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 1;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &MainForm::button1_Click);
//
// button2
//
this->button2->Location = System::Drawing::Point(117, 113);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(75, 23);
this->button2->TabIndex = 2;
this->button2->Text = L"button2";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &MainForm::button2_Click);
//
// button3
//
this->button3->Location = System::Drawing::Point(116, 154);
this->button3->Name = L"button3";
this->button3->Size = System::Drawing::Size(75, 23);
this->button3->TabIndex = 3;
this->button3->Text = L"button3";
this->button3->UseVisualStyleBackColor = true;
this->button3->Click += gcnew System::EventHandler(this, &MainForm::button3_Click);
//
// MainForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(292, 266);
this->Controls->Add(this->button3);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Controls->Add(this->checkBox1);
this->Name = L"MainForm";
this->Text = L"MainForm";
this->FormClosing += gcnew System::Windows::Forms::FormClosingEventHandler(this, &MainForm::MainForm_FormClosing);
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
TestMet();
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
CreateAnotherForm();
}
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {
this->anotherForm->Show();
}
private: System::Void MainForm_FormClosing(System::Object^ sender, System::Windows::Forms::FormClosingEventArgs^ e) {
delete this->anotherForm;
e->Cancel = false;
}
};
}
#include "AnotherForm.h"
namespace Forms_3 {
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 MainForm : public System::Windows::Forms::Form
{
public:
MainForm(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
void TestMet(void);
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~MainForm()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::CheckBox^ checkBox1;
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::Button^ button3;
AnotherForm ^anotherForm;
void CreateAnotherForm(void);
protected:
private: System::Windows::Forms::Button^ button1;
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->checkBox1 = (gcnew System::Windows::Forms::CheckBox());
this->button1 = (gcnew System::Windows::Forms::Button());
this->button2 = (gcnew System::Windows::Forms::Button());
this->button3 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// checkBox1
//
this->checkBox1->AutoSize = true;
this->checkBox1->Location = System::Drawing::Point(32, 25);
this->checkBox1->Name = L"checkBox1";
this->checkBox1->Size = System::Drawing::Size(80, 17);
this->checkBox1->TabIndex = 0;
this->checkBox1->Text = L"checkBox1";
this->checkBox1->UseVisualStyleBackColor = true;
//
// button1
//
this->button1->Location = System::Drawing::Point(116, 68);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 1;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &MainForm::button1_Click);
//
// button2
//
this->button2->Location = System::Drawing::Point(117, 113);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(75, 23);
this->button2->TabIndex = 2;
this->button2->Text = L"button2";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &MainForm::button2_Click);
//
// button3
//
this->button3->Location = System::Drawing::Point(116, 154);
this->button3->Name = L"button3";
this->button3->Size = System::Drawing::Size(75, 23);
this->button3->TabIndex = 3;
this->button3->Text = L"button3";
this->button3->UseVisualStyleBackColor = true;
this->button3->Click += gcnew System::EventHandler(this, &MainForm::button3_Click);
//
// MainForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(292, 266);
this->Controls->Add(this->button3);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Controls->Add(this->checkBox1);
this->Name = L"MainForm";
this->Text = L"MainForm";
this->FormClosing += gcnew System::Windows::Forms::FormClosingEventHandler(this, &MainForm::MainForm_FormClosing);
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
TestMet();
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
CreateAnotherForm();
}
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {
this->anotherForm->Show();
}
private: System::Void MainForm_FormClosing(System::Object^ sender, System::Windows::Forms::FormClosingEventArgs^ e) {
delete this->anotherForm;
e->Cancel = false;
}
};
}
Файл MainForm.cpp
Код:
#include "stdafx.h"
#include "MainForm.h"
#include "AnotherForm.h"
void Forms_3::MainForm::TestMet(void)
{
if (checkBox1->Checked)
checkBox1->Checked = false;
else
checkBox1->Checked = true;
}
void Forms_3::MainForm::CreateAnotherForm(void)
{
this->anotherForm = gcnew AnotherForm();
anotherForm->Owner = this;
}
#include "MainForm.h"
#include "AnotherForm.h"
void Forms_3::MainForm::TestMet(void)
{
if (checkBox1->Checked)
checkBox1->Checked = false;
else
checkBox1->Checked = true;
}
void Forms_3::MainForm::CreateAnotherForm(void)
{
this->anotherForm = gcnew AnotherForm();
anotherForm->Owner = this;
}
Файл AnotherForm.h
Код:
#pragma once
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 Forms_3 {
/// <summary>
/// Summary for AnotherForm
///
/// 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 AnotherForm : public System::Windows::Forms::Form
{
public:
AnotherForm(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~AnotherForm()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
protected:
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
void CallTestMet(void);
#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)
{
System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(AnotherForm::typeid));
this->button1 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(127, 54);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &AnotherForm::button1_Click);
//
// AnotherForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(292, 266);
this->Controls->Add(this->button1);
this->Icon = (cli::safe_cast<System::Drawing::Icon^ >(resources->GetObject(L"$this.Icon")));
this->Name = L"AnotherForm";
this->Text = L"AnotherForm";
this->FormClosing += gcnew System::Windows::Forms::FormClosingEventHandler(this, &AnotherForm::AnotherForm_FormClosing);
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
CallTestMet();
}
private: System::Void AnotherForm_FormClosing(System::Object^ sender, System::Windows::Forms::FormClosingEventArgs^ e) {
e->Cancel = true;
this->Hide();
}
};
}
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 Forms_3 {
/// <summary>
/// Summary for AnotherForm
///
/// 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 AnotherForm : public System::Windows::Forms::Form
{
public:
AnotherForm(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~AnotherForm()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
protected:
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
void CallTestMet(void);
#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)
{
System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(AnotherForm::typeid));
this->button1 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(127, 54);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &AnotherForm::button1_Click);
//
// AnotherForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(292, 266);
this->Controls->Add(this->button1);
this->Icon = (cli::safe_cast<System::Drawing::Icon^ >(resources->GetObject(L"$this.Icon")));
this->Name = L"AnotherForm";
this->Text = L"AnotherForm";
this->FormClosing += gcnew System::Windows::Forms::FormClosingEventHandler(this, &AnotherForm::AnotherForm_FormClosing);
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
CallTestMet();
}
private: System::Void AnotherForm_FormClosing(System::Object^ sender, System::Windows::Forms::FormClosingEventArgs^ e) {
e->Cancel = true;
this->Hide();
}
};
}
Файл AnotherForm.cpp
Код:
#include "StdAfx.h"
#include "AnotherForm.h"
#include "MainForm.h"
void Forms_3::AnotherForm::CallTestMet(void)
{
MainForm ^mainForm = (MainForm^) this->Owner;
mainForm->TestMet();
}
#include "AnotherForm.h"
#include "MainForm.h"
void Forms_3::AnotherForm::CallTestMet(void)
{
MainForm ^mainForm = (MainForm^) this->Owner;
mainForm->TestMet();
}