Permalänk
Medlem

Problem med c++

Hej, jag har lite problem med ett textbaserat spel jag håller på med i c++. Min kod stänger ett form och öppnar ett nytt, men när det nya öppnas så kommer det upp tomt. Kod nedanför.

Form1.h:

#pragma once #include "stdafx.h" #include "Form2.h" #include <string> namespace Projekt1 { string name; 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 Form1 /// </summary> public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); // //TODO: Add the constructor code here // } protected: /// <summary> /// Clean up any resources being used. /// </summary> ~Form1() { if (components) { delete components; } } private: System::Windows::Forms::Label^ label1; private: System::Windows::Forms::TextBox^ textBox1; private: System::Windows::Forms::Button^ button1; 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->label1 = (gcnew System::Windows::Forms::Label()); this->textBox1 = (gcnew System::Windows::Forms::TextBox()); this->SuspendLayout(); // // button1 // this->button1->BackColor = System::Drawing::Color::Transparent; this->button1->BackgroundImageLayout = System::Windows::Forms::ImageLayout::None; this->button1->Cursor = System::Windows::Forms::Cursors::Hand; this->button1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(0))); this->button1->Location = System::Drawing::Point(272, 236); this->button1->Name = L"button1"; this->button1->Size = System::Drawing::Size(200, 40); this->button1->TabIndex = 0; this->button1->Text = L"Start game"; this->button1->UseVisualStyleBackColor = false; this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click); // // label1 // this->label1->AutoSize = true; this->label1->BackColor = System::Drawing::Color::Black; this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 24, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(0))); this->label1->ForeColor = System::Drawing::Color::White; this->label1->Location = System::Drawing::Point(249, 106); this->label1->Name = L"label1"; this->label1->Size = System::Drawing::Size(252, 37); this->label1->TabIndex = 1; this->label1->Text = L"Choose a name:"; // // textBox1 // this->textBox1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(0))); this->textBox1->Location = System::Drawing::Point(256, 173); this->textBox1->Name = L"textBox1"; this->textBox1->Size = System::Drawing::Size(232, 26); this->textBox1->TabIndex = 2; this->textBox1->TextAlign = System::Windows::Forms::HorizontalAlignment::Center; // // Form1 // this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->BackColor = System::Drawing::Color::Black; this->ClientSize = System::Drawing::Size(784, 461); this->Controls->Add(this->textBox1); this->Controls->Add(this->label1); this->Controls->Add(this->button1); 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) { name = textBox1->Text; this->Hide(); Form2 ^ F2 = gcnew Form2(); F2->ShowDialog(); } }; }

Form2.h:

#pragma once #include "stdafx.h" //#include "Form3.h" #include <string> namespace Projekt1 { 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 Form2 /// </summary> public ref class Form2 : public System::Windows::Forms::Form { public: Form2(void) { InitializeComponent(); // //TODO: Add the constructor code here // } protected: /// <summary> /// Clean up any resources being used. /// </summary> ~Form2() { if (components) { delete components; } } private: System::Windows::Forms::Button^ button1; private: System::Windows::Forms::Button^ button2; private: System::Windows::Forms::Button^ button3; private: System::Windows::Forms::Label^ label1; 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->button2 = (gcnew System::Windows::Forms::Button()); this->button3 = (gcnew System::Windows::Forms::Button()); this->label1 = (gcnew System::Windows::Forms::Label()); this->SuspendLayout(); // // button1 // this->button1->Location = System::Drawing::Point(68, 345); this->button1->Name = L"button1"; this->button1->Size = System::Drawing::Size(200, 30); this->button1->TabIndex = 1; this->button1->Text = L"Exempeltext"; this->button1->UseVisualStyleBackColor = true; this->button1->Click += gcnew System::EventHandler(this, &Form2::button1_Click); // // button2 // this->button2->Location = System::Drawing::Point(293, 345); this->button2->Name = L"button2"; this->button2->Size = System::Drawing::Size(200, 30); this->button2->TabIndex = 2; this->button2->Text = L"Exempeltext"; this->button2->UseVisualStyleBackColor = true; this->button2->Click += gcnew System::EventHandler(this, &Form2::button2_Click); // // button3 // this->button3->Location = System::Drawing::Point(519, 345); this->button3->Name = L"button3"; this->button3->Size = System::Drawing::Size(200, 30); this->button3->TabIndex = 3; this->button3->Text = L"Exempeltext"; this->button3->UseVisualStyleBackColor = true; // // label1 // this->label1->AutoSize = true; this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(0))); this->label1->ForeColor = System::Drawing::Color::White; this->label1->Location = System::Drawing::Point(18, 40); this->label1->MaximumSize = System::Drawing::Size(776, 1000); this->label1->Name = L"label1"; this->label1->Size = System::Drawing::Size(81, 16); this->label1->TabIndex = 4; this->label1->Text = L"Exempeltext"; this->label1->TextAlign = System::Drawing::ContentAlignment::TopCenter; // // Form2 // this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->BackColor = System::Drawing::Color::Black; this->ClientSize = System::Drawing::Size(784, 461); this->Controls->Add(this->label1); this->Controls->Add(this->button3); this->Controls->Add(this->button2); this->Controls->Add(this->button1); this->Name = L"Form2"; this->Text = L"Form2"; this->ResumeLayout(false); this->PerformLayout(); } #pragma endregion private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) { /*this->Hide(); Form3 ^ F3 = gcnew Form3(); F3->ShowDialog();*/ } }; }

Tacksam för hjälp.

Permalänk
Hedersmedlem
Skrivet av valnoten:

Hej, jag har lite problem med ett textbaserat spel jag håller på med i c++. Min kod stänger ett form och öppnar ett nytt, men när det nya öppnas så kommer det upp tomt.

Tomt? Utan label1 och knapparna? Det fungerar hos mig...

Permalänk
Medlem

Hej jag lite problem med att lösa ett uppgift så kan ni hjälp med några förslag hur man skulle kunna göra ett program som skriver ut mönster t.ex kryss och de ska stå över varandra och med samma mellanrum! Några förslag på detta...