2 Zahlen addieren..
-
hallo leute, ich habe letztens mit visual c++ angefangen, und irgendwie find ich das ganze etwas seltsam, weil ich bei der grafischen programmierung irgendwie anders denken muss..
Ich versuche ein ganz einfaches Programm zu schreiben, das 2 Zahlen addiert, und in Textbox3 ausgibt.
hier erstmal der code
#pragma once namespace test { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); // //TODO: Konstruktorcode hier hinzufügen. // } protected: ~Form1() { 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; private: System::Windows::Forms::Label^ label1; protected: private: System::ComponentModel::Container ^components; #pragma region Windows Form Designer generated code 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->label1 = (gcnew System::Windows::Forms::Label()); this->SuspendLayout(); // // button1 // this->button1->Location = System::Drawing::Point(124, 116); this->button1->Margin = System::Windows::Forms::Padding(2, 3, 2, 3); this->button1->Name = L"button1"; this->button1->Size = System::Drawing::Size(90, 31); this->button1->TabIndex = 0; this->button1->Text = L"Berechnen"; this->button1->UseVisualStyleBackColor = true; this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click); // // textBox1 // this->textBox1->Location = System::Drawing::Point(207, 62); this->textBox1->Name = L"textBox1"; this->textBox1->Size = System::Drawing::Size(100, 20); this->textBox1->TabIndex = 1; // // textBox2 // this->textBox2->Location = System::Drawing::Point(33, 62); this->textBox2->Name = L"textBox2"; this->textBox2->Size = System::Drawing::Size(100, 20); this->textBox2->TabIndex = 2; // // textBox3 // this->textBox3->Location = System::Drawing::Point(33, 185); this->textBox3->Name = L"textBox3"; this->textBox3->Size = System::Drawing::Size(274, 20); this->textBox3->TabIndex = 3; // // label1 // this->label1->AutoSize = true; this->label1->Location = System::Drawing::Point(164, 65); this->label1->Name = L"label1"; this->label1->Size = System::Drawing::Size(13, 13); this->label1->TabIndex = 4; this->label1->Text = L"+"; this->label1->Click += gcnew System::EventHandler(this, &Form1::label1_Click); // // Form1 // this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(328, 267); this->Controls->Add(this->label1); this->Controls->Add(this->textBox3); this->Controls->Add(this->textBox2); this->Controls->Add(this->textBox1); this->Controls->Add(this->button1); this->Margin = System::Windows::Forms::Padding(2, 3, 2, 3); this->Name = L"Form1"; this->Text = L"Form1"; this->ResumeLayout(false); this->PerformLayout(); } #pragma endregion private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { } private: System::Void label1_Click(System::Object^ sender, System::EventArgs^ e) { } }; } :p
Ich habe versucht die zahlen in button1 zu initialsieren,
also int zahl1, int zahl2 und auf das entsprechende textbox verwiesen.int zahl1 = textBox1 zb
dann halt addiert und in "erg" gespeichert..
Wie kann ich das so machen, dass textBox3 das Ergebnis "erg" ausgibt.?=Vielen Dank für eure Antworten..
mfg
-
Dieser Thread wurde von Moderator/in estartu aus dem Forum MFC (Visual C++) in das Forum C++/CLI mit .NET verschoben.
Im Zweifelsfall bitte auch folgende Hinweise beachten:
C/C++ Forum :: FAQ - Sonstiges :: Wohin mit meiner Frage?Dieses Posting wurde automatisch erzeugt.
-
So?
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { int _zahl1, _zahl2, _ergebnis; _zahl1 = 12; _zahl2 = 25; _ergebnis = _zahl1 + _zahl2; label1->Text = _ergebnis.ToString(); //So geht es auch label1->Text = Convert::ToString(_zahl1 + _zahl2); }
-
ok, danke für deine hilfe, der code klingt logisch..hab jetzt bissn damit rumgespielt.. zb das ergebnis in textbox3 angezeigt..
allerdings möchte ich ja, die werte von textbox1 und 2 abgreifen und die dann bei button1_clicked addieren und in textbox3 anzeigen lassen..
nächste frage also: wie frage ich werte im textbox ab und übergebe sie anschließen button1_clicked.?
mfg
-
Auch nix dickes...
int _zahl1, _zahl2; //String in int konvertieren //Da zwei Zahlen die als String übergeben werden, würden das Ergbnis liefern... //10 + 12 = 1012 und nicht 22, da die beiden Strings nur aneinander gehängt werden _zahl1 = Convert::ToInt32(textBox1->Text); _zahl2 = Convert::ToInt32(textBox2->Text); //Das Ergebnis in einen Sting konvertieren, da die Textbox nur einen String verarbeiten kann. textBox3->Text = Convert::ToString(_zahl1 + _zahl2);
Lade Dir mal das OpenBook down hier...
Ist zwar C#, aber die Grundbegriffe sind die gleichen.Wichtig...
http://www.galileocomputing.de/openbook/visual_csharp/
Gruß, Doug_HH