c++ - 从 cpp 文件更改文本框

标签 c++ visual-studio textbox

我想做的是从主“.cpp”文件中的 form1 更改 textbox

基本上,我希望文本字段在加载时发生变化。 (后面会是其他原因)。

抱歉,如果这是一个愚蠢的问题。我是新手。

我在头文件中添加了 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) {
             }
    };
}

最佳答案

您可以通过在表单的构造函数中设置文本框文本来执行此操作。

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.
        }

请注意:

无需公开文本框,因为可以毫无问题地在类中访问私有(private)成员。 (文本框在“表单类”中定义)

更新您的第二个问题:

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

试试这个,

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

关于c++ - 从 cpp 文件更改文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31364794/

相关文章:

c++ - 捕获和处理视频时出现性能问题

c++ - 它是正确的随机生成器函数吗?

c++ - Vulkan前脸绕线顺序

c++ - 重载运算符参数太多,visual studio c++

javascript - 使用 jquery,如何像 Live Mail 处理电子邮件地址一样处理标签?

c++ - 为什么标准字符串函数比我的自定义字符串函数更快?

c# - Visual Studio 图标的含义

html - 如何单独且排他地更改 btn-group 颜色?

c# - Sharepoint webpart 属性在一天后消失 (C#)

php - 如何修改文本框区域内的一些数据