简体   繁体   中英

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. What I cannot figure out how to do is remove the items from the ListBox` when I switch to another tab. 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. If you want to do this on tab changing, pick up a SelectedIndexChanged .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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