Visual C ++ Windows窗体阻止对象被删除

时间:2016-07-01 21:51:09

标签: winforms visual-studio visual-c++ c++-cli

我是Visual C ++ Windows Forms的初学者,我有一些很大的问题(我几天都找不到解决方案......) 基本上我有基本程序表格在线模板。我添加了一些文本框,标签,按钮和一个类。 Class是一张BattleCard,它可以用于"战斗"使用它的Defend()函数并使用文本框Text作为他们的统计数据。问题出现时,我试图使这像回合基础,所以你需要每一轮点击开始按钮...一切都很好,除了事实,一旦我点击按钮,所有计算完成...对象和所有统计数据是删除。这意味着对象将从启动HP等开始,而不是刚刚计算的对象。你是否有任何想法如何解决它或只是使这些对象"不可取出"?

BattleCard.h

#pragma once
ref class BattleCard
{
public:
    int Attack;
    int Defense;
    int HP;
    bool Exist = false;
    void Defend(int Attack, int Defense);
    BattleCard(int A, int D, int H);
};

BattleCard.cpp

#include "BattleCard.h"

BattleCard::BattleCard(int A, int D, int H)
{
    if (Exist == false) { Attack = A; Defense = D;  HP = H; Exist = true; }
}

void BattleCard::Defend(int Attack, int Defense)
{
    this->HP = (double)this->HP - (double)Attack*((double)1 - ((double)Defense / ((double)Defense + (double)50)));
}

MyForm.h

#pragma once
#include <stdlib.h>
#include <array>
#include <string>
#include <iostream>
#include "BattleCard.h"

