C# 删除列表框项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18968885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Removing listbox item
提问by grimsan55
Im at beginner level and have tried for a while now. Im trying to remove an item from a ListBox
with the help of a remove button. The code is not giving away any errors but the items is not disappearing from the list.
This is the part im struggling with
我处于初学者水平,现在已经尝试了一段时间。我试图在ListBox
删除按钮的帮助下从 a 中删除一个项目。代码不会泄露任何错误,但项目不会从列表中消失。这是我挣扎的部分
void taBort()
{
listboxKontakter.SelectedItems.Remove(listboxKontakter.SelectedItems);
textboxAnteckningar.Clear();
textboxGatuadress.Clear();
textboxNamn.Clear();
textboxPostnummerOrt.Clear();
textboxEmail.Clear();
textboxF?delsedag.Value = DateTime.Now;
}
Here is my entire code:
这是我的整个代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace adressboken
{
public partial class Form1 : Form
{
List<Kontakter> kontaktLista = new List<Kontakter>();
Kontakter person;
string path = "kontakter.txt";
public Form1()
{
InitializeComponent();
}
private void L?ggTill_Click(object sender, EventArgs e)
{
person = new Kontakter();
person.Fullst?ndigtNamn = textboxNamn.Text;
person.Gatuadress = textboxGatuadress.Text;
person.PostnummerOrt = textboxPostnummerOrt.Text;
person.F?delsedag = textboxF?delsedag.Value;
person.Email = textboxEmail.Text;
person.Anteckningar = textboxAnteckningar.Text;
kontaktLista.Add(person);
listboxKontakter.DataSource = null;
listboxKontakter.DisplayMember = "Fullst?ndigtNamn";
listboxKontakter.DataSource = kontaktLista;
textboxAnteckningar.Clear();
textboxGatuadress.Clear();
textboxNamn.Clear();
textboxPostnummerOrt.Clear();
textboxEmail.Clear();
textboxF?delsedag.Value = DateTime.Now;
textboxAntal.Text = kontaktLista.Count.ToString();
}
private void Rensa_Click(object sender, EventArgs e)
{
textboxAnteckningar.Clear();
textboxGatuadress.Clear();
textboxNamn.Clear();
textboxPostnummerOrt.Clear();
textboxEmail.Clear();
textboxF?delsedag.Value = DateTime.Now;
}
void taBort()
{
textboxAnteckningar.Clear();
textboxGatuadress.Clear();
textboxNamn.Clear();
textboxPostnummerOrt.Clear();
textboxEmail.Clear();
textboxF?delsedag.Value = DateTime.Now;
}
private void Form1_Load(object sender, EventArgs e)
{
kontaktLista = new List<Kontakter>();
string line = "";
StreamReader sr = new StreamReader(path);
while ((line = sr.ReadLine()) != null)
{
string[] listarray = line.Split(',');
person = new Kontakter();
person.Fullst?ndigtNamn = listarray[0];
person.Gatuadress = listarray[1];
person.PostnummerOrt = listarray[2];
person.Email = listarray[3];
person.F?delsedag = Convert.ToDateTime(listarray[4]);
person.Anteckningar = listarray[5];
kontaktLista.Add(person);
}
sr.Close();
listboxKontakter.DataSource = kontaktLista;
listboxKontakter.DisplayMember = "Fullst?ndigtNamn";
}
public void listboxKontakter_Click(object sender, EventArgs e)
{
person = (Kontakter)listboxKontakter.SelectedItem;
textboxNamn.Text = person.Fullst?ndigtNamn;
textboxGatuadress.Text = person.Gatuadress;
textboxPostnummerOrt.Text = person.PostnummerOrt;
textboxEmail.Text = person.Email;
textboxF?delsedag.Value = person.F?delsedag;
var selectedindex = listboxKontakter.SelectedItems;
}
private void Spara_Click(object sender, EventArgs e)
{
StreamWriter sw = new StreamWriter(path);
foreach (Kontakter k in kontaktLista)
{
sw.WriteLine(k.FullInfo);
}
sw.Close();
}
private void taBortToolStripMenuItem_Click(object sender, EventArgs e)
{
taBort();
}
private void TaBort_Click(object sender, EventArgs e)
{
taBort();
}
}
}
采纳答案by paqogomez
Your code seems to remove all selected items, and does not refresh.
您的代码似乎删除了所有选定的项目,并且不会刷新。
How about:
怎么样:
listboxKontakter.SelectedItems.Remove(listboxKontakter.SelectedItem);
listboxKontakter.Refresh();
You remove only the single selected item, then refresh your listbox.
您只删除单个选定的项目,然后刷新您的列表框。
If you want to remove ALLitems try:
如果要删除所有项目,请尝试:
listboxKontakter.Items.Clear();
If you use a DataSourcetry:
如果您使用数据源,请尝试:
listboxKontakter.DataSource = null;
If all else fails you could loop through the collection and RemoveAt
:
如果所有其他方法都失败了,您可以遍历集合并RemoveAt
:
for(int i=listboxKontakter.Items.Count; i > -1; i--) {
{
listboxKontakter.Items.RemoveAt(i);
}
Based on a bit of chatting, this should work for you:
基于一些聊天,这应该适合你:
void taBort()
{
var newList = (List<Kontakter>)listboxKontakter.DataSource;
var ds = newList.Where(k => k.Fullst?ndigtNamn != ((Kontakter)listboxKontakter.SelectedItem).Fullst?ndigtNamn).ToList();
listboxKontakter.DataSource = ds;
listboxKontakter.DisplayMember = "Fullst?ndigtNamn";
textboxAnteckningar.Clear();
textboxGatuadress.Clear();
textboxNamn.Clear();
textboxPostnummerOrt.Clear();
textboxEmail.Clear();
textboxF?delsedag.Value = DateTime.Now;
}
If you want to remove several items at once try:
如果您想一次删除多个项目,请尝试:
var ds = newList.Where(k => !listboxKontakter.SelectedItems.Contains(k.Fullst?ndigtNamn)).ToList();
回答by Loko
How about:
怎么样:
listboxKontakter.Items.Remove(itemthatneedstoberemoved)
and
和
listboxKontakter.Items.Clear();
(I assume you called the listbox, listboxKontakter?)
(我假设您将列表框称为 listboxKontakter?)
:
:
回答by Josh
.SelectedItems is basically just an array list of what items you have selected, so you will need to access those like this .SelectedItems[0] .SelectedItems[1].
.SelectedItems 基本上只是您选择的项目的数组列表,因此您需要访问像这样的 .SelectedItems[0] .SelectedItems[1]。
However the above code even with the [0], [1] will only remove them from the selected list not the actual list box. If you want to remove them from the list box you need to use the .Items.Remove call.
然而,上面的代码即使使用 [0], [1] 也只会将它们从选定的列表中删除,而不是实际的列表框。如果您想从列表框中删除它们,您需要使用 .Items.Remove 调用。
while(listboxKontakter.SelectedItems.Count >0)
{
listboxKontakter.Items.Remove(listboxKontakter.SelectedItems[0]);
}
EDIT:If it is a single select listbox all you have to do is
编辑:如果它是一个单选列表框,你所要做的就是
listboxKontakter.Items.Remove(listboxKontakter.SelectedItem);