2012-11-01 49 views
0

我正在處理一個非常複雜的項目,我對windows項目非常陌生。
我有2種形式:將日期值從一個窗口傳遞到C中的另一個窗體#

  • ViewSchedule.cs
  • Scheduler.cs(它是一個對話表)

ViewSchedule.cs必須被從日曆中選擇的兩個日期。選擇兩個日期。
它們被保存在RESP:

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

表2即Scheduler.cs是dialogForm。這裏應該顯示在ViewScheduler中選擇的日期。

我需要幫助。

+2

「我需要幫助」不是一個有效的問題。我幾次重新閱讀您的文章,仍然無法找到問題。 – LightStriker

+0

如果我必須假設你說你想從form1訪問一些數據並在form2中顯示它?我對麼 ? – pordi

+0

是的,Form1日期值應出現在Form2中。 – user1141927

回答

1

您需要在對話框中創建公共屬性並在顯示對話框之前設置這些屬性。

然後onLoad使用這些屬性值。

在窗口2添加這些日期屬性:

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

    public Form2() 
    { 
     InitializeComponent(); 
    } 
} 

而且從Form1中訪問這些如下:

private void Form1_Load(object sender, EventArgs e) 
{ 
    Form2 frm2 = new Form2(); 
    frm2.Date1 = DateTime.Now; 
    frm2.Date2 = DateTime.Now; 
    frm2.ShowDialog(); 
} 
+0

你能解釋一下怎麼做? – user1141927

0

最簡單的方法是確保定義兩個日期這兩個變量public static,以便可以通過其他表單訪問它們。如果您想只在價值改變的情況下更新其他表單。然後,我建議你在第二種形式中有Timer,第一種形式中有bool,表示日期是否被更改。

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 
    } 
} 

這將設置的Scheduler.cs爲新值的值從ViewSchedule.cs我認爲這是你想實現

感謝什麼,
我希望對您有所幫助:)

+0

謝謝你會試試這個 – user1141927

+0

認真嗎?一個計時器和一個髒標誌來輪詢一個全局變量?當然,這可以用不太笨重的方式完成。 – millimoose

+0

@millimoose我試圖提供最簡單的方法來做到這一點。當然,這可以通過許多其他方式來完成。據我所知,這是最簡單的方法。請注意你的語言。有一個美好的一天:) –

0

你應該給Scheduler明確數據源和使用事件通知其基礎日期的變化。一個好的想法是給數據源自己的接口:

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

然後讓ViewSchedule實現這個接口:

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)); 
     } 
    } 
} 

請務必及時更新的值,從和使用性能,因此事件被解僱。或者,您可以製作FromTo計算出的屬性,以獲取您提到的Calendar的值;再次確保在基礎日曆值更改時觸發OnPropertyChanged。如果您使用的是MonthCalendar,您可以通過聽其DateChanged event來做到這一點。

然後,有你Scheduler採取IFromToDateProvider作爲構造函數的參數,並聽取其PropertyChanged事件:

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 
    } 
} 

這是假設ViewSchedule創建Scheduler實例。如果反之亦然,只要Scheduler在創建ViewSchedule後收聽該事件。如果兩者都不相同,只需將其設置爲屬性;主要部分是你最終收到SchedulerPropertyChanged事件ViewSchedule

0

我不會打擾靜態字段...這種類型的值之間傳遞值的問題已經被回答了很多次......所以對於前面的問題,我回答了this link。它甚至可以一步一步地創建兩個表單,並通過getter/setter和事件來回傳遞值。希望這可以幫助你。

相關問題