如何在类中声明按钮事件?

时间:2016-05-04 12:28:01

标签: events header c++-cli declare

我是新来的,希望你能帮助我...... 我的窗口布局有一个标题“Messagebox.h”(我正在使用visual studio 2013),我想在类“Class.cpp”而不是标题中声明click事件。

这是我的Messagebox.h

#pragma once

class Banana;
class Apple;

namespace Fruitsalat {

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>
/// Zusammenfassung für Messagebox
/// </summary>
public ref class Messagebox : public System::Windows::Forms::Form
{
public:
    Messagebox(void)
    {
        InitializeComponent();
        //
        //TODO: Konstruktorcode hier hinzufügen.
        //
    }

protected:
    /// <summary>
    /// Verwendete Ressourcen bereinigen.
    /// </summary>
    ~Messagebox()
    {
        if (components)
        {
            delete components;
        }
    }
private: System::Windows::Forms::Button^  button1;
private: System::Windows::Forms::Button^  button2;
private: System::Windows::Forms::TextBox^  textBox1;
protected:

private:
    /// <summary>
    /// Erforderliche Designervariable.
    /// </summary>
    System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
    /// <summary>
    /// Erforderliche Methode für die Designerunterstützung.
    /// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
    /// </summary>
    void InitializeComponent(void)
    {
        this->button1 = (gcnew System::Windows::Forms::Button());
        this->button2 = (gcnew System::Windows::Forms::Button());
        this->textBox1 = (gcnew System::Windows::Forms::TextBox());
        this->SuspendLayout();
        // 
        // button1
        // 
        this->button1->Location = System::Drawing::Point(197, 226);
        this->button1->Name = L"button1";
        this->button1->Size = System::Drawing::Size(75, 23);
        this->button1->TabIndex = 0;
        this->button1->Text = L"quit";
        this->button1->UseVisualStyleBackColor = true;
        this->button1->Click += gcnew System::EventHandler(this, &Messagebox::button1_Click);
        // 
        // button2
        // 
        this->button2->Location = System::Drawing::Point(102, 226);
        this->button2->Name = L"button2";
        this->button2->Size = System::Drawing::Size(75, 23);
        this->button2->TabIndex = 1;
        this->button2->Text = L"send";
        this->button2->UseVisualStyleBackColor = true;
        this->button2->Click += gcnew System::EventHandler(this, &Messagebox::button2_Click);
        // 
        // textBox1
        // 
        this->textBox1->Location = System::Drawing::Point(12, 12);
        this->textBox1->Multiline = true;
        this->textBox1->ReadOnly = true;
        this->textBox1->Name = L"textBox1";
        this->textBox1->BackColor = Color::White;
        this->textBox1->Size = System::Drawing::Size(260, 195);
        this->textBox1->TabIndex = 2;
        // 
        // Messagebox
        // 
        this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
        this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
        this->ClientSize = System::Drawing::Size(284, 261);
        this->Controls->Add(this->textBox1);
        this->Controls->Add(this->button2);
        this->Controls->Add(this->button1);
        this->Name = L"Messagebox";
        this->Text = L"Messagebox";
        this->ResumeLayout(false);
        this->PerformLayout();

    }
#pragma endregion

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
    this->Close();
}
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e){
    //declare this in Apple.cpp or Banana.cpp
}
};
}

我可以这样做吗?

1 个答案:

答案 0 :(得分:0)

看起来您正在询问如何在头文件和源文件中键入相应的行,以便您可以将方法的实现移出标题并进入源文件。这是对的吗?

事件处理程序方法没有天生的特殊之处。它只是您类上的常规方法,因此您可以以相同的方式实现它:在.h文件中声明方法,在.cpp文件中实现该方法。

MyClass.h

public ref class MyClass : public SomeGUIClass
{
private:
    void button2_Click(Object^ sender, EventArgs^ e);
};

MyClass.cpp

void MyClass::button2_Click(Object^ sender, EventArgs^ e)
{
    this->textBox->Text = "Woo!";
}