2009-08-18 31 views
1

我有一個自定義窗體將值返回給主窗體,但它沒有看到變量。我認爲我沒有把這個說得很清楚,所以我將包含我想要做的事例的鏈接。我的對話框返回值不起作用

我知道我可能忽視的東西很容易,或者明顯的,但這裏是我。

Form1.cs中:

private void addTime_Click(object sender, EventArgs e) 
{ 
    Form add = new addTime(false, new string[] { "", "" }); 
    if (add.ShowDialog(this) == DialogResult.OK) 
    { 
     // the line not working 
     Label1.Text = add.Details; 
     // reports with:'System.Windows.Forms.Form' does not contain a 
     // definition for 'Details' and no extension method 'Details' accepting  
     // a first argument of type 'System.Windows.Forms.Form' could be found (are you 
     // missing a using directive or an assembly reference?) 
    } 
} 

addTime.cs:

internal class addTime : Form 
{ 
    //.. 

    private string _details; 
    public string Details 
    { 
     get { return _details; } 
     private set { _details = value; } 
    } 

    private string _goalTime; 
    public string GoalTime 
    { 
     get { return _goalTime; } 
     private set { _goalTime = value; } 
    } 

    private void applybtn_Click(object sender, EventArgs e) 
    { 
     Details = detailslbl.Text; 
     GoalTime = goalTimelbl.Text; 
    } 
} 

回答

5

您的「添加」變量的類型爲Form,而不是addTime,而Form類型沒有Details屬性。

試試這個行:

addTime add = new addTime(false, new string[] { "", "" }); 
+0

哇!我知道它必須是簡單的!哈哈,謝謝,雖然我現在覺得自己像個白癡。 – Nyight 2009-08-18 17:56:50

+0

好的,我錯過了 – 2009-08-18 18:00:15

3

您需要設置子窗體

DialogResult = DialogResult.OK 
在點擊按鈕

的DialogResult屬性。

0

您需要將窗體的DialogResult屬性設置爲OK。你沒有在你的代碼中指定它。

在符合正確的標準後,您可以像這樣設置。

If (//condition) 
{ 
this.DialogResult = DialogResult.OK; 
This.Close(); 
}