Microsoft Visual C ++ 2010打开另一种形式

时间:2012-08-09 16:00:24

标签: forms c++-cli

我正在尝试这样做,以便当我单击我的Visual C ++项目上的一个按钮时,它会打开另一个表单,有点像java JOptionPane.showInputDialog,但它看起来像我想要的方式。我试图用Form21^form2=gcnew Form21(); form2->ShowDialog();打开它 但它只是说

1>c:\users\steve\documents\visual studio 2010\projects\lesson 2\lesson 2\Form1.h(253): error C2065: 'Form21' : undeclared identifier
1>c:\users\steve\documents\visual studio 2010\projects\lesson 2\lesson 2\Form1.h(253): error C2065: 'form2' : undeclared identifier
1>c:\users\steve\documents\visual studio 2010\projects\lesson 2\lesson 2\Form1.h(253): error C2061: syntax error : identifier 'Form21'
1>c:\users\steve\documents\visual studio 2010\projects\lesson 2\lesson 2\Form1.h(254): error C2065: 'form2' : undeclared identifier
1>c:\users\steve\documents\visual studio 2010\projects\lesson 2\lesson 2\Form1.h(254): error C2227: left of '->ShowDialog' must point to class/struct/union/generic type
1>          type is ''unknown-type''

表单2应该都可以,但这里是代码

#pragma once

namespace Lesson2 {

    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 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::Label^  label1;
    private: System::Windows::Forms::TextBox^  textBox1;
    private: System::Windows::Forms::Label^  label2;
    private: System::Windows::Forms::TextBox^  textBox2;
    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->label1 = (gcnew System::Windows::Forms::Label());
            this->textBox1 = (gcnew System::Windows::Forms::TextBox());
            this->label2 = (gcnew System::Windows::Forms::Label());
            this->textBox2 = (gcnew System::Windows::Forms::TextBox());
            this->SuspendLayout();
            // 
            // label1
            // 
            this->label1->AutoSize = true;
            this->label1->Location = System::Drawing::Point(12, 9);
            this->label1->Name = L"label1";
            this->label1->Size = System::Drawing::Size(61, 13);
            this->label1->TabIndex = 0;
            this->label1->Text = L"Username :";
            this->label1->Click += gcnew System::EventHandler(this, &Form2::label1_Click);
            // 
            // textBox1
            // 
            this->textBox1->Location = System::Drawing::Point(81, 8);
            this->textBox1->Name = L"textBox1";
            this->textBox1->Size = System::Drawing::Size(108, 20);
            this->textBox1->TabIndex = 1;
            this->textBox1->TextChanged += gcnew System::EventHandler(this, &Form2::textBox1_TextChanged);
            // 
            // label2
            // 
            this->label2->AutoSize = true;
            this->label2->Location = System::Drawing::Point(12, 54);
            this->label2->Name = L"label2";
            this->label2->Size = System::Drawing::Size(59, 13);
            this->label2->TabIndex = 2;
            this->label2->Text = L"Password: ";
            // 
            // textBox2
            // 
            this->textBox2->Location = System::Drawing::Point(81, 47);
            this->textBox2->Name = L"textBox2";
            this->textBox2->Size = System::Drawing::Size(108, 20);
            this->textBox2->TabIndex = 3;
            // 
            // Form2
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(211, 88);
            this->Controls->Add(this->textBox2);
            this->Controls->Add(this->label2);
            this->Controls->Add(this->textBox1);
            this->Controls->Add(this->label1);
            this->DoubleBuffered = true;
            this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedSingle;
            this->MaximizeBox = false;
            this->MinimizeBox = false;
            this->Name = L"Form2";
            this->Text = L"Create an account";
            this->ResumeLayout(false);
            this->PerformLayout();

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

我知道它说的是第二课,但那只是因为我在教自己一些东西。我拿起这种东西真的很容易。除非发生这种情况。有人看到什么出错了吗?

1 个答案:

答案 0 :(得分:1)

Form21很可能在另一个文件中声明,因此编译器无法找到Form21类型。

在Form1.h的顶部,添加#include "Form21.h"(或者包含Form21的类声明的头文件的名称,如果它不同的话)。

我还建议您阅读有关header和cpp文件(您似乎在头文件中有代码),前向声明和其他一些关于c ++ / cli的基本内容。

编辑:发布Form2代码后,错误是行Form21^form2=gcnew Form21();应为Form2^form2=gcnew Form2();。您声明了Form2,而不是Form21

祝你好运!