C#多个Windows表格无法引用
C# multiple Windows Forms cannot reference
我有2个Windows form1
(monitor 1 - 1920x1080)和form2
(monitor 2 - 1920x1080),form 1(monitor 1)有3个函数需要调用函数来自 form2(显示器 2)。
我必须在 Form1
中创建 form2 (screen2) 3 次,但它们不会是同一个 form2,对吗?有解决此问题的建议吗?
public partial class Form2 : Form
{
// Variables
private Form1 _ParentForm; // Add this here
// Constructor
public Form2(Form1 parentForm)
{
InitializeComponent();
_ParentForm = parentForm; // Add this here
}
public void Rec_image()
{
// Form 2 Image
PictureBox1.Show();
PictureBox2.Hide();
}
public void Charging()
{
// Form 2 Image
PictureBox6.Hide();
PictureBox1.Hide();
PictureBox2.Hide();
PictureBox3.Hide();
PictureBox4.Hide();
PictureBox5.Hide();
PictureBox7.Show();
}
}
public partial class Form1 : Form
{
private void Form1_Load(object sender, EventArgs e)
{
Form2 form2 = new Form2(this);
form2.Show();
}
private async void Button2_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2(this);
form2.Rec_image();
}
private void Button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2(this);
form2.Charging();
}
}
将对 Form2
实例的引用存储在 Form1
class 的字段中,您可以一次又一次地访问同一个实例。
public partial class Form1 : Form
{
private Form2 _form2; // field
private void Form1_Load(object sender, EventArgs e)
{
_form2 = new Form2(this);
_form2.Show();
}
private async void Button2_Click(object sender, EventArgs e)
{
_form2.Rec_image();
}
private void Button1_Click(object sender, EventArgs e)
{
_form2.Charging();
}
}
我有2个Windows form1
(monitor 1 - 1920x1080)和form2
(monitor 2 - 1920x1080),form 1(monitor 1)有3个函数需要调用函数来自 form2(显示器 2)。
我必须在 Form1
中创建 form2 (screen2) 3 次,但它们不会是同一个 form2,对吗?有解决此问题的建议吗?
public partial class Form2 : Form
{
// Variables
private Form1 _ParentForm; // Add this here
// Constructor
public Form2(Form1 parentForm)
{
InitializeComponent();
_ParentForm = parentForm; // Add this here
}
public void Rec_image()
{
// Form 2 Image
PictureBox1.Show();
PictureBox2.Hide();
}
public void Charging()
{
// Form 2 Image
PictureBox6.Hide();
PictureBox1.Hide();
PictureBox2.Hide();
PictureBox3.Hide();
PictureBox4.Hide();
PictureBox5.Hide();
PictureBox7.Show();
}
}
public partial class Form1 : Form
{
private void Form1_Load(object sender, EventArgs e)
{
Form2 form2 = new Form2(this);
form2.Show();
}
private async void Button2_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2(this);
form2.Rec_image();
}
private void Button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2(this);
form2.Charging();
}
}
将对 Form2
实例的引用存储在 Form1
class 的字段中,您可以一次又一次地访问同一个实例。
public partial class Form1 : Form
{
private Form2 _form2; // field
private void Form1_Load(object sender, EventArgs e)
{
_form2 = new Form2(this);
_form2.Show();
}
private async void Button2_Click(object sender, EventArgs e)
{
_form2.Rec_image();
}
private void Button1_Click(object sender, EventArgs e)
{
_form2.Charging();
}
}