2012-07-22 27 views
0

我正在學習Visual C++,並且在第1天遇到了相當大的絆腳石。我嘗試使用Visual C++ 2010中的表單模板製作一個簡單的「Hello World」應用程序。在Form.h之外更改表單標籤文本

解決方案創建了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++新手的任何幫助。

+0

您是否嘗試將'label1-> Text'更改爲'label1.Text'?看起來可能是一個語法錯誤。 – 2012-07-22 23:05:48

+0

@DougRamsey .Text產生:錯誤C2228:'.Text'的左邊必須有class/struct/union – Jeremy1026 2012-07-22 23:06:50

+0

解決方案:使用C#。 – Marlon 2012-07-22 23:11:41

回答

3

的問題是下面的代碼片段:

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

helloworld::Form1是你的窗體的類型;它不是實際的實例。我們使用this關鍵字來引用當前對象實例(大多數情況下this關鍵字是可選的)。另一個問題是helloworld::Void,應該是System::Void。您還需要將替換爲Form1::testButton_Click以顯示這是一個類方法,而不是一個自由函數。您的代碼應該變成:

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

注:與Windows窗體的工作時,應該使用C#。 C++/CLI是一種混亂的語言,大多數人只有在使用本機代碼進行管理時才使用C++/CLI。

+0

我改變Form1.cpp到: '#包括 「stdafx.h中」 的#include 「stdio.h中」 的#include 「Form1.h」 使用命名空間的HelloWorld; System :: Void testButton_Click(System :: Object^sender,System :: EventArgs^e){0} {> Text =「Test」; }' 但是錯誤仍然存​​在,現在我得到'label1':未聲明的標識符。 – Jeremy1026 2012-07-22 23:20:26

+0

@ Jeremy1026哦。我沒有看到這個錯誤(它應該解釋你的上述錯誤):你需要用'Form1 :: testButton_Click'來改變'testButton_Click',以表明這是一個方法而不是一個自由函數。 – Marlon 2012-07-22 23:21:27

+0

完美!謝謝。 – Jeremy1026 2012-07-22 23:23:00