I’m trying to make it so that when I click a button on my Visual C++ project, it opens another form, kinda acting like a java JOptionPane.showInputDialog, but making it look like the way I want. I’m trying to open it with Form21^form2=gcnew Form21(); form2->ShowDialog();
but all it says is
1>c:\users\steve\documents\visual studio 2010\projects\lesson 2\lesson 2\Form1.h(253): error C2065: 'Form21' : undeclared identifier
1>c:\users\steve\documents\visual studio 2010\projects\lesson 2\lesson 2\Form1.h(253): error C2065: 'form2' : undeclared identifier
1>c:\users\steve\documents\visual studio 2010\projects\lesson 2\lesson 2\Form1.h(253): error C2061: syntax error : identifier 'Form21'
1>c:\users\steve\documents\visual studio 2010\projects\lesson 2\lesson 2\Form1.h(254): error C2065: 'form2' : undeclared identifier
1>c:\users\steve\documents\visual studio 2010\projects\lesson 2\lesson 2\Form1.h(254): error C2227: left of '->ShowDialog' must point to class/struct/union/generic type
1> type is ''unknown-type''
Form 2 should be all fine, but here is the code for it anyways
#pragma once
namespace Lesson2 {
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::Label^ label1;
private: System::Windows::Forms::TextBox^ textBox1;
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::TextBox^ textBox2;
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->label1 = (gcnew System::Windows::Forms::Label());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->label2 = (gcnew System::Windows::Forms::Label());
this->textBox2 = (gcnew System::Windows::Forms::TextBox());
this->SuspendLayout();
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(12, 9);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(61, 13);
this->label1->TabIndex = 0;
this->label1->Text = L"Username :";
this->label1->Click += gcnew System::EventHandler(this, &Form2::label1_Click);
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(81, 8);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(108, 20);
this->textBox1->TabIndex = 1;
this->textBox1->TextChanged += gcnew System::EventHandler(this, &Form2::textBox1_TextChanged);
//
// label2
//
this->label2->AutoSize = true;
this->label2->Location = System::Drawing::Point(12, 54);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(59, 13);
this->label2->TabIndex = 2;
this->label2->Text = L"Password: ";
//
// textBox2
//
this->textBox2->Location = System::Drawing::Point(81, 47);
this->textBox2->Name = L"textBox2";
this->textBox2->Size = System::Drawing::Size(108, 20);
this->textBox2->TabIndex = 3;
//
// Form2
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(211, 88);
this->Controls->Add(this->textBox2);
this->Controls->Add(this->label2);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->label1);
this->DoubleBuffered = true;
this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedSingle;
this->MaximizeBox = false;
this->MinimizeBox = false;
this->Name = L"Form2";
this->Text = L"Create an account";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void label1_Click(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void textBox1_TextChanged(System::Object^ sender, System::EventArgs^ e) {
}
};
}
I know its says its lesson two, but that’s just because I was teaching myself some stuff. I pick up this kinda stuff real easy. Except when this happens. Anyone see whats going wrong?
Form21 is most likely declared in another file so the compiler can’t find the Form21 type.
At the top of Form1.h, add
#include "Form21.h"(or the name of the header file containing Form21’s class declaration if it’s different).I would also recommend you read about header and cpp files (you seem to have code in header files), forward declarations and some other basic stuff about c++/cli.
EDIT: After you posted your
Form2code, the error is that the lineForm21^form2=gcnew Form21();should beForm2^form2=gcnew Form2();. You declaredForm2, notForm21.Good luck!