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

时间:2013-05-24 14:07:20

标签: c# winforms listbox tabcontrol streamreader

我创建了一个包含三个标签的表单。下面的按钮位于一个选项卡下,单击时会使用文本文件中的项填充ListBox readBox。当我切换到另一个选项卡时,我无法弄清楚该怎么做是从ListBox`中删除项目。任何帮助,将不胜感激。感谢。

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


    }

2 个答案:

答案 0 :(得分:2)

抓住TabControl.SelectedIndexChanged事件,并在处理程序调用{​​{1}}

答案 1 :(得分:0)

使用readBox.Items.Clear(),从列表框中删除所有项目(=> see MSDN)。 如果您想在标签更改时执行此操作,请选择SelectedIndexChanged