Solved

C# - Calling to Form2 from Form1

Posted on 2009-05-14
15
2,313 Views
Last Modified: 2013-12-17
Hello,

I have the following code. I am calling to Form2 from Form1.
At Form2 code I need to open Form1 back again: form1.show();
and to use myFlag parameter.

But form1 is not defined in Form2 class. It is just an object that called Form2.
How to do it?

THANKS
Margarit
public partial class Form1: Form

    {

              Form2 form2 = new Form2();

               public int myFlag;

              public Form1()

             {

                     form2.Show();

                     InitializeComponent();

             }

Open in new window

0
Comment
Question by:margarit
15 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 24383499
You could mark your myFlag as static and it would then be class-level instead of instance-level. Then use form2.ShowDialog() as such:
public partial class Form1: Form

{

    Form2 form2 = new Form2();

    public static int myFlag;

    

    public Form1()

    {

        form2.ShowDialog();

        InitializeComponent();

    }

    

    

/////////////////////////////////////

// Within Form2 code

/////////////////////////////////////

public partial class Form2: Form

{
 

    public void SomeFunction()

    {

        Form1.myFlag = 100;  // Example of interacting with static variable; Use ClassName.VariableName to access

    }

Open in new window

0
 

Author Comment

by:margarit
ID: 24383551
THANKS!!!!
It helped!
Another question:
How can I show Form1 from Form2. To go back to Form1.
THANKS
0
 
LVL 12

Expert Comment

by:GuitarRich
ID: 24383576
Or you could pass the instance of form1 into the constructor of form2 and keep a reference there like this:
public partial class Form1: Form

{

    Form2 form2 = new Form2(this);

    public int myFlag;
 

    public Form1()

    {

        form2.ShowDialog();

        InitializeComponent();

    }

    

    

/////////////////////////////////////

// Within Form2 code

/////////////////////////////////////

public partial class Form2: Form

{

    private form1Instance;
 

    public Form2(Form1 myForm1)

    {

        form1Instance = myForm1;

        form1Instance .myFlag = 100;  // Example of interacting with public variable;

    }

}

Open in new window

0
 

Author Comment

by:margarit
ID: 24383578
Hello,
I used form2.ShowDialog() . But inside form2 there is no Form1.Show() option
0
 

Author Comment

by:margarit
ID: 24383620
In a case I need to go back from form1 to form2. how to do it?
There is no option form1.Show();
0
 
LVL 12

Expert Comment

by:GuitarRich
ID: 24383663
you need to create an instance of the form to use the .Show() method
Have a look at my example earlier of passing the instance of Form1 to Form2
Although as you are showing form2 as a modal dialog you will can only show a new instance of Form1 not the original one. To be able to show the original one you need to change the call to show form2 to
form2.Show();
 
0
 

Author Comment

by:margarit
ID: 24383737
private form1Instance;
Should it be:
 private Form form1Instance;


 
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 12

Expert Comment

by:GuitarRich
ID: 24383752
sorry yeah shouldbe
private Form1 form1Instance;
0
 

Author Comment

by:margarit
ID: 24383778
THANKS :)
Form2 form2 = new Form2(this);
I get an error that this is not allowed here
0
 
LVL 12

Expert Comment

by:GuitarRich
ID: 24383792
did you change the constructor in form2 to take a parameter of the type Form1 like this:
    public Form2(Form1 myForm1)

    {

        form1Instance = myForm1;

        form1Instance .myFlag = 100;  // Example of interacting with public variable;

    }

Open in new window

0
 

Author Comment

by:margarit
ID: 24383812
yes, I did
0
 
LVL 12

Accepted Solution

by:
GuitarRich earned 500 total points
ID: 24383839
ah sorry I see the issue - change it to this - I have change where you initialise the form2 object to within the form1 constructor.
public partial class Form1: Form

{

    Form2 form2;

    public int myFlag;

 

    public Form1()

    {

        form2 = new Form2(this);

        form2.ShowDialog();

        InitializeComponent();

    }

    

    

/////////////////////////////////////

// Within Form2 code

/////////////////////////////////////

public partial class Form2: Form

{

    private form1Instance;

 

    public Form2(Form1 myForm1)

    {

        form1Instance = myForm1;

        form1Instance .myFlag = 100;  // Example of interacting with public variable;

    }

}

Open in new window

0
 
LVL 6

Expert Comment

by:HarryNS
ID: 24383923
You can use ShowDialog,

Form2 frm = new Form2();
frm.ShowDialog(this);

You can mention ShowDialog(this), this defines your Form 1 is the owner of Form 2.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24387675
If you are showing Form2 via ShowDialog() then all you need to do is close Form2 to get back to Form1...

    // in Form2
    this.Close() // <--- code will now RETURN to Form1 after the ShowDialog() call!

...since execution in Form1 STOPS until your instance of Form2 is closed.
0
 

Author Closing Comment

by:margarit
ID: 31581379
THANKS
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now