Как получить доступ к элементам формы из других классов? - PullRequest
0 голосов
/ 09 ноября 2018

Это мой первый раз, когда я пытаюсь создать приложение для Windows, используя vs clr, и я пытаюсь реализовать его в моем существующем проекте cpp, который я почти закончил, но главная проблема, с которой я сталкиваюсь, это то, что я не могу получить доступ к функциям myForm.h (кнопки, текстовое поле и т. д.) из моего основного source.cpp, любая помощь будет оценена и спасибо

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;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...