2016-05-08 9 views
-2

ich eine neue Windows Forms-Anwendung gemacht habe, und ich versuchte, einen Knopf zu machen zwei Zahlen zu multiplizieren, aber die Schaltfläche doesent Arbeit ... Das ist MyForm.cppWindows Form Anwendungstaste doesent Arbeit

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

}

die Anwendung starten, aber wenn ich die Taste nichts drücken passiert Jeder Vorschlag?

+0

Können Sie bitte den Quellcode des Designers posten? – Michael

+0

Abgesehen von der Tatsache, dass Ihr Code die Zahlen addiert und nicht multipliziert, haben Sie versucht, das Debugging zu überprüfen, um sicherzustellen, dass der Text in 'textBox1' und' textBox2' korrekt in Ganzzahlen konvertiert wird. (Ich konvertiere auch zu Int32 und nicht zu Int16. – ChrisF

+0

Die Taste Doesent reagiert auf jeden Befehl .. –

Antwort

2

Ihre Schaltfläche Designer-Code nicht über diese Linie haben:

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

Wenn Sie einen Doppelklick auf die Schaltfläche in der visuellen Designer sollte dies für Sie hinzufügen. Wenn es nicht nur diese Zeile hinzugefügt und der Button-Handler sollte korrekt angeschlossen sein.

+0

Vielen Dank! –