C# 如何在列表框中选择索引

C# 如何在列表框中选择索引,c#,winforms,listbox,propertygrid,typeconverter,C#,Winforms,Listbox,Propertygrid,Typeconverter,我使用类型转换器将字符串数组显示为属性网格中的列表框,代码如下。 如何选择列表框的索引以初始化第一次加载的程序?我想要SelectedIndex=0,show first arrData=“one”。有什么想法吗 public partial class Form1 : Form { string[] arrData = new string[] { "one", "two", "three" }; PropertyGrid pGrid = new PropertyGrid();

我使用类型转换器将字符串数组显示为属性网格中的列表框,代码如下。
如何选择列表框的索引以初始化第一次加载的程序?我想要SelectedIndex=0,show first 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);
            }
        }

    }

}

如果我理解正确,那么如果您只想将列表框中所选项目的索引设置为0,应该像下面的代码一样简单

ListBox.SelectedIndex = 0;
有关更多详细信息,请查看MSDN页面

您在列表框中是对的。SelectedIndex=0;如果使用ListBox控件,则设置ListBox的索引,但问题是计数器属性使用了字符串类型,而不是ListBox。