[英]Switching between two Windows forms
I am working on a game that utilizes Windows Forms in C#. 我正在开发一个使用C#中的Windows窗体的游戏。 I want to be able to use the first form to call a second form.
我希望能够使用第一种形式调用第二种形式。 I have this working.
我有这个工作。 Then I would like for the second form to send data back to the first form rather than creating a new instance of the first form.
然后,我希望第二个表单将数据发送回第一个表单,而不是创建第一个表单的新实例。 Can this be done?
能做到吗? I know I need to have my properties set up so that I can set the variables from one form to the other.
我知道我需要设置属性,以便可以将变量从一种形式设置为另一种形式。 I am just not sure how to go about calling the first form without creating a new instance of it.
我只是不确定如何在不创建第一个表单的情况下调用第一个表单。 Is there a way that this can be done?
有没有办法可以做到这一点? For example if I have Form A create an instance of Form B, can I have Form B do some work and send the data back to the original Form A without creating a new instance of Form A?
例如,如果我有表单A创建表单B的实例,我可以让表单B做一些工作并将数据发送回原始表单A而不创建表单A的新实例吗?
It's hard to say without knowing your full requirements. 不知道您的全部要求就很难说。 But generally I go like this (Somewhat psuedo code).
但是总的来说,我是这样的(有点伪代码)。
Form2 dialogForm = new Form2();
if(dialogForm.ShowDialog() == DialogResult.OK)
{
this.PropertyOnForm1 = dialogForm.PropertyOnForm2
}
This ofcourse relies that your second form is a dialog. 当然,这取决于您的第二种形式是对话框。 You will need to set the dialogresult buttons on Form2, and have a public property that will be accessed from Form1 once the dialog has been completed.
您将需要在Form2上设置dialogresult按钮,并具有一个公共属性,对话框完成后将可以从Form1对其进行访问。
Let me know if this doesn't work and I'll write up a different answer. 让我知道这是否行不通,我将写一个不同的答案。
If you don't use the Data sent back Form A
right away then you could use the Form_Closing
event handler Form B
and then a public property in Form B
also. 如果不使用数据发送回
Form A
马上,那么你可以使用Form_Closing
事件处理Form B
,然后一个公共属性在Form B
也。
In your Form A
it could look like this: 在您的
Form A
它可能看起来像这样:
public partial class FormA : Form
{
FormB frmB = new FormB(); // Instantiate FormB so that you could create an event handler in the constructor
public FormA()
{
InitializeComponent();
// Event Handler for Form Closing
frmB.FormClosing += new FormClosingEventHandler(frmB_FormClosing);
}
void frmB_FormClosing(object sender, FormClosingEventArgs e)
{
String fromFormB = frm2.FormBData; // Get Data from Form B when form is about to close
}
private void button1_Click(object sender, EventArgs e)
{
frmB.ShowDialog(); // Showing Form B
}
}
And in your Form B
it could look like this: 在您的
Form B
它可能看起来像这样:
private void button1_Click(object sender, EventArgs e)
{
// Let just say that the data is sent back once you click a button
FormBData = "Hello World!";
Close();
}
public String FormBData { get; set; }
Since you are creating Form2 in Form1
, you can create a custom event in Form2
and subscribe to it in Form1
at the time that you create Form2
, if you are returning information from Form2
when you are closing it then Edper's or MindingData's answers will work. 由于您是在
Form1
中创建Form2的,因此您可以在Form2
创建一个自定义事件并在创建Form2
时在Form1
中对其进行订阅,如果您在关闭Form2
时从Form2
返回信息,那么Edper或MindingData的答案将起作用。
Here is a quick and dirty example using EventHandler<TEventArgs>
这是一个使用
EventHandler<TEventArgs>
的快速而肮脏的示例
Form1 表格1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.myCustomEvent += frm2_myCustomEvent;
frm2.Show();
}
void frm2_myCustomEvent(object sender, string e)
{
this.Text = e;
}
}
Form2 表格2
public partial class Form2 : Form
{
public event EventHandler<string> myCustomEvent;
int count;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
count +=1;
myCustomEvent(sender, count.ToString());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.