0

I am creating an windows application in that i used two forms.First form contain first datetimepicker and in second form contain second datetimepicker .If i am select any date in first datetimepicker then in second datetimepicker display future date which is 6 month differences.following is example

if i select date in first datetimepicker i.e. 8/9/2012 (mm:dd:yyy) then

in second datetimepicker display date i.e. 2/8/2013(mm:dd:yyy) there is must 6 month difference in second datetimepicker. .

4

2 回答 2

0

在第二次阅读您的问题时,我现在看到您有两种不同的形式。您需要DateTimePicker.ValueChanged在第一种形式中公开事件,然后DateTimePicker.Value在第二种形式中公开属性。例如:

class DateEventArgs : EventArgs
{
    public DateTime Date {get; private set;}

    public DateEventArgs(DateTime d) 
        : base()
    {
        this.Date = d;
    }
}

class FirstForm : Form
{
    DateTimePicker dtp = new DateTimePicker();

    public event EventHandler<DateEventArgs> DateSelected;
    protected virtual void OnDateSelected(DateEventArgs e)
    {
        EventHandler<DateEventArgs> handler = DateSelected;
        if(handler != null) handler(this, e);
    }

    public FirstForm()
    {
        this.Controls.Add(dtp);
        dtp.ValueChanged += new EventHandler(dtp_ValueChanged);
    }

    void dtp_ValueChanged(object sender, EventArgs e)
    {
        DateEventArgs dea = new DateEventArgs(dtp.Value.Date);
        OnDateSelected(dea);
    }
}

class SecondForm : Form
{
    DateTimePicker dtp = new DateTimePicker();

    public DateTime SelectedDate
    {
        set { dtp.Value = value; }
    }

    public SecondForm()
    {
        this.Controls.Add(dtp);
    }
}

static class Controller
{
    FirstForm f1;
    SecondForm f2;

    static void Go()
    {
        f1 = new FirstForm();
        f2 = new SecondForm();
        f1.DateSelected += new EventHandler<DateEventArgs>(f1_DateSelected);

        f1.Show();
        f2.Show();
    }

    static void f1_DateSelected(object sender, DateEventArgs e)
    {
        f2.SelectedDate = e.Date.AddMonths(6);
    }
}
于 2012-08-09T05:13:31.990 回答
0

这应该有帮助

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // Initialize picker 
        dateTimePicker2.Value = Convert.ToDateTime(ObjectForValue1).AddMonths(6);
    }

    private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
    {
        // Set title bar to selected date.
        DateTime result = dateTimePicker2.Value;
        this.Text = result.ToString();
    }
    }
}
于 2012-08-09T05:30:04.180 回答