我使用类型转换器在属性网格中将字符串数组显示为列表框,代码如下。
如何选择列表框的索引以初始化第一次加载的程序?我想要 SelectedIndex = 0,首先显示 arrData = "one"。任何想法?
public partial class Form1 : Form
{
string[] arrData = new string[] { "one", "two", "three" };
PropertyGrid pGrid = new PropertyGrid();
public Form1()
{
InitializeComponent();
FirstClass fClass = new FirstClass(arrData);
pGrid.SelectedObject = fClass;
pGrid.Dock = DockStyle.Fill;
this.Controls.Add(pGrid);
}
public class FirstClass
{
public FirstClass(string[] arrData)
{
this.DataSource = arrData;
}
[Browsable(true)]
[DefaultValue("one")]
[TypeConverter(typeof(SecondClass))]
public string Counter { get; set; }
[Browsable(false)]
public string[] DataSource { get; set; }
}
public class SecondClass : StringConverter
{
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
FirstClass fClass = (FirstClass)context.Instance;
if (fClass == null || fClass.DataSource == null)
{
return new StandardValuesCollection(new string[0]);
}
else
{
return new StandardValuesCollection(fClass.DataSource);
}
}
}
}