0
我正在使用Visual Studio 2010,我想在C++中的兩種窗體之間切換。我一直在做一些谷歌搜索,我不能找到任何解決方案,而不是所有關於C#和Visual Basic。在窗體之間切換vC++ 2010
我已經越來越到目前爲止,這個錯誤:
錯誤12錯誤C2227:左 ' - > ShowDialog的' 必須指向類/結構/聯合/通用型 錯誤4錯誤C2065:窗體2 ':未聲明的標識符
錯誤11錯誤C2065:F1':未聲明的標識符
錯誤5錯誤C2061:語法錯誤:標識符 '窗體2'
錯誤10錯誤C2061:語法錯誤:識別符 'Form1中'
我把我的代碼的唯一事情是
表1:
#include "Form2.h"
和按鈕事件的
Form2^form2 = gcnew Form2();
form2->ShowDialog();
this->Hide();
,並在表2
#include "Form1.h"
和按鈕事件
Form1^f1 = gcnew Form1();
this->Hide();
f1->ShowDialog();
這是否正確?這是我的第一篇文章曾經這樣告訴我需要在增加 幫助化妝還有什麼更清晰 編輯: 窗體2
#pragma once
#include "Form1.h"
namespace MBED4 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::IO::Ports; // *** added to allow listing of COM ports
using namespace System::Threading;
/// <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;
protected:
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::Button^ button3;
private: System::Windows::Forms::Button^ button4;
private: System::Windows::Forms::Button^ button5;
private: System::Windows::Forms::Button^ button6;
private: System::Windows::Forms::Button^ button7;
private: System::Windows::Forms::MenuStrip^ menuStrip1;
private: System::Windows::Forms::ToolStripMenuItem^ fileToolStripMenuItem;
private: System::Windows::Forms::ToolStripMenuItem^ exitToolStripMenuItem;
private: System::IO::Ports::SerialPort^ serialPort1;
private: System::ComponentModel::IContainer^ components;
private:
/// <summary>
/// Required designer variable.
/// </summary>
#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->components = (gcnew System::ComponentModel::Container());
this->button1 = (gcnew System::Windows::Forms::Button());
this->button2 = (gcnew System::Windows::Forms::Button());
this->button3 = (gcnew System::Windows::Forms::Button());
this->button4 = (gcnew System::Windows::Forms::Button());
this->button5 = (gcnew System::Windows::Forms::Button());
this->button6 = (gcnew System::Windows::Forms::Button());
this->button7 = (gcnew System::Windows::Forms::Button());
this->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip());
this->fileToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
this->exitToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
this->serialPort1 = (gcnew System::IO::Ports::SerialPort(this->components));
this->menuStrip1->SuspendLayout();
this->SuspendLayout();
//
// button1
//
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(29, 41);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(80, 30);
this->button1->TabIndex = 0;
this->button1->Text = L"Servo1";
this->button1->UseVisualStyleBackColor = true;
//
// button2
//
this->button2->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->button2->Location = System::Drawing::Point(29, 97);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(80, 30);
this->button2->TabIndex = 1;
this->button2->Text = L"Servo2";
this->button2->UseVisualStyleBackColor = true;
//
// button3
//
this->button3->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->button3->Location = System::Drawing::Point(29, 152);
this->button3->Name = L"button3";
this->button3->Size = System::Drawing::Size(80, 30);
this->button3->TabIndex = 2;
this->button3->Text = L"Servo3";
this->button3->UseVisualStyleBackColor = true;
//
// button4
//
this->button4->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->button4->Location = System::Drawing::Point(29, 203);
this->button4->Name = L"button4";
this->button4->Size = System::Drawing::Size(80, 30);
this->button4->TabIndex = 3;
this->button4->Text = L"Servo4";
this->button4->UseVisualStyleBackColor = true;
//
// button5
//
this->button5->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 14, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->button5->Location = System::Drawing::Point(29, 273);
this->button5->Name = L"button5";
this->button5->Size = System::Drawing::Size(113, 40);
this->button5->TabIndex = 4;
this->button5->Text = L"User Mode";
this->button5->UseVisualStyleBackColor = true;
this->button5->Click += gcnew System::EventHandler(this, &Form2::button5_Click);
//
// button6
//
this->button6->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->button6->Location = System::Drawing::Point(157, 41);
this->button6->Name = L"button6";
this->button6->Size = System::Drawing::Size(80, 30);
this->button6->TabIndex = 5;
this->button6->Text = L"LED1";
this->button6->UseVisualStyleBackColor = true;
//
// button7
//
this->button7->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->button7->Location = System::Drawing::Point(157, 97);
this->button7->Name = L"button7";
this->button7->Size = System::Drawing::Size(80, 30);
this->button7->TabIndex = 6;
this->button7->Text = L"LED2";
this->button7->UseVisualStyleBackColor = true;
//
// menuStrip1
//
this->menuStrip1->Items->AddRange(gcnew cli::array<System::Windows::Forms::ToolStripItem^>(1) {this->fileToolStripMenuItem});
this->menuStrip1->Location = System::Drawing::Point(0, 0);
this->menuStrip1->Name = L"menuStrip1";
this->menuStrip1->Size = System::Drawing::Size(570, 24);
this->menuStrip1->TabIndex = 7;
this->menuStrip1->Text = L"menuStrip1";
//
// fileToolStripMenuItem
//
this->fileToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array<System::Windows::Forms::ToolStripItem^>(1) {this->exitToolStripMenuItem});
this->fileToolStripMenuItem->Name = L"fileToolStripMenuItem";
this->fileToolStripMenuItem->Size = System::Drawing::Size(37, 20);
this->fileToolStripMenuItem->Text = L"File";
//
// exitToolStripMenuItem
//
this->exitToolStripMenuItem->Name = L"exitToolStripMenuItem";
this->exitToolStripMenuItem->Size = System::Drawing::Size(152, 22);
this->exitToolStripMenuItem->Text = L"Exit";
this->exitToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form2::exitToolStripMenuItem_Click);
//
// Form2
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(570, 344);
this->Controls->Add(this->button7);
this->Controls->Add(this->button6);
this->Controls->Add(this->button5);
this->Controls->Add(this->button4);
this->Controls->Add(this->button3);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Controls->Add(this->menuStrip1);
this->MainMenuStrip = this->menuStrip1;
this->Name = L"Form2";
this->Text = L"Form2";
this->Load += gcnew System::EventHandler(this, &Form2::Form2_Load);
this->menuStrip1->ResumeLayout(false);
this->menuStrip1->PerformLayout();
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void Form2_Load(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void exitToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
serialPort1->Close();
Application::Exit();
}
private: System::Void button5_Click(System::Object^ sender, System::EventArgs^ e) {
Form1^f1 = gcnew Form1();
this->Hide();
f1->ShowDialog();
}
};
}
請出示您的Form2.h(至少相關部分)。 Form2.h是否包含警衛?如果是這樣,他們是唯一的嗎? – 2014-11-03 16:55:33
查看編輯我上面張貼的 – zahdeh 2014-11-03 17:14:15
首先,這不是C++,而是C++/CLI。差異很大,標籤應該改變。其次,爲什麼你使用C++/CLI用於帶有GUI的新項目? C++/CLI的目的是包裝一些舊的C++庫,以便它們可以在新的C#項目中使用。如果你是編程新手,那麼C#優於C++/CLI。 – Dialecticus 2014-11-03 17:20:22