【问题标题】:Listview selected item value - not IndexListview 选定项值 - 不是索引
【发布时间】:2013-11-18 23:54:50
【问题描述】:

我正在开发一个供个人使用的项目(简单的电话簿)。 This is how it looks like:

我有一个列表视图,里面装满了联系人,一切正常,直到我添加了这行代码,然后一切都变得一团糟。

listView1.Sorting = SortOrder.Ascending;

所以,问题很明显。假设列表中有 6 个联系人,联系人 1 住在 City 1,在 Address 1 上,有一个 电话.没有 1 [等],联系人 2 住在 City 2,在 Address 2,有一个 电话。没有 2 等[...序列继续...]

当我尝试进行一些更改时,例如,联系人 5 突然获取了其他联系人的信息,在本例中为 联系人 7。不管它得到谁的信息,问题是一切都变得一团糟。

另外,如果我想从列表视图中删除所有联系人 - 这是不可能的 - 总会有一个剩余的。例如,如果有 6 个,则删除 5 个,剩下 1 个。此外,如果有 100 个,它将删除 99 个,并且始终保留一个。

我发现不再使用所选项目的索引没有意义,我现在必须使用所选项目的值(而不是其索引)。但是,问题是我不知道该怎么做。

也许只涉及 listView1_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;
    }

我觉得代码太大了在这里上传,所以I uploaded the code here

有人有解决办法吗?

【问题讨论】:

    标签: c# listview selecteditem listviewitem


    【解决方案1】:

    为了找到要更新的正确对象,您首先需要在People 集合中找到与ListView 中选择的对象匹配的对象。

    Person i = People.FirstOrDefault(p => p.Name == ((ListView) sender).SelectedItems[0].Text);
    PopulateEditData(i); // refer below for method...
    

    这只有在您将MultiSelect 属性设置为 false 时才有效,否则您将需要从所选项目的集合中获取正确的项目。

    如此处所示:http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.selecteditems(v=vs.110).aspx

    一旦您拥有正确的Person 对象,您就可以在文本框中检索和显示对象详细信息:

    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;
            }
        }
    

    【讨论】:

      【解决方案2】:

      感谢您的回答。

      无论如何,我已经设法通过以下方式解决了这个问题:

      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);
      

      FindPerson 在哪里:

      private Person FindPerson(string name)
      {
          return people.Find(x => x.Name == name);
      }
      

      无论如何,当我尝试删除最后一个剩余联系人时,我一直收到错误消息。即使我尝试选择它,而不仅仅是删除它。在这里你可以看看错误是关于什么的:

      图片http://s24.postimg.org/ls4nak6et/Kruzeri.png

      所以,这就是现在的样子:

      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();
          }
      

      【讨论】:

        【解决方案3】:

        您的函数返回 null,因为没有选择任何项目。所以人当然是空的。您应该在 FindPerson-Function 中处理此问题。

        【讨论】:

          猜你喜欢
          • 2017-04-09
          • 2014-12-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多