Click here to Skip to main content
15,890,336 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
pls help....search through internet but no example on switch between more than 2 forms....

i had 3 form ,form a,b,c;
i want to make it like this,form a can switch to form b and c,
form b can switch to form a and c,form c can switch to form a and b.

as form a is my main form..i had no problem to switch from other form to form a...(i use this->hide() to show form a);
but when i in form c,i want to show form b,how?if i include form b.h then i
had problem when i wan to switch to form c from form b..i cant include form c.h in my form b ....

any method to make all my form can freely switch between?
Posted

As promised a simple example. Remember that it is just a functional example and that the code isn't all how it should be. Well, that said, here is the class used to toggle the forms.

C++
ref class ToggleForm
{
public:
    ToggleForm(void)
    {
        current = 0;
        //formlist = gcnew ArrayList()
    };
    void addForm(Form^ form)
    {
        formlist.Add(form);
    };
    void next(void)
    {
        moveToForm(1);
    };
    void previous(void)
    {
        moveToForm(-1);
    };
private:
    int current;
    ArrayList formlist;

    void moveToForm(int movement)
    {
        Form^ currentForm = (Form^)formlist[current];
        currentForm->Visible = false;
        current = (formlist.Count + current + movement) % (formlist.Count);
        Form^ nextForm = (Form^)formlist[current];
        nextForm->Visible = true;
    };

};


In the load of the first form I create the other forms, add them to the ToggleForm class and give each a reference of toggleForm. I simply used the so called OtherForm twice but of course it can be any other form.

C++
toggleForm = gcnew ToggleForm();
toggleForm->addForm(this);
OtherForm^ otherFormB = gcnew OtherForm;
otherFormB->Text = "B";
OtherForm^ otherFormC = gcnew OtherForm;
otherFormC->Text = "C";
toggleForm->addForm(otherFormB);
toggleForm->addForm(otherFormC);
otherFormB->toggleForm = toggleForm;
otherFormC->toggleForm = toggleForm;


The buttons on the form simply call the previous and next functions of the ToggleForm object and this handles the hiding and showing of the forms.

You can also download this simple example project at:
http://orion.woelmuis.nl/FormToggleExampleProject.zip[^]

Well, this probably will give you enough idea about how to implement such a mechanism.

Good luck!
 
Share this answer
 
Comments
tuolesi 5-Aug-10 9:18am    
wao...thx for ur great help..
You could simply create a new class that is the placeholder for the generic type Form. You can then simply fill that with the three forms and pass it to all the three forms. That newly created class can the just simply be crafted in a way that the forms in the on show simply call some class function that hides all forms except the calling form that's is provided as a parameter. You would get very small code and efficient. You could also simply add next/previous functionality because instead of a, b, c you can use 0, 1, 2 that are simply array indexes to use.

Good luck!
 
Share this answer
 
Comments
tuolesi 2-Aug-10 9:19am    
erm......i cant get it....
so i have to create a new header file which create a class to have function to call the form?
but i have to include form a,b,c .h again in my newly create header file???

anyone can show me the coding ??i m totally lost....
thx...
E.F. Nijboer 2-Aug-10 12:27pm    
You create a class with a simple Vector and put the three forms in there. Because you then are using the base class instead of the specific implementation of A B and C you are allowed to use them in the A B and C forms without circular referencing.
tuolesi 3-Aug-10 6:55am    
erm i dont understand what is" put the three forms in there "??
what i need to do inside the headerfile?
include form a,b,c??then gcnew it all ??
E.F. Nijboer 3-Aug-10 12:39pm    
You have some starting point where the forms are created right? In your case you use A for that and is also the first form. Before you create the other two forms you simply create a vector or list and fill it with the forms and give a reference to this to the other forms B and C also.
list<Form> formList;
formList.push_back(this);
FormB^ B = gcnew FormB;
formList.push_back(B);
FormC^ C = gcnew FormC;
formList.push_back(B);
Give the other forms a reference to this list also using a self defined property for example:
B.FormList = formList;
C.FormList = formList;
This is the most simple form. It would be nicer to create a class to handle all the actions like previous and next and hide access to the forms directly and only serve the functionality needed.
If you still don't exactly get it just comment again and maybe I find some time this week to create an example for this.
tuolesi 4-Aug-10 5:09am    
thx for ur help...
i will go try this out first.....

btw..
will be great if u can write out a functional example .
It seems like you want to implement something very similar to a property-sheet, why don't you have a look to the CPropertySheet class[^]?
 
Share this answer
 
Comments
E.F. Nijboer 5-Aug-10 9:38am    
Reason for my vote of 1
I don't think this had anything to do with a property sheet.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900