C# はコンストラクターとグローバル変数を介してフォーム間でデータを渡します (簡単な入力)

この例では、クラスのコンストラクターを使用して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静的クラスを追加しましたボタンをクリックすると、テキストボックス内のデータが に保存されますその後、新しいフォームのコンストラクターで、渡されたデータに直接アクセスして取得できます。DataGlobals.DataGlobals.Data

グローバル変数はアプリケーション全体でデータを共有できますが、グローバル変数を使用する場合はデータの同期と変更の問題に注意してください。

Windows フォーム アプリケーションでデータを渡すために静的 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);
    }
}

おすすめ

転載: blog.csdn.net/book_dw5189/article/details/131731983