2011-04-07 22 views
1

我需要將類「public ref class Form1」中的一些變量傳遞給對象「btnevaluate_Click」。我該怎麼做呢?下面是代碼:如何在C++/CLI中將變量從Class傳遞到Object

#pragma once 

#include <cstdlib> 
#include <ctime> 

namespace CIS170CLab6B { 

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 
/// 
/// WARNING: If you change the name of this class, you will need to change the 
///   'Resource File Name' property for the managed resource compiler tool 
///   associated with all .resx files this class depends on. Otherwise, 
///   the designers will not be able to interact properly with localized 
///   resources associated with this form. 
/// </summary> 
public ref class Form1 : public System::Windows::Forms::Form 
{ 
public: 
    Form1(void) 
    { 
     InitializeComponent(); 
     // 
     //TODO: Add the constructor code here 
     // 
     int guess = 0; 
     srand((unsigned)time(0)); 
     int randNumber = rand() % 100; 
    } 

protected: 
    /// <summary> 
    /// Clean up any resources being used. 
    /// </summary> 
    ~Form1() 
    { 
     if (components) 
     { 
      delete components; 
     } 
    } 
private: System::Windows::Forms::Label^ lblguessNum; 
protected: 
private: System::Windows::Forms::TextBox^ txtGuessNum; 
private: System::Windows::Forms::Label^ lblhighlow; 
private: System::Windows::Forms::Button^ btnevaluate; 
private: System::Windows::Forms::Button^ btnclear; 

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->lblguessNum = (gcnew System::Windows::Forms::Label()); 
     this->txtGuessNum = (gcnew System::Windows::Forms::TextBox()); 
     this->lblhighlow = (gcnew System::Windows::Forms::Label()); 
     this->btnevaluate = (gcnew System::Windows::Forms::Button()); 
     this->btnclear = (gcnew System::Windows::Forms::Button()); 
     this->SuspendLayout(); 
     // 
     // lblguessNum 
     // 
     this->lblguessNum->AutoSize = true; 
     this->lblguessNum->Location = System::Drawing::Point(40, 32); 
     this->lblguessNum->Name = L"lblguessNum"; 
     this->lblguessNum->Size = System::Drawing::Size(111, 13); 
     this->lblguessNum->TabIndex = 0; 
     this->lblguessNum->Text = L"Enter a guess 0 - 100:"; 
     // 
     // txtGuessNum 
     // 
     this->txtGuessNum->Location = System::Drawing::Point(158, 32); 
     this->txtGuessNum->Name = L"txtGuessNum"; 
     this->txtGuessNum->Size = System::Drawing::Size(66, 20); 
     this->txtGuessNum->TabIndex = 1; 
     // 
     // lblhighlow 
     // 
     this->lblhighlow->AutoSize = true; 
     this->lblhighlow->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9.75F, static_cast<System::Drawing::FontStyle>((System::Drawing::FontStyle::Bold | System::Drawing::FontStyle::Italic)), 
      System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(0))); 
     this->lblhighlow->Location = System::Drawing::Point(115, 95); 
     this->lblhighlow->Name = L"lblhighlow"; 
     this->lblhighlow->Size = System::Drawing::Size(51, 16); 
     this->lblhighlow->TabIndex = 2; 
     this->lblhighlow->Text = L"label1"; 
     this->lblhighlow->Visible = false; 
     // 
     // btnevaluate 
     // 
     this->btnevaluate->Location = System::Drawing::Point(43, 148); 
     this->btnevaluate->Name = L"btnevaluate"; 
     this->btnevaluate->Size = System::Drawing::Size(207, 53); 
     this->btnevaluate->TabIndex = 3; 
     this->btnevaluate->Text = L"Press to EVALUATE your guess!"; 
     this->btnevaluate->UseVisualStyleBackColor = true; 
     this->btnevaluate->Click += gcnew System::EventHandler(this, &Form1::btnevaluate_Click); 
     // 
     // btnclear 
     // 
     this->btnclear->Location = System::Drawing::Point(57, 232); 
     this->btnclear->Name = L"btnclear"; 
     this->btnclear->Size = System::Drawing::Size(181, 62); 
     this->btnclear->TabIndex = 4; 
     this->btnclear->Text = L"Press to CLEAR your guess and TRY AGAIN!"; 
     this->btnclear->UseVisualStyleBackColor = true; 
     this->btnclear->Visible = false; 
     this->btnclear->Click += gcnew System::EventHandler(this, &Form1::btnclear_Click); 
     // 
     // Form1 
     // 
     this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); 
     this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; 
     this->BackColor = System::Drawing::SystemColors::MenuHighlight; 
     this->ClientSize = System::Drawing::Size(292, 333); 
     this->Controls->Add(this->btnclear); 
     this->Controls->Add(this->btnevaluate); 
     this->Controls->Add(this->lblhighlow); 
     this->Controls->Add(this->txtGuessNum); 
     this->Controls->Add(this->lblguessNum); 
     this->Name = L"Form1"; 
     this->Text = L"Guessing Game"; 
     this->ResumeLayout(false); 
     this->PerformLayout(); 

    } 
#pragma endregion 
private: System::Void btnevaluate_Click(System::Object^ sender, System::EventArgs^ e) { 
     guess++; 
     if(Convert::ToInt32(this->txtGuessNum->Text) == randNumber) 
      { 
      MessageBox::Show("You are right!! It took you " + guess + " guesses."); 
      Close(); 
      } 
     else if(Convert::ToInt32(this->txtGuessNum->Text) < randNumber) 
      this->lblhighlow->Text = "Too Low!!"; 
     else 
      this->lblhighlow->Text = "Too High!!"; 
     this->lblhighlow->Visible = true; 
     this->btnclear->Visible = true; 
     this->btnclear->Focus(); 
     this->btnevaluate->Visible = false; 
     } 
private: System::Void btnclear_Click(System::Object^ sender, System::EventArgs^ e) { 
     this->lblhighlow->Visible = false; 
     this->btnclear->Visible = false; 
     this->btnevaluate->Visible = true; 
     this->txtGuessNum->Focus(); 
     this->txtGuessNum->Clear(); 
    } 
}; 
} 

回答

3

,我需要已傳遞到對象「btnevaluate_Click」

這不是一個對象,這是一個方法。 Form1類的實例方法。這使您可以自由訪問該課程的成員。只是聲明爲private成員:

private: 
    int guess; 
    int randNumber; 

    System::Void btnevaluate_Click(System::Object^ sender, System::EventArgs^ e) { 
     // etc.. 
    } 

和修復構造,他們沒有做任何好局部變量:

Form1(void) 
    { 
     InitializeComponent(); 
     guess = 0; 
     srand((unsigned)time(0)); 
     randNumber = rand() % 100; 
    } 

還挺重要的是要得到這個權利順便說一句,非常難寫代碼否則正確。查看您最喜愛的C++/CLI編程手冊,瞭解類的結構和變量的範圍。避免試驗和錯誤。

+0

非常感謝您清理那個。我討厭當我做了大量的編碼,並最終陷入困境。 – 2011-04-07 04:40:29

+1

您選擇了合適的用戶名。當你開始學習語言時,不要做很多沒有測試的編碼。 – 2011-04-07 05:06:55