本文介绍了visual studio 2010 C ++,包括窗体反复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用visual studio 2010.
我有两个窗体相互调用。 Form1包含Form2

I am using visual studio 2010.I have two windows form that calls each other. Form1 has include Form2

我读了,我不能再在Form2包括Form1。但是使用ref类Form1。

i read that i can not include Form1 in Form2 again. but instead use ref class Form1.

但我得到以下错误

代码低于

 #pragma once
namespace spl_project {

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

ref class Form1;
//
/// <summary>
/// Summary for Form2
/// </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::Button^  button1;
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->SuspendLayout();
        //
        // button1
        //
        this->button1->Location = System::Drawing::Point(176, 205);
        this->button1->Name = L"button1";
        this->button1->Size = System::Drawing::Size(75, 23);
        this->button1->TabIndex = 0;
        this->button1->Text = L"button2";
        this->button1->UseVisualStyleBackColor = true;
        this->button1->Click += gcnew System::EventHandler(this, &Form2::button1_Click);
        //
        // Form2
        //
        this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
        this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
        this->ClientSize = System::Drawing::Size(284, 262);
        this->Controls->Add(this->button1);
        this->Name = L"Form2";
        this->Text = L"Form2";
        this->ResumeLayout(false);



    }
#pragma endregion
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {

             Form1 ^ na2 = gcnew Form1();
             na2->Show();

         }
};

}

推荐答案

您必须从声明中分离 button1_Click 的实现。

h只有关于button1_Click的声明:

So put in "Form2.h" only the declaration regarding the "button1_Click":

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

并删除Form2.h中Form1的形式化定义

And also remove the "formward definition" of "Form1" in "Form2.h"

现在创建一个新的文件 Form1.cpp 现在包含实现,其中还包括Forms2.h:

And now create a new File Form1.cpp which now contains the implementation, which also includes the "Forms2.h":

这是Form2.cpp的内容:

This is the content of "Form2.cpp":

#include "Form2.h"
#include "Form1.h"

namespace Form2::spl_project {
  System::Void button1_Click(System::Object^  sender, System::EventArgs^ e) {
    Form1 ^ na2 = gcnew Form1();
    na2->ShowDialog(this);
  }
}

这篇关于visual studio 2010 C ++,包括窗体反复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 15:34