在Form.h之外更改表单标签文本

时间:2012-07-22 23:02:10

标签: c++ .net winforms visual-c++ c++-cli

我正在学习Visual C ++,并且在第1天遇到了相当绊脚石。我正在尝试使用Visual C ++ 2010中的Form模板创建一个简单的“Hello World”应用程序。

解决方案创建了Form1.h,resource.h,stdafx.h,AssemblyInfo.cpp,helloworld.cpp和stdafx.cpp。我向Form1添加了一个按钮和一个标签,并且能够使它在单击按钮时标签从Form1.h中将文本更改为“Hello World”。我想从Form1.cpp这样做,所以我从标题中删除了代码。我目前有以下代码:

Form1.cpp

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

using namespace helloworld;


helloworld::Void testButton_Click(System::Object^  sender, System::EventArgs^  e) {
    helloworld::Form1::label1->Text = "Test";
}

Form1.h

#pragma once

namespace helloworld {

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

    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~Form1()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::Button^  testButton;
    public: System::Windows::Forms::Label^  label1;
    protected: 

    private:
        /// <summary>
        /// Required designer variable.
        /// </summary>testButton
        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->testButton = (gcnew System::Windows::Forms::Button());
            this->label1 = (gcnew System::Windows::Forms::Label());
            this->SuspendLayout();
            // 
            // testButton
            // 
            this->testButton->Location = System::Drawing::Point(136, 159);
            this->testButton->Name = L"testButton";
            this->testButton->Size = System::Drawing::Size(75, 23);
            this->testButton->TabIndex = 0;
            this->testButton->Text = L"Test Button";
            this->testButton->UseVisualStyleBackColor = true;
            this->testButton->Click += gcnew System::EventHandler(this, &Form1::testButton_Click);
            // 
            // label1
            // 
            this->label1->AutoSize = true;
            this->label1->Location = System::Drawing::Point(125, 100);
            this->label1->Name = L"label1";
            this->label1->Size = System::Drawing::Size(35, 13);
            this->label1->TabIndex = 1;
            this->label1->Text = L"label1";
            // 
            // Form1
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(284, 262);
            this->Controls->Add(this->label1);
            this->Controls->Add(this->testButton);
            this->Name = L"Form1";
            this->Text = L"Form1";
            this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
            this->ResumeLayout(false);
            this->PerformLayout();

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

使用此代码,我收到以下错误:

error C2227: left of '->Text' must point to class/struct/union/generic type

根据我的发现,该错误意味着该程序不知道我在谈论label1->Text的内容。我非常感谢任何可以提供给Visual C ++新手的帮助。

1 个答案:

答案 0 :(得分:3)

问题出在以下代码段中:

helloworld::Void testButton_Click(System::Object^  sender, System::EventArgs^  e) {
    helloworld::Form1::label1->Text = "Test";
}

helloworld::Form1是您表单的类型;它不是实际的实例。我们使用this关键字来引用当前对象实例(大多数时候this关键字是可选的)。另一个问题是helloworld::Void,应该是System::Void。您还需要将testButton_Click替换为Form1::testButton_Click,以表明这是一个类方法而不是自由函数。您的代码应该成为:

System::Void Form1::testButton_Click(System::Object^  sender, System::EventArgs^  e) {
    label1->Text = "Test";
}

注意:使用Windows窗体时应使用C#。 C ++ / CLI是一种语言混乱,大多数人只有在使用本机代码进行组合时才使用C ++ / CLI。

相关问题