You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WinForms C#:如何从另一个窗体返回值(日期传递)

How to Pass Selected Date from Form2 Back to Form1 in WinForms

Hey there! Let's get that date value from Form2 over to Form1 so you can update your grid row. I'll walk you through two straightforward, widely-used approaches—pick whichever fits your workflow better.

Approach 1: Use a Public Property + ShowDialog()

This is the simplest method, great for basic scenarios where you just need to grab a value after the user closes Form2.

Step 1: Add a Public Property to Form2

In your Form2 code, add a public property to store the selected date. Make sure you set it when the user confirms their selection (like clicking an "OK" button):

public partial class Form2 : Form
{
    // Public property to hold the selected date
    public DateTime SelectedDate { get; private set; }

    public Form2()
    {
        InitializeComponent();
    }

    private void btnOK_Click(object sender, EventArgs e)
    {
        // Assume you're using a DateTimePicker named dtpDateSelector
        SelectedDate = dtpDateSelector.Value;
        // Set DialogResult so Form1 knows the user confirmed
        this.DialogResult = DialogResult.OK;
        this.Close();
    }

    private void btnCancel_Click(object sender, EventArgs e)
    {
        this.DialogResult = DialogResult.Cancel;
        this.Close();
    }
}

Step 2: Retrieve the Date in Form1

When you open Form2 from Form1, use ShowDialog() to wait for it to close, then check the DialogResult to confirm the user didn't cancel. If they confirmed, grab the SelectedDate property and update your grid:

private void btnEditDate_Click(object sender, EventArgs e)
{
    // Get the currently selected row in your grid (adjust based on your grid control)
    var selectedRow = dataGridView1.SelectedRows[0];
    if (selectedRow == null) return;

    using (var form2 = new Form2())
    {
        // Optional: Pre-set the date picker to the row's current date
        form2.dtpDateSelector.Value = (DateTime)selectedRow.Cells["DateColumn"].Value;

        if (form2.ShowDialog() == DialogResult.OK)
        {
            // Update the selected row's date with the value from Form2
            selectedRow.Cells["DateColumn"].Value = form2.SelectedDate;
            // Don't forget to save your changes to the data source if needed!
        }
    }
}

Approach 2: Use Delegates & Events (More Flexible)

If you want a more decoupled approach (or need to update the grid in real-time as the user selects a date), events are the way to go.

Step 1: Define an Event in Form2

Add an event that Form1 can subscribe to, which will fire when the user selects a date:

public partial class Form2 : Form
{
    // Define an event that passes a DateTime when triggered
    public event Action<DateTime> DateSelected;

    public Form2()
    {
        InitializeComponent();
    }

    private void btnOK_Click(object sender, EventArgs e)
    {
        // Trigger the event with the selected date
        DateSelected?.Invoke(dtpDateSelector.Value);
        this.Close();
    }
}

Step 2: Subscribe to the Event in Form1

When opening Form2, hook up the event handler to update your grid as soon as the user confirms:

private void btnEditDate_Click(object sender, EventArgs e)
{
    var selectedRow = dataGridView1.SelectedRows[0];
    if (selectedRow == null) return;

    var form2 = new Form2();
    // Pre-set the current date if needed
    form2.dtpDateSelector.Value = (DateTime)selectedRow.Cells["DateColumn"].Value;

    // Subscribe to the DateSelected event
    form2.DateSelected += (selectedDate) =>
    {
        selectedRow.Cells["DateColumn"].Value = selectedDate;
        // Save changes to your data source here
    };

    form2.ShowDialog();
    form2.Dispose();
}

Quick Notes:

  • Make sure your Form2's date picker (dtpDateSelector in the examples) is accessible—if it's private, you can either make it internal/public or set the initial date via a constructor parameter instead.
  • Always handle cases where no row is selected in Form1 to avoid null reference errors.

内容的提问来源于stack exchange,提问作者user3127554

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:35:03