C# Listview所选项目值-非索引
我正在做一个供个人使用的项目(简单电话簿): 我有一个充满联系人的列表视图,在我添加这行代码之前,一切都很好地工作,当一切都变得一团糟时C# Listview所选项目值-非索引,c#,listview,selecteditem,listviewitem,C#,Listview,Selecteditem,Listviewitem,我正在做一个供个人使用的项目(简单电话簿): 我有一个充满联系人的列表视图,在我添加这行代码之前,一切都很好地工作,当一切都变得一团糟时 listView1.Sorting = SortOrder.Ascending; 因此,问题是显而易见的。假设列表中有6个联系人,联系人1住在城市1,在地址1上,有一个电话号码1[etc],而联系人2住在城市2,在地址2上,有一个电话号码2等等。[顺序继续…] 当我尝试进行一些更改时,例如,联系人5突然获得其他联系人的信息,在本例中,联系人7。不管它得到谁的
listView1.Sorting = SortOrder.Ascending;
因此,问题是显而易见的。假设列表中有6个联系人,联系人1住在城市1,在地址1上,有一个电话号码1[etc],而联系人2住在城市2,在地址2上,有一个电话号码2等等。[顺序继续…]
当我尝试进行一些更改时,例如,联系人5突然获得其他联系人的信息,在本例中,联系人7。不管它得到谁的信息,问题是一切都变得一团糟
此外,如果我想从listview中删除所有联系人-这是不可能的-将始终保留一个联系人。例如,如果其中有6个,5个将被删除,剩下一个。此外,如果有100个,它将删除99个,而一个也将始终保留
我发现不再使用所选项目的索引,我现在必须使用所选项目的值(而不是其索引)。但是,问题是我不知道怎么做
可能是listView1\u SelectedIndexChanged。请注意,我只是在猜测,我不完全确定
如果是,代码如下:
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count == 0) return;
textBox1.Text = people[listView1.SelectedItems[0].Index].Name;
textBox2.Text = people[listView1.SelectedItems[0].Index].Hometown;
textBox3.Text = people[listView1.SelectedItems[0].Index].Address;
textBox4.Text = people[listView1.SelectedItems[0].Index].Phone;
textBox5.Text = people[listView1.SelectedItems[0].Index].Email;
textBox6.Text = people[listView1.SelectedItems[0].Index].AdditionalInfo;
dateTimePicker1.Value = people[listView1.SelectedItems[0].Index].Birthday;
textBox1.ReadOnly = true;
textBox2.ReadOnly = true;
textBox3.ReadOnly = true;
textBox4.ReadOnly = true;
textBox5.ReadOnly = true;
textBox6.ReadOnly = true;
dateTimePicker1.Enabled = false;
toolStripButton5.Enabled = true;
}
我认为上传到这里的代码太大了,所以:
有人有解决办法吗 为了找到要更新的正确对象,您首先需要在
人员
集合中找到与列表视图
中选择的对象匹配的对象
Person i = People.FirstOrDefault(p => p.Name == ((ListView) sender).SelectedItems[0].Text);
PopulateEditData(i); // refer below for method...
只有当MultiSelect
属性设置为false时,这才有效,否则需要从所选项目集合中获取正确的项目
如图所示:
拥有正确的人物
对象后,您将能够检索并在文本框中显示对象详细信息:
private void PopulateEditData(Person selectedPerson)
{
textBox1.Text = selectedPerson.Name;
textBox2.Text = selectedPerson.Hometown;
textBox3.Text = selectedPerson.Address;
textBox4.Text = selectedPerson.Phone;
textBox5.Text = selectedPerson.Email;
textBox6.Text = selectedPerson.AdditionalInfo;
dateTimePicker1.Value = selectedPerson.Birthday;
textBox1.ReadOnly = true;
textBox2.ReadOnly = true;
textBox3.ReadOnly = true;
textBox4.ReadOnly = true;
textBox5.ReadOnly = true;
textBox6.ReadOnly = true;
dateTimePicker1.Enabled = false;
toolStripButton5.Enabled = true;
}
我还建议将selectPerson
设置为窗体的属性或可用的类之一,以便编辑和保存对象上的数据更容易一些
对于问题的删除部分,请使用要删除项目的SelectedPerson
属性
private void button1_Click(object sender, EventArgs e)
{
if (SelectedPerson != null)
{
People.Remove(SelectedPerson);
this.listView1.Items.Clear();
foreach (var person in People)
{
this.listView1.Items.Add(person.ToString());
this.listView1.Sorting = SortOrder.Ascending;
}
this.listView1.Refresh();
this.button1.Enabled = false;
}
}
谢谢你的回答 不管怎样,我已经通过以下方式解决了这个问题: 1) 我补充说:
private Person FindPerson(string name)
{
return people.Find(x => x.Name == name);
}
2) 我已取代:
people[listView1.SelectedItems[0].Index]
带“人”,其中人是:
Person person = FindPerson(listView1.SelectedItems[0].Text);
private Person FindPerson(string name)
{
return people.Find(x => x.Name == name);
}
其中FindPerson是:
Person person = FindPerson(listView1.SelectedItems[0].Text);
private Person FindPerson(string name)
{
return people.Find(x => x.Name == name);
}
不管怎样,当我试图删除最后一个联系人时,总是会出错。即使我尝试选择它,也不只是删除它。在这里,您可以查看错误是关于什么的:
图像:
这就是现在的情况:
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count == 0) return;
Person person = FindPerson(listView1.SelectedItems[0].Text);
textBox1.Text = person.Name;
textBox2.Text = person.Hometown;
textBox3.Text = person.Address;
textBox4.Text = person.Phone;
textBox5.Text = person.Email;
textBox6.Text = person.AdditionalInfo;
dateTimePicker1.Value = person.Birthday;
textBox1.ReadOnly = true;
textBox2.ReadOnly = true;
textBox3.ReadOnly = true;
textBox4.ReadOnly = true;
textBox5.ReadOnly = true;
textBox6.ReadOnly = true;
dateTimePicker1.Enabled = false;
toolStripButton5.Enabled = true;
}
和保存按钮:
private void toolStripButton1_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
Person person = FindPerson(listView1.SelectedItems[0].Text);
person.Name = textBox1.Text;
person.Hometown = textBox2.Text;
person.Address = textBox3.Text;
person.Phone = textBox4.Text;
person.Email = textBox5.Text;
person.Birthday = dateTimePicker1.Value;
person.AdditionalInfo = textBox6.Text;
listView1.SelectedItems[0].Text = textBox1.Text;
textBox1.ReadOnly = true;
textBox2.ReadOnly = true;
textBox3.ReadOnly = true;
textBox4.ReadOnly = true;
textBox5.ReadOnly = true;
textBox6.ReadOnly = true;
dateTimePicker1.Enabled = false;
}
else
{
MessageBox.Show("Nothing is selected ", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
UserCount();
}
函数返回null,因为未选择任何项。所以这个人当然是空的。您应该在FindPerson函数中处理此问题