0

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

当我在 listbox 上的选定项目 [0] 之后没有获得索引选项时。请帮我。谢谢各位

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

1 回答 1

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;
    }
}
于 2013-03-02T11:42:28.120 回答