2016-04-03 51 views

回答

3

使用「編輯器」(強制使用瀏覽[...]按鈕選擇文件夾)屬性來指定該財產的編輯。確保你的項目中有一個對System.Design.dll的引用。

public partial class UserControl1 : UserControl 
{ 
    public UserControl1() 
    { 
     InitializeComponent(); 
    } 

    [Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor))] 
    public string SelectedPath 
    { 
     get; 
     set; 
    } 
} 
+0

不錯的工作。謝謝。現在對於路徑本身,你知道我是否可以使用'string'以外的字符串,因爲'字符串'在設置像UserControl1.SelectedPath =「foo」時更容易出現用戶錯誤' –

+0

我不認爲FolderNameEditor可以工作與不同類型的對象。我建議你使用SelectedPath的setter來驗證傳入的字符串是否與路徑中的任何規則相符,例如根據Path.GetInvalidPathChars()檢查字符,使用File.Exists(),與正則表達式匹配,等等。 – ma7r

相關問題