标签或文本框上的列表框索引显示
问题描述:
是否有人知道如何使用Listbox SelectedIndexChanged事件的以下命令。请告诉我。我从互联网拿这个例子,我只是想知道如何使用ListBox而不是ListView。标签或文本框上的列表框索引显示
当我没有得到列表框上选定的项目[0]后的索引选项。请帮帮我。 Thankx guys
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = people[listView1.SelectedItems[0].Index].Name;
}
}
答
也许不完全准确,但工作。
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-02 13:00:04
如果您喜欢我的回答,请将其标记为已接受。 Thx http://stackoverflow.com/about – 2013-03-02 15:26:28