列表框索引显示在标签或文本框上

时间:2013-03-02 11:17:47

标签: c# listview listbox

是否有人知道如何使用以下命令与Listbox SelectedIndexChanged事件。请告诉我。我从互联网上拿了这个例子,我只想知道如何使用它与listbox而不是Listview。

当我在列表框上的所选项目[0]之后没有得到索引选项时。请帮我。 谢谢你们

 private void listView1_SelectedIndexChanged(object sender, EventArgs e)
  {
    textBox1.Text = people[listView1.SelectedItems[0].Index].Name;
  }
}

1 个答案:

答案 0 :(得分:0)

可能不完全准确但有效。

public partial class Form1 : Form
{
    List<People> people = new List<People>();

    public Form1()
    {
        InitializeComponent();
        people.Add(new People("Joe Montana"));
        people.Add(new People("Alex Smith"));
        people.Add(new People("Colin Kaepernick"));

        foreach (People p in people)
        {
            this.listBox1.Items.Add(p.Name);
        }
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.textBox1.Text = people[listBox1.SelectedIndices[0]].Name;
    }
}

class People
{
    public People(string Name)
    {
        this.Name = Name;
    }

    public string Name
    {
        get;
        set;
    }
}