2016-04-03 53 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

相关问题