Windows窗体应用程序按钮执行工作

时间:2016-05-08 17:08:23

标签: c++ windows winforms

我已经制作了一个新的Windows窗体应用程序,我尝试制作一个按钮来将两个数字相乘,但是按钮会显示工作...这是  MyForm.cpp

#include "MyForm.h"

using namespace System;
using namespace System::Windows::Forms;
[STAThread]
void Main(array<String^>^ args)
{
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);

Project5::MyForm form;
Application::Run(%form);
}

MyForm.h

#pragma once

 namespace Project5 {

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::Button^  button1;
private: System::Windows::Forms::TextBox^  textBox1;
private: System::Windows::Forms::TextBox^  textBox2;
private: System::Windows::Forms::TextBox^  textBox3;
protected:

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->button1 = (gcnew System::Windows::Forms::Button());
        this->textBox1 = (gcnew System::Windows::Forms::TextBox());
        this->textBox2 = (gcnew System::Windows::Forms::TextBox());
        this->textBox3 = (gcnew System::Windows::Forms::TextBox());
        this->SuspendLayout();
        // 
        // button1
        // 
        this->button1->Location = System::Drawing::Point(148, 169);
        this->button1->Name = L"button1";
        this->button1->Size = System::Drawing::Size(75, 23);
        this->button1->TabIndex = 0;
        this->button1->Text = L"button1";
        this->button1->UseVisualStyleBackColor = true;
        // 
        // textBox1
        // 
        this->textBox1->Location = System::Drawing::Point(35, 27);
        this->textBox1->Name = L"textBox1";
        this->textBox1->Size = System::Drawing::Size(100, 22);
        this->textBox1->TabIndex = 1;
        this->textBox1->Text = L"1";
        // 
        // textBox2
        // 
        this->textBox2->Location = System::Drawing::Point(35, 65);
        this->textBox2->Name = L"textBox2";
        this->textBox2->Size = System::Drawing::Size(100, 22);
        this->textBox2->TabIndex = 2;
        this->textBox2->Text = L"2";
        // 
        // textBox3
        // 
        this->textBox3->Location = System::Drawing::Point(35, 109);
        this->textBox3->Name = L"textBox3";
        this->textBox3->Size = System::Drawing::Size(100, 22);
        this->textBox3->TabIndex = 3;
        // 
        // MyForm
        // 
        this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
        this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
        this->ClientSize = System::Drawing::Size(282, 253);
        this->Controls->Add(this->textBox3);
        this->Controls->Add(this->textBox2);
        this->Controls->Add(this->textBox1);
        this->Controls->Add(this->button1);
        this->Name = L"MyForm";
        this->Text = L"MyForm";
        this->ResumeLayout(false);
        this->PerformLayout();

    }
 #pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
    int a, b, c;
    a = System::Convert::ToInt16(textBox1->Text);
    b = System::Convert::ToInt16(textBox2->Text);
    c = b + a;
    textBox3->Text = System::Convert::ToString(c);
    }
};

}

应用程序启动但是当我按下按钮时没有发生任何事情 有什么建议吗?

1 个答案:

答案 0 :(得分:2)

你的按钮设计师代码没有这一行:

this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);

如果您双击视觉设计器中的按钮,它应该为您添加此项。如果它没有添加此行,则应正确连接按钮处理程序。