簡體   English   中英

從cpp文件更改文本框

[英]Changing Textbox from the cpp file

我想做的是從主'.cpp'文件中的form1更改textbox

基本上,我希望文本字段在加載時發生變化。 (稍后將是另一個原因)。

抱歉,這是一個愚蠢的問題。 我是新手。

我在Header文件中添加了兩行,並將textbox從private更改為public。 (猜測這不是一個好主意嗎?)

然后,我嘗試在.cpp文件的主要功能中調用更改。

非常感謝你的幫助

.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運行該函數以查看其是否更改了框,但沒有進行任何更改。

因此,我決定查看該函數是否正在運行...然后,我編寫了一個文件,並使用與假定更新文本框相同的函數將其打入該文件。 它寫文件沒問題,但文本框仍然沒有變化,請幫助!

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

注意 :

無需將文本框設為公開,因為可以在班級內訪問私人成員而沒有任何問題。 (文本框在“表單類”中定義)

更新到第二個問題:

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

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

仔細查看您的代碼。 您有兩個不同的Form1對象。
1.在peaches (obj1)處,調用go)函數。 什么也沒發生,因為您的用戶界面當時未運行。

  1. 然后,使用gcnew Form1()創建另一個對象並在UI上進行午餐。 UI不變,因為您在另一個對象中調用了go()

嘗試這個,

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM