简体   繁体   English

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

[英]Remove listbox items when tab is changed

I created a form with three tabs. 我创建了一个包含三个选项卡的表单 The button below is under one tab and when clicked fills the ListBox readBox with items from a text file. 下面的按钮位于一个选项卡下,单击时会使用文本文件中的项填充ListBox readBox。 What I cannot figure out how to do is remove the items from the ListBox` when I switch to another tab. 当我切换到另一个选项卡时,我无法弄清楚该怎么做是从ListBox`中删除项目。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks. 谢谢。

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

Use readBox.Items.Clear() , to remove all items (=> see MSDN) from your ListBox. 使用readBox.Items.Clear()从ListBox中删除所有项目(=>请参阅MSDN) If you want to do this on tab changing, pick up a SelectedIndexChanged . 如果要在更改选项卡上执行此操作,请选择SelectedIndexChanged

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

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