当选项卡更改时,删除列表框项

本文关键字:删除列 删除 列表 选项 | 更新日期: 2023-09-27 18:15:16

我创建了一个有三个选项卡的表单。下面的按钮位于一个选项卡下,当单击时,用文本文件中的项目填充ListBox readBox。当我切换到另一个选项卡时,我不知道如何从列表框中删除项目。任何帮助都会很感激。谢谢。

    private void read_Click(object sender, EventArgs e)
    {
        FileStream file = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite);
        StreamReader read = new StreamReader(file);
        string readIt;
        string[] show;
        string birthday;
        readIt = read.ReadLine();
        while (readIt != null)
        {
            show = readIt.Split(DELIM);
            friend.FirstName = show[0];
            friend.LastName = show[1];
            friend.PhoneNumber = show[2];
            birthday = show[3] + "/" + show[4];
            readIt = String.Format("{0, -10}{1, -10}{2,-10}{3, -3}",  friend.FirstName,
            friend.LastName, friend.PhoneNumber, birthday);
            readBox.Items.Add(readIt);
            readIt = read.ReadLine();
        }
        read.Close();
        file.Close();

    }

当选项卡更改时,删除列表框项

捕获TabControl.SelectedIndexChanged事件,并在处理程序中调用readBox.Items.Clear()

使用readBox.Items.Clear(),从您的ListBox中删除所有项目(=>参见MSDN)。如果你想改变制表符,选择一个SelectedIndexChanged .