[英]How to update ListBox when ObservableCollection items changed?
[英]Remove listbox items when tab is changed
我創建了一個包含三個選項卡的表單 下面的按鈕位於一個選項卡下,單擊時會使用文本文件中的項填充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();
}
捕獲TabControl.SelectedIndexChanged
事件,並在處理程序中調用readBox.Items.Clear()
使用readBox.Items.Clear()
從ListBox中刪除所有項目(=>請參閱MSDN) 。 如果要在更改選項卡上執行此操作,請選擇SelectedIndexChanged
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.