Windows 窗体应用程序:访问其他窗体中的元素 Visual Studio 2012 C++

Posted

技术标签:

【中文标题】Windows 窗体应用程序:访问其他窗体中的元素 Visual Studio 2012 C++【英文标题】:Windows Forms Application: accessing elements in other forms visual studio 2012 c++ 【发布时间】:2013-01-28 10:03:37 【问题描述】:

使用 c++ vs2010 访问(添加)Form2 的元素到 Form1 工作得很好。 如果我在 Visual Studio Express 2012 中从头开始尝试相同的操作,我会不断收到错误消息,指出 Form2 是未声明的标识符。有什么想法有什么问题吗?

    #include "stdafx.h"
    #include "Form2.h"
    ....
    
    Form2^ frm = gcnew Form2;
    this->Controls->Add(frm->panel1);

错误 C2065:“Form2”:未声明的标识符

代码表格1:

包括“stdafx.h”

包括“Form2.h”

    #pragma once

    namespace WindowsFormsApplication

        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
        /// </summary>
        public ref class Form1 : public System::Windows::Forms::Form
        
        public:
            Form1(void)
            
                InitializeComponent();
                //
                //TODO: Add the constructor code here
                //
            

        protected:
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            ~Form1()
            
                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(31, 32);
                this->button1->Name = L"button1";
                this->button1->Size = System::Drawing::Size(75, 27);
                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, 13);
                this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
                this->ClientSize = System::Drawing::Size(284, 262);
                this->Controls->Add(this->button1);
                this->Name = L"Form1";
                this->Text = L"Form1";
                this->ResumeLayout(false);

            
    #pragma endregion
        private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
                     Form2^ frm = gcnew Form2;
            this->Controls->Add(frm->panel1);
             
                 
        ;
    

代码表格2:

    #pragma once

    namespace Windows_Forms_Application

        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;
                
            
        public: System::Windows::Forms::Panel^  panel1;
        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->panel1 = (gcnew System::Windows::Forms::Panel());
                this->SuspendLayout();
                // 
                // panel1
                // 
                this->panel1->BackColor =        System::Drawing::SystemColors::ActiveCaptionText;
                this->panel1->Location = System::Drawing::Point(106, 85);
                this->panel1->Name = L"panel1";
                this->panel1->Size = System::Drawing::Size(132, 118);
                this->panel1->TabIndex = 0;
                // 
                // 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->panel1);
                this->Name = L"Form2";
                this->Text = L"Form2";
                this->ResumeLayout(false);

            
    #pragma endregion
        ;
    

代码cpp:

    // Windows Forms Application.cpp : main project file.

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

    using namespace WindowsFormsApplication;

    [STAThreadAttribute]
    int main(array<System::String ^> ^args)
    
        // Enabling Windows XP visual effects before any controls are created
        Application::EnableVisualStyles();
        Application::SetCompatibleTextRenderingDefault(false); 

        // Create the main window and run it
        Application::Run(gcnew Form1());
        return 0;
    

【问题讨论】:

请显示Form2.h的代码。 【参考方案1】:

尝试使用类前向声明​​。这是您的问题的最可能原因。

如果您不确定如何操作,这里有一个示例 (C++ - 2 classes 1 file)。 在您的情况下,请尝试在

之后添加以下行
#include "stdafx.h"
#include "Form2.h"
class form2;

您也可以尝试添加

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

class form1;

如果这不起作用,我们需要查看更多代码和文件结构。 您需要前向声明,因为它有助于编译器了解代码中存在哪些函数和对象以及如何访问它们。

【讨论】:

已解决:命名空间声明与下划线不一致 - 感谢您的帮助!

以上是关于Windows 窗体应用程序:访问其他窗体中的元素 Visual Studio 2012 C++的主要内容,如果未能解决你的问题,请参考以下文章

vs2015中的c#windows窗体应用程序怎么使用OpenGL?

从 C# 中的外部 DLL 访问 windows 窗体控件

C# - 从线程更新 windows 窗体元素

C#Windows窗体 - 调整窗体大小时移动元素

如何运行Windows窗体应用程序而不安装SQL Server Studio但从其数据库访问实时数据(服务器)

非托管代码中的 Windows 窗体?