2012-11-04 28 views
3

我是一名C++ - CLI初學者,我必須熟悉一些簡單的事情。 我用1個標籤和1個按鈕製作了一個表格。 我想通過點擊按鈕,並呼籲通過虛空中另一CPP類一些文字(changetext.cpp)更改標籤的文本如何更改「Form.h」外部的文本標籤?

結構:

Form1.h //只是一個普通的用1個標籤和1點按鈕的處理程序在結束形式代碼

#pragma once 

namespace ms { 

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

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

public: 
    /// <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->label1 = (gcnew System::Windows::Forms::Label()); 
     this->button1 = (gcnew System::Windows::Forms::Button()); 
     this->SuspendLayout(); 
     // 
     // label1 
     // 
     this->label1->AutoSize = true; 
     this->label1->Location = System::Drawing::Point(313, 140); 
     this->label1->Name = L"label1"; 
     this->label1->Size = System::Drawing::Size(35, 13); 
     this->label1->TabIndex = 0; 
     this->label1->Text = L"label1"; 
     // 
     // button1 
     // 
     this->button1->Location = System::Drawing::Point(101, 117); 
     this->button1->Name = L"button1"; 
     this->button1->Size = System::Drawing::Size(100, 58); 
     this->button1->TabIndex = 1; 
     this->button1->Text = L"button1"; 
     this->button1->UseVisualStyleBackColor = true; 
     this->button1->Click += gcnew System::EventHandler(this,    &Form1::button1_Click); 
     // 
     // Form1 
     // 
     this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); 
     this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; 
     this->ClientSize = System::Drawing::Size(467, 262); 
     this->Controls->Add(this->button1); 
     this->Controls->Add(this->label1); 
     this->Name = L"Form1"; 
     this->Text = L"Form1"; 
     this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load); 
     this->ResumeLayout(false); 
     this->PerformLayout(); 

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


     } 
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { 

      changetext my = new changetext(); 
      my.changeText(); 

     } 
}; 
    } 

ms.cpp(主)//僅創建形式

// ms.cpp : main project file. 

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

using namespace ms; 

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

changetext.cpp

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

int number; 

void changeText() 
{ 
number = 5; 
Form1.label1->Text=number; 

} 

我想提出一個小錯誤的地方,我想它與頭文件的事,我已經習慣了的代碼在java中這樣headerfiles不是我的那杯茶然而。

+1

「Form1」是*類型名稱*,不是變量tha t引用表單對象,因此可以訪問其名爲「label1」的實例成員。一定要區分類和對象,大多數關於面向對象編程的書應該是有用的閱讀。 –

+0

然後,從「Form1.h」調用「label1」的正確方法是什麼? – DauchoT

回答

1

這是做什麼:

在changetext.cpp

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

int number = 5; 

void getNumber() 
{ 
return number; 
} 

創建header.h

#pragma once 
void getNumber(); 

包含header.h和編輯,創建吸氣Form1.h

#pragma once 
#include header.h 

namespace ms { 

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

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

public: 
    /// <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->label1 = (gcnew System::Windows::Forms::Label()); 
     this->button1 = (gcnew System::Windows::Forms::Button()); 
     this->SuspendLayout(); 
     // 
     // label1 
     // 
     this->label1->AutoSize = true; 
     this->label1->Location = System::Drawing::Point(313, 140); 
     this->label1->Name = L"label1"; 
     this->label1->Size = System::Drawing::Size(35, 13); 
     this->label1->TabIndex = 0; 
     this->label1->Text = L"label1"; 
     // 
     // button1 
     // 
     this->button1->Location = System::Drawing::Point(101, 117); 
     this->button1->Name = L"button1"; 
     this->button1->Size = System::Drawing::Size(100, 58); 
     this->button1->TabIndex = 1; 
     this->button1->Text = L"button1"; 
     this->button1->UseVisualStyleBackColor = true; 
     this->button1->Click += gcnew System::EventHandler(this,&Form1::button1_Click); 
     // 
     // Form1 
     // 
     this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); 
     this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; 
     this->ClientSize = System::Drawing::Size(467, 262); 
     this->Controls->Add(this->button1); 
     this->Controls->Add(this->label1); 
     this->Name = L"Form1"; 
     this->Text = L"Form1"; 
     this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load); 
     this->ResumeLayout(false); 
     this->PerformLayout(); 

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


     } 
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { 

label1->Text=""+getNumber(); 

} 
}; 
}