C#在属性内声明OpenFileDialog
我将要创建一个自定义控件,并希望在属性中使用OpenFileDialog。它工作正常,但每次我编译项目时,对话框都会打开,但每当我更改属性时,我都想打开它,下面是我正在使用的代码:C#在属性内声明OpenFileDialog,c#,properties,dialog,openfiledialog,C#,Properties,Dialog,Openfiledialog,我将要创建一个自定义控件,并希望在属性中使用OpenFileDialog。它工作正常,但每次我编译项目时,对话框都会打开,但每当我更改属性时,我都想打开它,下面是我正在使用的代码: private string _customproperty; public string stringpro { get { return _customproperty; }
private string _customproperty;
public string stringpro
{
get
{
return _customproperty;
}
set
{
switch (value)
{
case "1":
_customproperty = "1";
break;
case "2":
using (OpenFileDialog openFile = new OpenFileDialog())
{
if (openFile.ShowDialog() == DialogResult.OK)
{
_customproperty = IO.File.ReadAllText(openFile.FileName);
}
}
break;
}
Invalidate();
}
}
还是有更好的方法
提前谢谢。你有没有想过如果他们点击
取消会发生什么情况呢?
我不能使用esle
语句。在这种情况下,用户点击取消,我想避免在编译时执行此对话框。“或者有没有更好的方法”当然有。这是一个非常非常糟糕的主意。但是如果我们不知道你想要实现什么,就很难提出一个更好的解决方案。@Pawełukasik,我只是通过改变声明和使用的方式来解决它。你有没有想过如果他们点击Cancel
,会发生什么情况?我不能使用esle
语句,如果用户点击Cancel,我想避免在编译时执行此对话框。“或者有没有更好的方法”当然有。这是一个非常非常糟糕的主意。但是,如果我们不知道您想要实现什么,就很难提出更好的方案。@Pawełukasik,我只是通过改变声明和使用的方式来解决这个问题。