如何从另一个类访问表单元素?

时间:2018-11-09 06:54:14

标签: c++-cli

这是我第一次尝试使用vs clr创建Windows应用程序,我试图在我已经完成的现有cpp项目上实现它,但是我面临的主要问题是我无法访问功能我的主要source.cpp中的myForm.h(按钮,文本框等)的内容,我们将不胜感激,谢谢

MyForm.h

#pragma once
namespace Project2 {

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

protected:
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    ~MyForm()
    {
        if (components)
        {
            delete components;
        }
    }
private: System::Windows::Forms::Button^  button1;
private: System::Windows::Forms::RichTextBox^  richTextBox1;
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->richTextBox1 = (gcnew System::Windows::Forms::RichTextBox());
        this->SuspendLayout();
        // 
        // button1
        // 
        this->button1->Location = System::Drawing::Point(104, 177);
        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, &MyForm::button1_Click);
        // 
        // richTextBox1
        // 
        this->richTextBox1->Location = System::Drawing::Point(47, 36);
        this->richTextBox1->Name = L"richTextBox1";
        this->richTextBox1->Size = System::Drawing::Size(208, 135);
        this->richTextBox1->TabIndex = 1;
        this->richTextBox1->Text = L"";
        this->richTextBox1->TextChanged += gcnew System::EventHandler(this, &MyForm::richTextBox1_TextChanged);
        // 
        // MyForm
        // 
        this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
        this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
        this->ClientSize = System::Drawing::Size(282, 253);
        this->Controls->Add(this->richTextBox1);
        this->Controls->Add(this->button1);
        this->Name = L"MyForm";
        this->Text = L"MyForm";
        this->ResumeLayout(false);
    }

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

Main.c

#include <iostream>
#include <fstream>
#include <sstream>
#include <locale>
#include <string>
#include<vector>
#include <time.h>

#include "MyForm.h"
using namespace std;
using namespace System;

[STAThread]
int main()
{

    System::Windows::Forms::Application::Run(gcnew Project2::MyForm());

    MyForm F; \\ can't create an object of MyForm class

    F.richtextbox->AppendText("Hello world"); \\ here is the confusing part

    System::Windows::Forms::Application::Run(gcnew Project2::MyForm());

    return 0;
}

0 个答案:

没有答案