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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help Going back to Form1 2

Status
Not open for further replies.

aalmeida

MIS
Aug 31, 2000
468
US
I'm new to C# and am trying to create a WFA where I have the need to go from form 1 to Form2 and back.
My problem is coming back from form2 to form one.Bellow is the code on form2:
Code:
public partial class form2 : Form
    {
        public Object CallingForm;
        public Form frm2;

        public Ad_frm(Object caller)
        {
            // InitializeComponent();
            // Note which form has called this one
            CallingForm = caller;
           
        }
private void Voltar_Click(object sender, EventArgs e)
        {
            frm2 = new CallingForm();
            frm2.Show();
            this.Hide();
        }
}
On this line frm2 = new CallingForm(); I'm getting the following error:
'SigCard.Ad_frm.CallingForm' is a 'field' but is used like a 'type' E:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\SigCard\SigCard\Ad_frm.cs

what type should I use to declare CallingForm for this to work?

Thanks,

AL Almeida
CIO
May all those that come behind us, find us faithful.
 
Jason,

I was not able to find any relevant content in the link you provided.

I'm still looking for an answer to my question.

Thanks,

AL Almeida
CIO
May all those that come behind us, find us faithful.
 
Some thoughts:

1. The .ShowDialog() will show you the form2 and wont let you go back unless there is a DialogResult or you close the form2.

2. The .Show(?) takes a parameter which is the form owner. I think that you can use that when you are exiting the form2. Something like this.parentform.show ? I am not sure and never used it. Give it a try.

3. You can have a public class to hold info that everyone can see. Make a class library project. Every project should reference that class library project.
The items (variables, soutines, whatever) may be public and static for quick access. Make a variable where you will save there the "caller" form.

I have used the 3rd way a lot. (in vb.net)
 
Hey TipGiver,

If you look closely you will see that I'm using the 3rd approach as you described above.
My C# code is actually an adaptation from a VB.NET WF application.
The premise is that Form1 passes it's name to Form2 in the parameter "caller" like this:

Code:
frm2 = new Form2(this.Name);

Once on Form2:

Code:
public partial class form2 : Form
    {
        public Object CallingForm;
        public Form frm2;

        public Form2(Object caller)
        {
            // InitializeComponent();
            // Note which form has called this one
            CallingForm = caller;
           
        }
private void Voltar_Click(object sender, EventArgs e)
        {
            frm2 = new CallingForm();
            frm2.Show();
            this.Hide();
        }
}

The name of Form1 is passed onto the CallingForm() in here:

Code:
public Form2(Object caller)
        {
            // InitializeComponent();
            // Note which form has called this one
            CallingForm = caller;
           
        }

Oce the value is stored it can be used anytime to go back to Form1 like it is here:
Code:
private void Voltar_Click(object sender, EventArgs e)
        {
            frm2 = new CallingForm();
            frm2.Show();
            this.Hide();
        }

This works perfectly on VB.NET, I'm now trying the same approach using C#.NET but that is giving me the following error message:

'SigCard.Ad_frm.CallingForm' is a 'field' but is used like a 'type' E:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\SigCard\SigCard\Form1.cs
Now my question is: how should I declare the CallingForm() for this to work? as it stands right now it's like this:
Code:
 public Object CallingForm;

Thanks,


AL Almeida
CIO
May all those that come behind us, find us faithful.
 
if you're using a parent form, you can try this approach:

In the parent form, use this code to open the form:

Code:
        Form1 frm1;
        Form2 frm2;

        private void OpenForm1()
            {
            frm1 = new Form1();
            frm1.MdiParent = this;
            frm1.Show();
            frm1.FormClosing += new System.Windows.Forms.FormClosingEventHandler( onFormClosing );
            }

        private void OpenForm2()
            {
            frm2 = new Form2();
            frm2.MdiParent = this;
            frm2.Show();
            frm2.FormClosing += new System.Windows.Forms.FormClosingEventHandler( onFormClosing );
            }

        private void onFormClosing( object sender, System.Windows.Forms.FormClosingEventArgs e )
            {
            // this is the Form1.Text text
            if ( sender.ToString().Contains( "Form1" ) )
                {
                OpenForm2();
                }
            else if ( sender.ToString().Contains( "Form2" ) )
                {
                OpenForm1();
                }
            }

When frm1 closes, it executes the code in the onFormClosing subroutine and opens Form2. Vice-versa for when frm2 closes. It's up to you to figure out how to get the two forms closed when you want to end toggling between them.

HTH

Chew

10% of your life is what happens to you. 90% of your life is how you deal with it.
 
ChewDoggie!

Hmm really interesting, but the solution I described already take these things into consideration, it actually works just fine with VB.NET Forms application.

My issue with C# is that I'm not sure what type I need to declare public Object CallingForm; the current type "Object" gives me the error message I mentioned above.

What are your thoughts on the code I presented?

AL Almeida
CIO
May all those that come behind us, find us faithful.
 
Hurray All,

Finally one solution.
I'm posting the solution here for the benefit of all:
Code:
public partial class form2 : Form
    {
        public String CallingForm;
        public Form frm2;

        public Form2(String caller)
        {
            // InitializeComponent();
            // Note which form has called this one
            CallingForm = caller;
           
        }
private void Voltar_Click(object sender, EventArgs e)
        {
            switch(CallingForm)
            {
                case "Form1":
                    frm2 = new Form1(this.Name);
                    frm2.Show();
                    this.Hide();
                    break;
                case "Form3": // you are already on Form2
                    frm2 = new Form3();
                    frm2.Show();
                    this.Hide();
                    break;
                default:
                    frm2 = new Form0();
                    frm2.Show();
                    this.Hide();
                    break;
            }

        }

This is not exactly what I was looking for but it works just fine when you have just a few forms it might be to cumbersome if you have more that 10 forms.

Thanks to all that try to help me with this.

AL Almeida
CIO
May all those that come behind us, find us faithful.
 
yes, it would be:) this is where the concept of the screen activation pattern plays in. I understand there isn't too much information on the link I provided. The wiki was only launched a week or two ago.

At a minimum I would encapsulate your switch statement into strategies (strategy pattern) so you can abstract the Form being displayed. this would ease the management of new forms when they are added to the system.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top