2012-06-04 26 views
0

我創建了一個包含標籤,DatetimePicker和一個OK按鈕的僞對話框(FormBorderStyle屬性設置爲FixedDialog)。確定按鈕的DialogResult屬性是「確定」爲什麼我的僞對話框隱藏了它的「魅力」並忽略了它自己的屬性?

改編自此文章:How to return a value from a Form in C#?,我試圖創建一種方式來提示用戶選擇一個日期,當我的應用程序無法suss出日期的文件名有時可以,有時不能,這取決於是否在文件名是

的代碼是下面

的問題是,被調用時它的形式不顯示其控制「良好地形成。」;並且,它不顯示在屏幕的中心,儘管它的StartPosition = CenterScreen ... ???

public partial class ReturnDate : Form { 

public DateTime ReturnVal { get; set; } 
private String _lblCaption; 

public ReturnDate() { 
    InitializeComponent(); 
} 

public ReturnDate(String lblCaption) { 
    _lblCaption = lblCaption; // "Object not set to an instance of an object" if I try to set the Label here directly 
} 

private void buttonOK_Click(object sender, EventArgs e) 
{ 
    this.ReturnVal = dateTimePicker1.Value.Date; 
    this.Close(); 
} 

private void ReturnDate_Shown(object sender, EventArgs e) { 
    labelCaption.Text = _lblCaption; 
} 

}

...我有條件地調用它像這樣:

public static DateTime getDateTimeFromFileName(String SelectedFileName) { 
    // Expecting selected files to be valid pilsner files, which are expected 
    // to be of the format "duckbilledPlatypus.YYYY-MM-DD.pil" such as: 
    // "duckbilledPlatypus.2011-06-11.pil" 
    const int DATE_BEGIN_POS = 19; 
    const int DATE_LENGTH = 10; 

String substr = string.Empty; 
if (SelectedFileName.Length >= DATE_BEGIN_POS + DATE_LENGTH) { 
    substr = SelectedFileName.Substring(DATE_BEGIN_POS, DATE_LENGTH); 
} 
DateTime dt = DateTime.Now; 
if (!(DateTime.TryParse(substr, out dt))) { 
    using (var dtpDlgForm = new ReturnDate("Please select the Date that the file was created:")) { 
     DialogResult dr = dtpDlgForm.ShowDialog(); 
     if (dr == DialogResult.OK) { 
      dt = dtpDlgForm.ReturnVal; 
     } 
    } 
} 
return dt; 

}

回答

2

如果你添加一個新的構造函數重載的形式(或任何派生控制),您需要以確保InitializeComponent()中的設計者生成的代碼被調用。

讓您的新的構造通過: this()首先調用默認的構造函數:

public ReturnDate(String lblCaption) : this() 
{ 
    _lblCaption = lblCaption; 
} 
相關問題