Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open a Form2 with a Button 1

Status
Not open for further replies.

Variably

Programmer
Jan 30, 2008
1
0
0
SE
I have just created a project(Form1).
To this I have "Form1".
I have Added a New Item to this project: "Form2"

So I have 2 Forms !

Now I have created a button on "Form1". With this button I want to open
"Form2".

I have never done this before so I have no idéa where to start.

Code for "Form1"

Code:
#pragma once


namespace Form1 {

	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
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </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::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->SuspendLayout();
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(112, 112);
			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;
			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(292, 266);
			this->Controls->Add(this->button1);
			this->Name = L"Form1";
			this->Text = L"Form1";
			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
			this->ResumeLayout(false);

		}
#pragma endregion
	private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
			 }
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
			 }
	};
}


What will I do with this code now, the only thing I know is that this possible have something to do in order to open "Form2" ?.
This is the only thing I can come up with after all research.
Code:
this->Form2.Show();
 
add
Code:
#include "Form2.h"
before
Code:
namespace Form1 {
Then in the button handler add something like:-
Code:
Form2 myForm;
if(myForm.ShowDialog() == System::Windows::Forms::DialogResult::OK)
{
    //Do stuff
}
 
I would also like to open another form from a button but I do not understand the explanation.

I have used the #include Form2.h at the beginning
of Form1 but I don't understand how to open it.

I get an error when I try
Form2::eek:pen();

Asimov
 
Ok I found out how to do this but now I have another problem.

At the start of form1 after #pragma I put the lines
#include "resource.h"
#include "Addrecord.h"

Then on a button event I put this to call form2
Form1::Hide();
Addrecord^ form = gcnew Addrecord();
form->Show();

This also hides form1 so only one form is showing. Namely Form2.

My problem is how do I get Form1 to show again after I click my Cancel or Apply Buttons in the second form.

I put Form1::Show(); in Form2 but it comes up with an error in compiling. I also tried putting #include "Form1.h" in Form2 but this makes it worse. For some reason Form2 cannot see Form1.

Any ideas?

Asimov
 
you will probably need to do a forward declaration in form2.h to let form2 know about form 1.

something like:

Code:
//form2.h
#include "someInclude.h"


class form1;       //forward declaration



not sure if that will cure everything, but it's worth a shot
 
Hi Sdowd,

I have never used a forward declaration before, and even though I read the page you gave, I am still not sure how it works. I have Form1.h and another form called Addrecord.h.

Do I need to add a.h .b.h to my program or are they just example names?

I am new to c++/cli and would appreciate any help.

Asimov
 
the a.h and the b.h are just examples. Your code would be your includes. Form1.h would include Form2.h(or addrecord.h, if thats the name of your form 2. In your Form2.h, underneath your includes you would just do your forward declaration about form1.

so:

form1.h
Code:
#include "resource.h"
#include "Addrecord.h"

then
form2.h
Code:
#include "someInclude.h"
#include "someInclude2.h"

class Form1;        //forward declaration of form1
                    //now your form2 should know about form1

the forward declarations make it so that you dont have to include form1.h in form2.h because form1.h includes form2.h.
this would create a circle dependency.


 
Hi SDowd,

Thanks for your help so far. I read your last post and thought I understood what to do but the class Form1; causes an error in my code, something to do with gcnew. I will try and explain and will try and paste the relevant code.

In Form1.h I #include Addrecord.h which is my second Form.

In the start of my second form I put the line class Form1;

When I try to compile it comes up with an error. Pasted below.

.\AddressBook.cpp(16) : error C2872: 'Form1' : ambiguous symbol
could be 'd:\my documents\visual studio 2008\projects\addressbook\addressbook\Addrecord.h(12) : Form1'
or 'd:\my documents\visual studio 2008\projects\addressbook\addressbook\Form1.h(27) : AddressBook::Form1'
.\AddressBook.cpp(16) : error C2726: 'gcnew' may only be used to create an object with managed type
.\AddressBook.cpp(16) : error C2512: 'Form1' : no appropriate default constructor available

Look above to my previous post to see how I called the second Form.

Have you any idea why I am getting this error?
Would you mind me rarring up my code and sending it to you?
I'm not sure if that is possible on this forum though.

Thanks in advance

Asimov
 
In the .cpp files you should include both of the headers..
in the example site i posted the .cpps of both class a and class b include each their own headers followed by the the others. for you, your two form class' .cpps will include its own header, followed by the the other classes header.

form1.cpp
Code:
#include "form1.h"
#include "form2.h"

and
form2.cpp
Code:
#include "form2.h"
#include "form1.h"

while still adding the forward declaration in form2's .h file.

also, if that doesnt work, just google c++ and forward declarations. You should be able to find some better examples. i realize the site i posted wasnt very explanatory, it was just a quick search.

 
Hi SDowd,

Since your original email I have been googling "Forward Declarations" since there was no tomorrow. They tend to show an example but don't really explain how it works. I do however understand the concept thanks your your mail. I have followed your instructions and still get errors. I am obviously still missing something. the latest errors are.

.\AddressBook.cpp(19) : error C2872: 'Form1' : ambiguous symbol
could be 'd:\my documents\visual studio 2008\projects\addressbook\addressbook\Addrecord.h(3) : Form1'
or 'd:\my documents\visual studio 2008\projects\addressbook\addressbook\Form1.h(29) : AddressBook::Form1'
.\AddressBook.cpp(19) : error C2726: 'gcnew' may only be used to create an object with managed type
.\AddressBook.cpp(19) : error C2512: 'Form1' : no appropriate default constructor available

Please could you download my start of a program from the link and see where I am going wrong. I have commented out the extra bits of code just to show you my program works to a point before they are added.

Thanks in advance

Asimov
 
 http://www.asimov.dsl.pipex.com/addressbook.rar
Hey, sorry, I was out of town. I will try to take a look at your code today. If anyone else can chime in, it would be great.
 
Hi SDowd,

Thanks for taking the time to look. Luckily VS 2008 is free to download. I looked at the example. Still not quite sure how it works. I understand the concept of a forward declaration but I am not sure where to put the code in my program.

You see if I was writing a dos program I would know exactly where to put code in main without a problem. With Visual c++/cli everything is in cpp files and header files so it complicates things a little. Once I see a few examples I am sure I can pick things up. I usually learn quite quick, but I am getting downhearted by my compiler errors at the moment.

I have been googling like crazy and keep trying different things. Even moving events into the cpp file instead of the header file. This ended in a bad way too however.

Thanks for the help.

Asimov
 
Hi SDowd,

This is strange. Seeing as you got a compile error I redownloaded my code off my website to check it, and it compiled without error. I am puzzled. I didn't get the error C2872 that you got.

There is a link to a screenshot below.

I thought I may have left some dubious code in from testing, but it seems not.

This is all very disheartening LOL. I don't really want to have to go back to C#. Set my heart on learning C++/Cli.

Asimov
 
oh i actually havent had a chance to compile your code.. i was just trying to help you figure out what some of the error messages were. sorry for the confusion. Im going to have to wait til i get home and install vs2008. they wont let me download it at work :)

So if it compiled, are you good to go? things working the way they should be now?
 
Hi SDowd,

Sorry I misunderstood your message. Well the program works until I add the extra lines for the forward declarations. The program only compiles at the moment because they are commented out.

I believe most of the errors come because Form2 cannot see Form1. I think gcnew that I run in Form1 does not like the extra includes I need for the forward decleration.

When you compile the code, I want the program to close Form2 and re open Form1 when the cancel button is clicked.
At the moment the command Form1::Show(); is commented out because it obviously gives me an error.

Ignore my badly designed interface. Gonna update that when the program actually works.

Asimov
 
yeah there seem to be some funky things in the background of gcnew.. i have to be honest i dont use managed types. This is a .net project? last night i made it home and downloaded the express edition of 08, but was getting some weird errors. Plus that generated code that VS gives you, is more than difficult to look to at. I tried making a new project that was two form classes defined in the namespace, each with a single button to do the switch, and it kept griping about some managed types. ill have to look into it.
 
Hi SDowd,

Thanks anyway. It's hard enough learning a new language, but even harder when the documentation is limited. Trouble is whenever I search for C++/cli stuff in google I end up getting C++ stuff and I can't always tell the difference.I wish they used an entirely different name and then it would be easier to search for help.

Which compiler do you use, as I am thinking of changing if I can't get nothing to work?

Asimov
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top