0

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

  • ViewSchedule.cs
  • Scheduler.cs (it is a Dialog Form)

ViewSchedule.cs has two dates to be selected from the Calendar. Two dates are selected.
They are saved in Resp:

_fromDate = dtFromDate.DateTime.ToUniversalTime();
_toDate = dtToDate.DateTime.ToUniversalTime();

Form 2 ie Scheduler.cs is a dialogForm. Here the dates selected in ViewScheduler should appear here.

I need help.

Picrofo Software
  • 5,475
  • 3
  • 23
  • 37
Hima
  • 63
  • 1
  • 9
  • 2
    "I need help" isn't a valid question. I re-read your post a few time, and still can't find the question. – LightStriker Nov 01 '12 at 21:25
  • If I have to assume then you are saying that you want to access some data from form1 and show it in form2? Am I correct ? – Pradip Nov 01 '12 at 21:31
  • Yes,The Form1 date value should appear in the Form2. – Hima Nov 01 '12 at 21:33
  • How about keeping a static property that will be set once you set a date and then access it on form 2 ? Will that help? – Pradip Nov 01 '12 at 21:42

4 Answers4

1

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();
}
nilobarp
  • 3,806
  • 2
  • 29
  • 37
0

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 :)

Picrofo Software
  • 5,475
  • 3
  • 23
  • 37
  • Seriously? A timer and a dirty flag to poll a global variable? Surely this can be done in a less clunky way. – millimoose Nov 01 '12 at 23:10
  • @millimoose I've tried to provide the simplest way to do this. Surely, this can be done in many other ways. As far as I believe, this is the simplest way. Please watch your language. Have a great day :) – Picrofo Software Nov 01 '12 at 23:28
  • @PicrofoEGY It's simple because it's a bad design, and promotes an incredibly bad practice. Using a global variable when you need a chunk of data accessible in more than one class should be your last resort, not the first thing you reach to. – millimoose Nov 01 '12 at 23:38
  • @millimoose Sorry about that. Perhaps someone else may answer this. Sorry again. – Picrofo Software Nov 01 '12 at 23:40
0

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.

millimoose
  • 39,073
  • 9
  • 82
  • 134
0

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.

Community
  • 1
  • 1
DRapp
  • 47,638
  • 12
  • 72
  • 142