从cpp文件更改文本框

时间:2015-07-12 05:47:33

标签: c++ visual-studio textbox

我要做的是从主要'.cpp'文件中的textbox更改form1

基本上,我希望文本字段在加载时更改。 (后来这将是另一个原因)。

对不起,如果这是一个愚蠢的问题。我是新手。

我在Header文件中添加了2行,并将textbox从private更改为public。 (猜测这不是一个好主意?)

然后我尝试调用.cpp文件的main函数中的更改。

非常感谢你的帮助

.cpp档案

// help.cpp : main project file.

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

using namespace help;

[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());

    //Added this.
    Form1^ myform1 = gcnew Form1();
    Form1::myForm1->MyBox->Text = L" ShowME! ";


    return 0;
}

form1.h档案

#pragma once

namespace help {
    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:
        static Form1^ myForm1; //Add this... 
        Form1(void)
        {
            InitializeComponent();
            myForm1 = this; //added this...
            //
            //TODO: Add the constructor code here
            //
        }

    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~Form1()
        {
            if (components)
            {
                delete components;
            }
        }
    public: System::Windows::Forms::TextBox^  MyBox; //Changed To Public..
    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->MyBox = (gcnew System::Windows::Forms::TextBox());
            this->SuspendLayout();
            // 
            // MyBox
            // 
            this->MyBox->Location = System::Drawing::Point(20, 57);
            this->MyBox->Name = L"MyBox";
            this->MyBox->Size = System::Drawing::Size(247, 20);
            this->MyBox->TabIndex = 0;
            this->MyBox->TextChanged += gcnew System::EventHandler(this, &Form1::MyBox_TextChanged);
            // 
            // Form1
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(284, 261);
            this->Controls->Add(this->MyBox);
            this->Name = L"Form1";
            this->Text = L"Form1";
            this->ResumeLayout(false);
            this->PerformLayout();

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

更新....

我还在努力解决这个问题。今天我早早醒来再试一次,我似乎无法让它发挥作用。

我尝试在From1.h上创建一个名为“go”的函数,然后从cpp运行该函数以查看它是否更改了框而没有任何内容。

然后我决定看看该函数是否正在运行...然后我写了一个文件并使用假设更新文本框的相同函数将hello放入该文件中。它写的文件没问题,但文本框仍然没有改变请帮忙!

cpp文件

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

using namespace test6;

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{




    // Enabling Windows XP visual effects before any controls are created
    Application::EnableVisualStyles();
    Application::SetCompatibleTextRenderingDefault(false); 


    Form1 peaches;
    peaches.go();



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





    return 0;
}

Form1.h

#include <iostream>
#include <sstream>
#include <String>
#pragma once

namespace test6 {

    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
            //



        }

        void go() {

            //This doesn't update the Box?
            MyBox->Text = ("Hello");


            //This does make a file with hello in it. 
            FILE * fname = fopen("text.txt","w");
            fprintf(fname, "Hello");
            fclose(fname);




        }

    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~Form1()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::TextBox^  MyBox;
    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->MyBox = (gcnew System::Windows::Forms::TextBox());
            this->button1 = (gcnew System::Windows::Forms::Button());
            this->SuspendLayout();
            // 
            // MyBox
            // 
            this->MyBox->Location = System::Drawing::Point(99, 112);
            this->MyBox->Name = L"MyBox";
            this->MyBox->Size = System::Drawing::Size(100, 20);
            this->MyBox->TabIndex = 0;
            this->MyBox->TextChanged += gcnew System::EventHandler(this, &Form1::MyBox_TextChanged);
            // 
            // button1
            // 
            this->button1->Location = System::Drawing::Point(110, 178);
            this->button1->Name = L"button1";
            this->button1->Size = System::Drawing::Size(75, 23);
            this->button1->TabIndex = 1;
            this->button1->Text = L"button1";
            this->button1->UseVisualStyleBackColor = true;
            // 
            // Form1
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(284, 261);
            this->Controls->Add(this->button1);
            this->Controls->Add(this->MyBox);
            this->Name = L"Form1";
            this->Text = L"Form1";
            this->ResumeLayout(false);
            this->PerformLayout();

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

1 个答案:

答案 0 :(得分:2)

您可以在表单的构造函数中设置文本框文本。

Form1(void)
        {
            InitializeComponent();
            myForm1 = this; //added this... (?? no need)
            //
            //TODO: Add the constructor code here
            MyBox.Text = "Result is 22"; //where MyBox is your text box object.
        }

请注意:

无需公开文本框,因为私有成员可以在课堂内访问而不会出现任何问题。 (文本框在“表单类”中定义)

更新您的第二个问题:

Form1 peaches; // obj1
peaches.go();

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

仔细查看您的代码。您有两个不同的Form1对象 1.在peaches(obj1),您调用go)函数。没有任何事情发生,因为你的UI没有在那一刻运行。

  1. 然后使用gcnew Form1()创建另一个对象并按下UI。 UI没有变化,因为您在另一个对象中调用了go()
  2. 试试这个,

    Form1 ^peaches = gcnew Form1(); 
    // Create the main window and run it
    Application::Run(peaches);
    peaches->go();