显示列表
 ComboBox 控件2008-06-29

ComboBox 是一个下拉列表框。

DataSource
获取或设置此 ComboBox 的数据源。

DisplayMember
获取或设置要为此 ListControl 显示的属性。(从 ListControl 继承。)

ValueMember
获取或设置一个属性,该属性将用作 ListControl 中的项的实际值。(从 ListControl 继承。)

SelectedIndex
获取或设置指定当前选定项的索引。


SelectedText
获取或设置 ComboBox 的可编辑部分中选定的文本。


SelectedItem
获取或设置 ComboBox 中当前选定的项。

ArrayList list = new ArrayList();
list.Add(new Info());
list.Add(new Info());
comboBox1.DataSource = list;
comboBox1.DisplayMember = "Name";

public class Info
{
    private string name = string.Empty;
    public string Name
   {
        get{return name;}
        set{name = value;}
   }
}


comboBox1.ValueMember 好象没用
实现的方法

public class ListItem
{
    private string key;
    private string value;

    public ListItem(string key, string value)
    {
        this.key = key;
        this.value = value;
    }

    public string Key
    {
        get { return this.key; }
        set { this.key = value; }
    }

    public string Value
    {
        get { return this.value; }
        set { this.value = value; }
    }

    // 关键点,重写方法
    public override string ToString()
    {
        return this.value;
    }
}



private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    // 返回 ListItem.Value 的值
    textBoxLayout.Text = comboBox1.SelectedItem.ToString();
}

comboBox1.Items.Add(new ListItem("AAA","1"));
comboBox1.Items.Add(new ListItem("BBB", "2"));
comboBox1.DisplayMember = "Key";

标签:控件 
返回摘要 | 分类(C#/CSharp) | 访问(466) | 编辑