Click to See Complete Forum and Search --> : Help with windows froms and managed c++


deathscythe
January 13th, 2007, 07:45 AM
Hi,

I have created a managed C++ application in VS2003, and have created saveral froms, assume I have created 5 forms all named form1 to form5, what I want to do is when form1 is loaded as the application starts, the other 4 forms should be loaded into memeory as well but remain hidden. The user would then should be able to click the next button on form1, where it will be hidden and form2 shown. However I am having difficulty in calling the form3 object which was created in form1 from form2, when i try to include the header file for form3 into form2 i get compilation errors.

Is there any solution to this? and is this the best method for achieving what I want to achieve?

Usually in Java I would really create many form but just one master form and have panels for the other screens which I would load when the next button has been clicked, I couldnt find a way to use VS2003 GUI builder and achieve this, if it is possible any hints on this would be appreciated as well.

To those who will say use Java, or use VB.Net or C# the project I am working on will also require access to some code that has been written in C++ and needs to be unmanaged. Hence the choice of managed C++.

If moe details are required I would supply them so just ask.

Thank you.

Zaccheus
January 13th, 2007, 11:45 AM
when i try to include the header file for form3 into form2 i get compilation errors.
You need to use forward declarations.

Let's say you had two simple classes A & B:



//--------------------------------
// File: A.h

// Forward declare B:

class __gc B;

// Declare A

class __gc A
{
public:
void f(B* b); // Using B's forward declaration.
};

//--------------------------------
// File: A.cpp

#include "A.h"
#include "B.h"

void A::f(B* b)
{
// do something with 'b' using the full declaration from "B.h".
}

//--------------------------------
// File: B.h

// Forward declare A:

class __gc A;

// Declare B

class __gc B
{
public:
void g(A* a); // Using A's forward declaration.
};

//--------------------------------
// File: b.cpp

#include "B.h"
#include "A.h"

void B::g(A* a)
{
// do something with 'a' using the full declaration from "A.h".
}

deathscythe
January 15th, 2007, 10:56 AM
Hi,

I understand the concept, but when I try to include the forward declaration I get weird error messages.

As the forms I have been making are made with VS2003 GUI builder, and it has declared everything in the header file, and I would like each of the forms to know about the others.

At the moment the form classes are wrapped in a namespace, and the name space is the same in each of the header file.

Any help would be much appreciated.


Thanks

Zaccheus
January 15th, 2007, 02:06 PM
For this to work, you need to separate the declarations from the implementations like I showed in my post (otherwise the forward declare vs #include does not work).

Zaccheus
January 15th, 2007, 02:10 PM
To those who will say use Java, or use VB.Net or C# the project I am working on will also require access to some code that has been written in C++ and needs to be unmanaged. Hence the choice of managed C++.
For what it's worth, I think that is a bad reason to use managed C++.

If you have some existing C++ code then you can wrap it up in a native C++ DLL which exposes standard functions (like the windows DLLs do) and call that via p/invoke.

Not that this should stop you from writing pure .net code in managed C++.
:)

deathscythe
January 16th, 2007, 07:13 AM
Hi,

At the moment I'm facing problems seperating the delcarations and the body of the code.

The main problem is with the way the VS2003 windows form needs the InitializeComponent method in the header, and if I move it to the cpp file, I am unable to edit the from using the graphical method, which is the biggest pain, as I need to retain the graphical method fo editing the form, as other people in the future will need to use this way of making cosmetic changes to the application, but they necessarily won't be competent programmers.

If there is another way of making what I wanna do work that would be great, even if it is a hack.

Thanks

Zaccheus
January 16th, 2007, 07:43 AM
If you move the region markers as well, it should still work with the editor.

deathscythe
January 16th, 2007, 09:34 AM
Hi,

Thanks you help has been great. Just got one little question, how would I load all the forms into memeory at once, so as to make transitions between them smooth?

As I have a windows media player control on one of my forms, and moving away froma nd moving back to, I can notice it takes a little while before the form video plays back, and as a result the buttons on the forms arent drawn straight away.

Thanks once again.

Zaccheus
January 19th, 2007, 06:21 PM
Setting the Visible property is faster than creating/deleting the Form.