如何在C#windows应用程序中连接2个表单 [英] How to Connect 2 forms in C# windows applications
问题描述
我正在创建一个C#Windows表单应用程序,其中有2个表单Form1和Form2。我需要以一种方式连接这两个表单,当一个表单处于活动状态时,应该禁用其他表单。例如,Form1在点击事件上有一个按钮,其中表单2以打开方式打开
Hi,
I am creating a C# Windows forms application where there are 2 forms Form1 and Form2. I need to connect these 2 forms in a way that when one form is active other should be disabled. For eg, Form1 has a button on the click event of which form 2 is opened in the way
Form2 newForm= new Form2();
newForm.Show();
this.Hide();
现在我想要激活Form2时的功能,那么打开的Form1应该被停用或禁用,并且当Form2关闭时应该再次激活它。任何人都可以帮我解决这个问题issue.Thanks in Advance。
谢谢
Jashobanta Chakraborty
Now I want the functionality that when the Form2 is activated,then the Form1 which is open should be deactivated or Disabled, and it should be activated again when the form Form2 is closed .Can anybody help me out on solving this issue.Thanks in Advance.
Thanks
Jashobanta Chakraborty
推荐答案
反之亦然,而是使用ShowDialog;
Do it the other way around, and use ShowDialog instead;
Form2 newform = new Form2();
this.Hide();
newForm.ShowDialog();
this.Show();
ShowDialog
直到 newform
已关闭。
你可以这样做:
(我不确定质量,但它适用于我)
You can do that on this way:
(I'm not sure for the quality, but it works for me)
private void button1_Click(object sender, EventArgs e)
{
Form2 newForm = new Form2(this);
newForm.Show();
this.Hide();
}
//In Form2
Form parentForm;
public Form2(Form form)
{
InitializeComponent();
this.parentForm = form;
}
// and on closing event
private void Form2_FormClosing(object sender, EventArgs e)
{
this.parentForm.Visible = true;
}
链接两个表单(Form1知道Form2,Forms2知道Form1)。
然后使用Form.Activate
,Form.Deactivate
,Form.Closed
事件在
Link the two forms (Form1 knows about Form2 and Forms2 knows about Form1).
Then useForm.Activate
,Form.Deactivate
,Form.Closed
events on both
这篇关于如何在C#windows应用程序中连接2个表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!