namespace CppWinForm2 {

    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 MyForm
    /// </summary>
    public ref class MyForm : public System::Windows::Forms::Form
    {
    public:
        MyForm(void)
        {
            InitializeComponent();
            //
            //TODO: Add the constructor code here
            //
        }

    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~MyForm()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::TextBox^  textBox1;
    protected:
    private: System::Windows::Forms::Panel^  panel1;
    private: System::Windows::Forms::Label^  label3;
    private: System::Windows::Forms::Label^  label2;
    private: System::Windows::Forms::Label^  label1;
    private: System::Windows::Forms::TextBox^  textBox3;
    private: System::Windows::Forms::TextBox^  textBox2;
    private: System::Windows::Forms::Panel^  panel2;
    private: System::Windows::Forms::Label^  label4;
    private: System::Windows::Forms::Label^  label5;
    private: System::Windows::Forms::Label^  label6;
    private: System::Windows::Forms::TextBox^  textBox4;
    private: System::Windows::Forms::TextBox^  textBox5;
    private: System::Windows::Forms::TextBox^  textBox6;
    private: System::Windows::Forms::Button^  button1;
    private: System::Windows::Forms::Label^  label7;
    private: System::Windows::Forms::Label^  label8;

    private: System::ComponentModel::IContainer^  components;




    protected:

    private:
        /// <summary>
        /// Required designer variable.
        /// </summary>


#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->textBox1 = (gcnew System::Windows::Forms::TextBox());
            this->panel1 = (gcnew System::Windows::Forms::Panel());
            this->label3 = (gcnew System::Windows::Forms::Label());
            this->label2 = (gcnew System::Windows::Forms::Label());
            this->label1 = (gcnew System::Windows::Forms::Label());
            this->textBox3 = (gcnew System::Windows::Forms::TextBox());
            this->textBox2 = (gcnew System::Windows::Forms::TextBox());
            this->panel2 = (gcnew System::Windows::Forms::Panel());
            this->label4 = (gcnew System::Windows::Forms::Label());
            this->label5 = (gcnew System::Windows::Forms::Label());
            this->label6 = (gcnew System::Windows::Forms::Label());
            this->textBox4 = (gcnew System::Windows::Forms::TextBox());
            this->textBox5 = (gcnew System::Windows::Forms::TextBox());
            this->textBox6 = (gcnew System::Windows::Forms::TextBox());
            this->button1 = (gcnew System::Windows::Forms::Button());
            this->label7 = (gcnew System::Windows::Forms::Label());
            this->label8 = (gcnew System::Windows::Forms::Label());
            this->panel1->SuspendLayout();
            this->panel2->SuspendLayout();
            this->SuspendLayout();
            // 
            // textBox1
            // 
            this->textBox1->Location = System::Drawing::Point(106, 0);
            this->textBox1->Name = L"textBox1";
            this->textBox1->Size = System::Drawing::Size(100, 20);
            this->textBox1->TabIndex = 0;
            // 
            // panel1
            // 
            this->panel1->Controls->Add(this->label3);
            this->panel1->Controls->Add(this->label2);
            this->panel1->Controls->Add(this->label1);
            this->panel1->Controls->Add(this->textBox3);
            this->panel1->Controls->Add(this->textBox2);
            this->panel1->Controls->Add(this->textBox1);
            this->panel1->Location = System::Drawing::Point(365, 202);
            this->panel1->Name = L"panel1";
            this->panel1->Size = System::Drawing::Size(206, 100);
            this->panel1->TabIndex = 1;
            // 
            // label3
            // 
            this->label3->AutoSize = true;
            this->label3->Location = System::Drawing::Point(1, 81);
            this->label3->Name = L"label3";
            this->label3->Size = System::Drawing::Size(22, 13);
            this->label3->TabIndex = 5;
            this->label3->Text = L"HP";
            // 
            // label2
            // 
            this->label2->AutoSize = true;
            this->label2->Location = System::Drawing::Point(1, 45);
            this->label2->Name = L"label2";
            this->label2->Size = System::Drawing::Size(47, 13);
            this->label2->TabIndex = 4;
            this->label2->Text = L"Defense";
            // 
            // label1
            // 
            this->label1->AutoSize = true;
            this->label1->Location = System::Drawing::Point(1, 2);
            this->label1->Name = L"label1";
            this->label1->Size = System::Drawing::Size(38, 13);
            this->label1->TabIndex = 3;
            this->label1->Text = L"Attack";
            this->label1->Click += gcnew System::EventHandler(this, &MyForm::label1_Click);
            // 
            // textBox3
            // 
            this->textBox3->Location = System::Drawing::Point(106, 80);
            this->textBox3->Name = L"textBox3";
            this->textBox3->Size = System::Drawing::Size(100, 20);
            this->textBox3->TabIndex = 2;
            // 
            // textBox2
            // 
            this->textBox2->Location = System::Drawing::Point(106, 40);
            this->textBox2->Name = L"textBox2";
            this->textBox2->Size = System::Drawing::Size(100, 20);
            this->textBox2->TabIndex = 1;
            // 
            // panel2
            // 
            this->panel2->Controls->Add(this->label4);
            this->panel2->Controls->Add(this->label5);
            this->panel2->Controls->Add(this->label6);
            this->panel2->Controls->Add(this->textBox4);
            this->panel2->Controls->Add(this->textBox5);
            this->panel2->Controls->Add(this->textBox6);
            this->panel2->Location = System::Drawing::Point(595, 202);
            this->panel2->Name = L"panel2";
            this->panel2->Size = System::Drawing::Size(206, 100);
            this->panel2->TabIndex = 6;
            // 
            // label4
            // 
            this->label4->AutoSize = true;
            this->label4->Location = System::Drawing::Point(3, 83);
            this->label4->Name = L"label4";
            this->label4->Size = System::Drawing::Size(22, 13);
            this->label4->TabIndex = 5;
            this->label4->Text = L"HP";
            // 
            // label5
            // 
            this->label5->AutoSize = true;
            this->label5->Location = System::Drawing::Point(3, 45);
            this->label5->Name = L"label5";
            this->label5->Size = System::Drawing::Size(47, 13);
            this->label5->TabIndex = 4;
            this->label5->Text = L"Defense";
            // 
            // label6
            // 
            this->label6->AutoSize = true;
            this->label6->Location = System::Drawing::Point(3, 2);
            this->label6->Name = L"label6";
            this->label6->Size = System::Drawing::Size(38, 13);
            this->label6->TabIndex = 3;
            this->label6->Text = L"Attack";
            // 
            // textBox4
            // 
            this->textBox4->Location = System::Drawing::Point(106, 80);
            this->textBox4->Name = L"textBox4";
            this->textBox4->Size = System::Drawing::Size(100, 20);
            this->textBox4->TabIndex = 2;
            // 
            // textBox5
            // 
            this->textBox5->Location = System::Drawing::Point(106, 40);
            this->textBox5->Name = L"textBox5";
            this->textBox5->Size = System::Drawing::Size(100, 20);
            this->textBox5->TabIndex = 1;
            // 
            // textBox6
            // 
            this->textBox6->Location = System::Drawing::Point(106, 0);
            this->textBox6->Name = L"textBox6";
            this->textBox6->Size = System::Drawing::Size(100, 20);
            this->textBox6->TabIndex = 0;
            // 
            // button1
            // 
            this->button1->Location = System::Drawing::Point(545, 326);
            this->button1->Name = L"button1";
            this->button1->Size = System::Drawing::Size(75, 23);
            this->button1->TabIndex = 7;
            this->button1->Text = L"FIGHT!";
            this->button1->UseVisualStyleBackColor = true;
            this->button1->Click += gcnew System::EventHandler(this, &MyForm::button1_Click);
            // 
            // label7
            // 
            this->label7->AutoSize = true;
            this->label7->Location = System::Drawing::Point(543, 435);
            this->label7->Name = L"label7";
            this->label7->Size = System::Drawing::Size(28, 13);
            this->label7->TabIndex = 8;
            this->label7->Text = L"XXX";
            // 
            // label8
            // 
            this->label8->AutoSize = true;
            this->label8->Location = System::Drawing::Point(592, 435);
            this->label8->Name = L"label8";
            this->label8->Size = System::Drawing::Size(28, 13);
            this->label8->TabIndex = 9;
            this->label8->Text = L"XXX";
            // 
            // MyForm
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(1280, 720);
            this->Controls->Add(this->label8);
            this->Controls->Add(this->label7);
            this->Controls->Add(this->button1);
            this->Controls->Add(this->panel2);
            this->Controls->Add(this->panel1);
            this->Name = L"MyForm";
            this->Text = L"MyForm";
            this->panel1->ResumeLayout(false);
            this->panel1->PerformLayout();
            this->panel2->ResumeLayout(false);
            this->panel2->PerformLayout();
            this->ResumeLayout(false);
            this->PerformLayout();



        }
#pragma endregion
    private: System::Void label1_Click(System::Object^  sender, System::EventArgs^  e) {
    }
    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {

//      Battle_Card Card1(Int32::Parse(textBox1->Text), Int32::Parse(textBox2->Text), Int32::Parse(textBox3->Text));
//      Battle_Card Card2(Int32::Parse(textBox6->Text), Int32::Parse(textBox5->Text), Int32::Parse(textBox4->Text));

        BattleCard^ Card1 = gcnew BattleCard(Int32::Parse(textBox1->Text), Int32::Parse(textBox2->Text), Int32::Parse(textBox3->Text));
        BattleCard^ Card2 = gcnew BattleCard(Int32::Parse(textBox6->Text), Int32::Parse(textBox5->Text), Int32::Parse(textBox4->Text));

        label8->Text = System::Convert::ToString(Card2->HP);
        if (Card1->HP > 0 && Card2->HP > 0)
        {
            Card2->Defend(Card1->Attack, Card2->Defense);
        }

        label8->Text = System::Convert::ToString(Card2->HP);
        if (Card2->HP > 0 && Card1->HP > 0)
        {
            Card1->Defend(Card2->Attack, Card1->Defense);
        }
        label7->Text = System::Convert::ToString(Card1->HP);
    }
    }; //After MyForm.h is finished, all variables are deleted.
}

如何防止它们被删除?我需要把它们放在MyForm.cpp中吗?

0 个答案:

没有答案