简体   繁体   English

为什么列表框行中的单词不仅删除了1行,而且删除了2行?

[英]Why word in listbox line removed not just one line but 2 lines?

I launch the program, add some words to a listbox and try to remove it. 我启动该程序,在列表框中添加一些单词,然后尝试将其删除。 If the selected item is last one, then it works fine, but if I select a word in the middle, then it removes the selected word. 如果所选项目是最后一项,那么它可以正常工作,但是如果我在中间选择一个单词,则它将删除所选单词。 Then I loaded listbox again, and the last word disappeared somewhere. 然后我再次加载列表框,最后一个单词消失在某处。 Any solutions? 有什么办法吗?

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

    }

Since words in your code is a global variable to all of your functions what happen is you are trying to remove items twice when you call this: 由于代码中的words是所有函数的全局变量,因此发生这种情况是您在调用此代码时试图两次删除项目:

words.RemoveAt and itemAll.Remove words.RemoveAtitemAll.Remove

To prevent it, instead of calling Remove() method you can rename it as UpdateListBox() passing the words as argument like: 为了防止出现这种情况,您可以将其作为参数传递,而不是调用Remove()方法,而将其重命名为UpdateListBox() ,例如:

private void UpdateListBox(List<string> words)
{
    string contents = null;
    List<string> itemAll = new List<string>();
    foreach (string str in words)
    {
        itemAll.Add(str);
    }
}

Then you will have a separate method to save back: 然后,您将有一个单独的方法来保存:

private void Save(List<string> words)
{
    StringBuilder contents = new StringBuilder();
    foreach (string s in words)
    {
        contents.AppendLine(s);
    }
    System.IO.File.WriteAllText(@"Data.txt", contents);
}

Then you will call it like: 然后,您将这样称呼它:

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

I just created it here and haven't tested it on my IDE, but you may have an idea how it is working. 我只是在这里创建它的,还没有在我的IDE上进行过测试,但是您可能对它的工作方式有所了解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM