Jumping Between Forms
We Can Access Form2 From Form1 By this way :
In Form1 :
private buttonForm1_click(object sender,EventArgs e)
{
Form2 frm2 = new Form2()
frm2.show()
this.hide()
}
And In Form2 , We want to access Form1 which it has hidden.
In Form2 :
private buttonForm2_click(object sender,EventArgs e)
{
//What Can I DO ?
//I Don't Want to Create Another I开发者_JS百科nstance From Form1
}
You can pass a Form1
object to Form2
constructor:
public partial class Form1
{
// ...
private buttonForm1_click(object sender,EventArgs e)
{
Form2 frm2 = new Form2(this)
frm2.show()
this.hide()
}
// ...
}
public partial class Form2
{
private Form1 _form1;
public Form2(Form1 form1)
{
InitializeComponents();
_form1 = form1;
}
// ...
private buttonForm2_click(object sender,EventArgs e)
{
_form1.Show();
}
}
If you want two forms to have access to each other, they need to know about each other somehow. One way to do this would be a parent/child relationship. However, this probably isn't what you want to do. The other option would be to make the references for each form scoped in such a way so that both have access to them.
A third way to do it would be to pass a reference to Form1 into Form2 when you open Form2. Create a Form1 variable inside of Form2 and populate it when you open Form2 from Form1. That will work when you want two forms to have access to each other but it won't scale well.
On Form1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Form1Form2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var f = new Form2(this);
f.Show();
}
}
}
On Form2:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Form1Form2
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
Form1 _f1 = null;
public Form2(Form1 f) : this()
{
_f1 = f;
}
private void button1_Click(object sender, EventArgs e)
{
_f1.Text = "Nice!";
}
}
}
The easy answer is that in Form2 constructor you pass a reference to Form1.
That way, in Form2 you have a reference you can use and access its controls or call a method in it.
If you are not creating multiple copies of your forms, I find this to be the easiest method to use. Have the form itself create a handle to it with:
public partial class Form1
{
public static Form1 Current;
public Form1()
{
InitializeComponents();
Current = this;
}
}
public partial class Form2
{
public static Form2 Current;
public Form2()
{
InitializeComponents();
Current = this;
}
private buttonForm2_click(object sender,EventArgs e)
{
Form1.Current.Show();
}
}
It gets a little more complicated if people close the form. So in the Closing handler do (if you want to keep a handle):
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
Visible = false;
WindowState = FormWindowState.Minimized;
}
The e.Cancel
will keep the form from actually being destroyed, requiring you to create it again. Though if you want it destroyed, you could always create a new one again by converting your Current variable to a Current property that create's a new one in it's get;
.
精彩评论