C# 为什么列表框中的单词不只是删除了一行,而是删除了两行?
我启动程序,在列表框中添加一些单词,然后尝试删除它。如果选中的项目是最后一个,那么它工作得很好,但是如果我在中间选择一个单词,那么它将删除所选单词。然后我再次加载listbox,最后一个单词消失在某处。有什么解决办法吗C# 为什么列表框中的单词不只是删除了一行,而是删除了两行?,c#,C#,我启动程序,在列表框中添加一些单词,然后尝试删除它。如果选中的项目是最后一个,那么它工作得很好,但是如果我在中间选择一个单词,那么它将删除所选单词。然后我再次加载listbox,最后一个单词消失在某处。有什么解决办法吗 public List<string> words = new List<string>(); public Form2() { InitializeComponent(); Load();
public List<string> words = new List<string>();
public Form2()
{
InitializeComponent();
Load();
listBox1.DataSource = words;
}
public void Save()
{
const string sPath = "Data.txt";
System.IO.StreamWriter sw = new System.IO.StreamWriter(sPath);
foreach (string item in words)
{
sw.WriteLine(item);
}
sw.Close();
}
private void Load()
{
string line;
var file = new System.IO.StreamReader("Data.txt");
while ((line = file.ReadLine()) != null)
{
words.Add(line);
}
}
// This Part ???
private void Remove()
{
string contents = null;
List<string> itemAll = new List<string>();
foreach (string str in words)
{
itemAll.Add(str);
}
foreach (string lstitem in listBox1.SelectedItems)
{
itemAll.Remove(lstitem);
//File.AppendAllText(strFile + "Currently In.txt", strName + Environment.NewLine);
}
foreach (string s in itemAll)
{ contents += s + Environment.NewLine; }
System.IO.File.WriteAllText(@"Data.txt", contents);
}
private void button2_Click(object sender, EventArgs e)
{
// The Remove button was clicked.
int selectedIndex = listBox1.SelectedIndex;
try
{
// Remove the item in the List.
words.RemoveAt(selectedIndex);
}
catch
{
}
listBox1.DataSource = null;
listBox1.DataSource = words;
Remove();
}
public List words=new List();
公共表格2()
{
初始化组件();
加载();
listBox1.DataSource=单词;
}
公共作废保存()
{
常量字符串sPath=“Data.txt”;
System.IO.StreamWriter sw=新的System.IO.StreamWriter(sPath);
foreach(单词中的字符串项)
{
软件写入线(项目);
}
sw.Close();
}
专用空心荷载()
{
弦线;
var file=new System.IO.StreamReader(“Data.txt”);
而((line=file.ReadLine())!=null)
{
添加(行);
}
}
//这部分???
私有无效删除()
{
字符串内容=null;
List itemAll=新列表();
foreach(字串str)
{
itemAll.Add(str);
}
foreach(listBox1.SelectedItems中的字符串lstitem)
{
itemAll.移除(lstitem);
//File.AppendAllText(strFile+“当前在.txt中”,strName+Environment.NewLine);
}
foreach(itemAll中的字符串s)
{contents+=s+Environment.NewLine;}
System.IO.File.WriteAllText(@“Data.txt”,contents);
}
私有无效按钮2\u单击(对象发送者,事件参数e)
{
//已单击删除按钮。
int selectedIndex=列表框1.selectedIndex;
尝试
{
//删除列表中的项目。
words.RemoveAt(selectedIndex);
}
抓住
{
}
listBox1.DataSource=null;
listBox1.DataSource=单词;
删除();
}
由于代码中的单词对所有函数来说都是全局变量,因此调用此函数时会出现两次试图删除项的情况:
words.RemoveAt
和itemAll.Remove
为了防止出现这种情况,您可以将其重命名为UpdateListBox()
而不是调用Remove()
方法,将单词作为参数传递,如下所示:
private void UpdateListBox(List<string> words)
{
string contents = null;
List<string> itemAll = new List<string>();
foreach (string str in words)
{
itemAll.Add(str);
}
}
我只是在这里创建了它,还没有在我的IDE上测试过它,但您可能已经知道它是如何工作的。因为代码中的单词是所有函数的全局变量,所以调用此函数时会发生两次尝试删除项的情况:
words.RemoveAt
和itemAll.Remove
为了防止出现这种情况,您可以将其重命名为UpdateListBox()
而不是调用Remove()
方法,将单词作为参数传递,如下所示:
private void UpdateListBox(List<string> words)
{
string contents = null;
List<string> itemAll = new List<string>();
foreach (string str in words)
{
itemAll.Add(str);
}
}
我只是在这里创建了它,还没有在我的IDE上测试它,但是你可能知道它是如何工作的。我在void save(words)-CS1503参数2中遇到错误:无法从'System.Text.StringBuilder'转换为'string',我注意到我使用了你的代码,但是我的save();代码工作正常,但不要从列表框中删除word,要查看更改,我必须重新启动该表单。但是在.txt文件中,它应该被更改为contents
到contents.ToString()
,看看它是否有效。我自己找到了解决方案,我只使用了Save()代码>然后在<代码>保存()后按下按钮代码>我刚刚把listBox1.DataSource=null代码>listBox1.DataSource=words代码>工作正常我在void save(words)-CS1503参数2中出错:无法从'System.Text.StringBuilder'转换为'string',我注意到我使用了你的代码,但是我的save();代码工作正常,但不要从列表框中删除word,要查看更改,我必须重新启动该表单。但是在.txt文件中,它应该被更改为contents
到contents.ToString()
,看看它是否有效。我自己找到了解决方案,我只使用了Save()代码>然后在<代码>保存()后按下按钮代码>我刚刚把listBox1.DataSource=null代码>listBox1.DataSource=words代码>并且它工作得很好
private void button2_Click(object sender, EventArgs e)
{
// The Remove button was clicked.
int selectedIndex = listBox1.SelectedIndex;
try
{
// Remove the item in the List.
words.RemoveAt(selectedIndex); //Remove item from words
UpdateListBox(words); //Update contents on GUI
Save(words); //Save on IO
}
catch
{
}
}