Passing a Date Value from one Windows From to Another Form in C#

前端 未结 4 1057
轮回少年
轮回少年 2021-01-16 16:33

I am working on a very complex project and I am very very new to the windows Project.
I have 2 Forms :

  • ViewSchedule.cs
  • Sch
相关标签:
4条回答
  • 2021-01-16 17:00

    You should give Scheduler an explicit data source and use events to notify it of changes to the underlying dates. A good idea would be giving the data source its own interface:

    interface IFromToDateProvider : INotifyPropertyChanged
    {
        DateTime From { get; }
        DateTime To { get; }
    }
    

    Then have ViewSchedule implement this interface:

    class ViewSchedule : IFromToDateProvider
    {
        DateTime _from;
    
        public DateTime From
        {
            get { return _from; }
            set
            {
                if (_from == value) return;
    
                _from = value;
                OnPropertyChanged("From");
            }
        }
    
        DateTime _to;
    
        public DateTime To
        {
            get { return _to; }
            set
            {
                if (_to == value) return;
                _to = value;
                OnPropertyChanged("To");
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
    

    Make sure to update the values of from and to using the properties so the event gets fired. Alternately, you can make From and To computed properties that just grab the value from the Calendars you mention; once again, make sure to fire OnPropertyChanged when the underlying calendar values change. If you're using MonthCalendar, you can do this by listening to its DateChanged event.

    Then, have your Scheduler take a IFromToDateProvider as a constructor parameter and listen to its PropertyChanged event:

    class Scheduler
    {
        readonly IFromToDateProvider _provider;
        public Scheduler(IFromToDateProvider provider)
        {
            _provider = provider;
            _provider.PropertyChanged += provider_PropertyChanged;
        }
    
        void provider_PropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs)
        {
            // update display with new values in _provider.From and/or 
            // _provider.To in this event handler
        }
    }
    

    This is assuming that ViewSchedule creates a Scheduler instance. If it's vice versa, just have Scheduler listen to the event after it creates the ViewSchedule. If neither, just set it as a property; the essential part is that you end up with Scheduler listening to the PropertyChanged event of a ViewSchedule.

    0 讨论(0)
  • 2021-01-16 17:06

    You need to create public properties in the dialog form and set these properties before showing the dialog.

    Then onLoad use these property values.

    In form2 add these date properties:

    public partial class Form2 : Form
    {
        public DateTime Date1 { get; set; }
        public DateTime Date2 { get; set; }
    
        public Form2()
        {
            InitializeComponent();
        }
    }
    

    And from form1 access these as follows:

    private void Form1_Load(object sender, EventArgs e)
    {
       Form2 frm2 = new Form2();
       frm2.Date1 = DateTime.Now;
       frm2.Date2 = DateTime.Now;
       frm2.ShowDialog();
    }
    
    0 讨论(0)
  • 2021-01-16 17:14

    The simplest way is to make sure that these two variables that define the two dates are public static so that they can be accessed through the other form. If you'd like to update the other form only if the value is changed. Then, I'd suggest you to have a Timer in the second form and a bool in the first form that indicates whether the date is changed or not.

    Example

    ViewSchedule.cs

    //These values must be static and public so that they'd be accessible through the second form
    public static bool DateChanged = false;
    public static DateTime _fromDate;
    public static DateTime _toDate;
    
    private void SetValues()
    {
        _fromDate = dtFromDate.DateTime.ToUniversalTime();
        _toDate = dtToDate.DateTime.ToUniversalTime();
        DateChanged = true; //Set DateChanged to true to indicate that there has been a change made recently
    }
    

    Scheduler.cs

    public Scheduler()
    {
        InitializeComponent();
        timer1.Tick += new EventHandler(timer1_Tick); //Link the Tick event of timer1 to timer1_Tick
    }
    
    DateTime _fromDate; //Set a new variable of name _fromDate which will be used to get the _fromDate value of ViewSchedule.cs
    DateTime _toDate; ////Set a new variable of name _toDate which will be used to get the _toDate value of ViewSchedule.cs
    
    private void timer1_Tick(object sender, EventArgs e)
    {
        if (Form1.DateChanged) //Check if a change has been done recently
        {
            Form1.DateChanged = false; //Set the change to false so that the timer won't repeat
            _fromDate = Form1._fromDate; //Set our value of _fromDate from Form1._fromDate
            _toDate = Form1._toDate;//Set our value of _toDate from Form1._toDate
        }
    }
    

    This will set the values of Scheduler.cs to the new values from ViewSchedule.cs which I think is what you would like to achieve

    Thanks,
    I hope you find this helpful :)

    0 讨论(0)
  • 2021-01-16 17:15

    I would not bother with static fields... This type of question of passing values back/forth between forms has been answered many times... So to is this link to a previous question I so answered. It even has a step-by-step on creating two forms and getting values passed back-and-forth via getter/setter and even events. Hope this helps you out.

    0 讨论(0)
提交回复
热议问题