×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Open a Form2 with a Button

Open a Form2 with a Button

Open a Form2 with a Button

(OP)
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();

RE: Open a Form2 with a Button

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
}

RE: Open a Form2 with a Button

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::open();

Asimov

RE: Open a Form2 with a Button

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

RE: Open a Form2 with a Button

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

http://www.adp-gmbh.ch/cpp/forward_decl.html


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

RE: Open a Form2 with a Button

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

RE: Open a Form2 with a Button

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.  


RE: Open a Form2 with a Button

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

RE: Open a Form2 with a Button

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.

 

RE: Open a Form2 with a Button

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

RE: Open a Form2 with a Button

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.  

RE: Open a Form2 with a Button

Im not a  fan of wikipedia, but this explains it a little better:

http://en.wikipedia.org/wiki/Circular_dependency

i dont have VS 2008 so i couldnt load your project.  Ill play around with it hopefully later on tonight.  let me know if that website provides any more clarity for you.

RE: Open a Form2 with a Button

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

RE: Open a Form2 with a Button

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

RE: Open a Form2 with a Button

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?

RE: Open a Form2 with a Button

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

RE: Open a Form2 with a Button

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.

RE: Open a Form2 with a Button

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

RE: Open a Form2 with a Button

I used to use VC7(2003).  Just switched to 2005.  Ive used VC6 also. I like VC7.  2005 is decent, it's pretty much the same.  I haven't been working with it any longer then a couple of months though.   

RE: Open a Form2 with a Button

I'll take a look when I get back to me VS2008 box.
O.B
 

RE: Open a Form2 with a Button

Hi Folks,

Great post nice to see you guys work out a solution.

I have started down the same path of calling form2 from form1 (VS 2005 C++) My question is, is this best practice for calling forms in VS C++? newbie to VS 2005 C++. coming from VB form2.show

TheBoy2b

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close