【问题标题】:Windows Form Application Button doesent workWindows 窗体应用程序按钮不起作用
【发布时间】:2016-05-08 17:08:23
【问题描述】:

我制作了一个新的 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);
    }
};

}

应用程序启动,但当我按下按钮时没有任何反应 有什么建议吗?

【问题讨论】:

  • 能把设计师的源代码发一下吗?
  • 除了您的代码将数字相加而不是相乘这一事实之外,您是否尝试过调试以确保 textBox1textBox2 中的文本正确地转换为整数。 (我也转换为 Int32 而不是 Int16。
  • 按钮不响应任何命令..

标签: c++ windows winforms


【解决方案1】:

您的按钮设计器代码没有这一行:

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

如果您双击可视化设计器中的按钮,它应该会为您添加此按钮。如果它不只是添加这一行并且按钮处理程序应该正确连接。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-02
    • 2021-05-18
    • 1970-01-01
    相关资源
    最近更新 更多