开发者

How to pass variables from Class to Object in C++/CLI

I have some variables in the class "public ref class Form1" that I need to have passed into the object "btnevaluate_Click". How do I do this? Here is the code:

#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();
     }
};
}


that I need to have passed into the object "btnevaluate_Click"

That's not an object, it's a method. An instance method of the Form1 class. Which allows you to freely access the members of that class. Just declare them as private members:

private: 
    int guess;
    int randNumber;

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

And fix the constructor, they don't do any good as local variables:

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

Kinda important to get this right btw, very hard to write code correctly otherwise. Review your favorite C++/CLI programming book about the structure of a class and the scope of variables. Avoid trial and error.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