在这个示例中,我们使用 NewForm
类的构造函数接收要传递的数据,并在新窗体的标签上显示出来。通过将数据作为参数传递给构造函数,我们实现了数据的传递。
这种方法更加清晰、可控,并且符合良好的封装和可测试性原则。您可以根据需要扩展和修改这个示例,以满足您的具体要求。
using System;
using System.Windows.Forms;
public class MainForm : Form
{
private TextBox textBox;
private Button button;
public MainForm()
{
textBox = new TextBox();
button = new Button();
textBox.Location = new System.Drawing.Point(50, 50);
button.Location = new System.Drawing.Point(50, 80);
button.Text = "打开新窗口";
button.Click += Button_Click;
Controls.Add(textBox);
Controls.Add(button);
}
private void Button_Click(object sender, EventArgs e)
{
string inputData = textBox.Text;
NewForm newForm = new NewForm(inputData);
newForm.ShowDialog();
}
[STAThread]
public static void Main()
{
Application.Run(new MainForm());
}
}
public class NewForm : Form
{
private Label label;
public NewForm(string data)
{
label = new Label();
label.Text = data;
label.Location = new System.Drawing.Point(50, 50);
Controls.Add(label);
}
}
改进的程序,关闭新窗口前弹出信息,再退出程序
using System;
using System.Threading.Tasks;
using System.Windows.Forms;
public class MainForm : Form
{
private TextBox textBox;
private Button button;
private NewForm newForm;
public MainForm()
{
textBox = new TextBox();
button = new Button();
textBox.Location = new System.Drawing.Point(50, 50);
button.Location = new System.Drawing.Point(50, 80);
button.Text = "打开新窗口";
button.Click += Button_Click;
Controls.Add(textBox);
Controls.Add(button);
}
private void Button_Click(object sender, EventArgs e)
{
string inputData = textBox.Text;
newForm = new NewForm(inputData);
newForm.Show();
Hide(); // 隐藏主窗体
//newForm.FormClosing += NewForm_FormClosing;
}
[STAThread]
public static void Main()
{
Application.Run(new MainForm());
}
}
public class NewForm : Form
{
private Label label;
public NewForm(string data)
{
label = new Label();
label.Text = data;
label.Location = new System.Drawing.Point(50, 50);
Controls.Add(label);
FormClosing += NewForm_FormClosing;
}
private void NewForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
MessageBox.Show("即将关闭窗体退出程序", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
e.Cancel = true; // 取消新窗体的关闭操作
Task.Delay(4000).Wait(); // 停顿4秒
FormClosing -= NewForm_FormClosing; // 取消事件绑定
Close(); // 关闭新窗体
Application.Exit(); // 退出整个程序
}
}
}
-----------------------------------------------------------------------------
在这个修改后的示例中,我添加了一个静态的 Globals
类,其中定义了一个公共静态属性 Data
,用于存储要传递的数据。当按钮被点击时,将文本框中的数据存储到 Globals.Data
中。然后,在新窗体的构造方法中,可以直接访问 Globals.Data
来获取传递的数据。
请注意,全局变量可以在整个应用程序中共享数据,但请注意在使用全局变量时要谨慎处理数据的同步和修改问题。
在 Windows Forms 应用程序中使用静态的 Globals 类来传递数据并不是一个良好的做法。静态变量在多线程环境下可能引发线程安全问题,并且不符合良好的封装和可测试性原则。
using System;
using System.Windows.Forms;
public static class Globals
{
public static string Data { get; set; }
}
public class MainForm : Form
{
private TextBox textBox;
private Button button;
public MainForm()
{
textBox = new TextBox();
button = new Button();
textBox.Location = new System.Drawing.Point(50, 50);
button.Location = new System.Drawing.Point(50, 80);
button.Text = "打开新窗口";
button.Click += Button_Click;
Controls.Add(textBox);
Controls.Add(button);
}
private void Button_Click(object sender, EventArgs e)
{
Globals.Data = textBox.Text;
NewForm newForm = new NewForm();
newForm.ShowDialog();
}
[STAThread]
public static void Main()
{
Application.Run(new MainForm());
}
}
public class NewForm : Form
{
private Label label;
public NewForm()
{
label = new Label();
label.Text = Globals.Data;
label.Location = new System.Drawing.Point(50, 50);
Controls.Add(label);
}
}