2013-07-28 34 views
1

我的道歉,如果我使用了錯誤的術語,請下跌免費問我澄清如果有什麼似乎在這個問題困惑如何事先編輯或從列表框中刪除記錄

我想寫在C#,做三件事的程序: 加4個文本字段到列表框,編輯在列表框的一個記錄的任何或所有字段,刪除所有4個字段(在一個單一的記錄)

我存儲數據在一個文本文件「test1.txt」中使用StreamWriter編寫文件和StreamReader來讀取它。 香港專業教育學院設法添加記錄,但我沒有得到它刪除或編輯記錄 這是我的代碼如下所示:

string path = "test1.txt"; 
int index = -1;  
public Form1()  
{InitializeComponent();}  

private void Form1_Load(object sender, EventArgs e) 
    { 
     readFile();  
    } 

private void readFile() 
    {  
     displayEventsBox.Items.Clear(); 
     StreamReader sr = new StreamReader(path); 
     string record = sr.ReadLine(); 
      { 
       displayEventsBox.Items.Add(record);  
       record = sr.ReadLine();  
      }  
     sr.Close(); 
    } 


private void displayEventsBox_SelectedIndexChanged(object sender, EventArgs e) 
    {  
     index = displayEventsBox.SelectedIndex; 
     if (index > -1) 
      {  
       string record = displayEventsBox.Items[index].ToString();  
       char[] delim = { ',' };  
       string[] tokens = record.Split(delim);  
       txtTaskName.Text = tokens[0];  
       txtTaskDescription.Text = tokens[1];  
       txtDateCreated.Text = tokens[2];  
       txtDateCompleted.Text = tokens[3];  
      } 
    } 

private void butAddTask_Click(object sender, EventArgs e) 
    { 
     StreamWriter sw = new StreamWriter(path, true); 
     sw.WriteLine(txtTaskName.Text + "," + txtTaskDescription.Text + "," + txtDateCreated.Text + "," + txtDateCompleted.Text); 
     sw.Close(); 
     readFile(); 
     clearText();} 

private void butEditTask_Click(object sender, EventArgs e) 
    { 
     File.Delete(path);  
     StreamWriter sw = new StreamWriter(path, true);  
     for (int i = 0; i < displayEventsBox.Items.Count; i++)  
      {  
       if (i != index) 
        { 
         sw.WriteLine(displayEventsBox.Items[i].ToString()); 
        } 
       else 
        { 
         sw.WriteLine(txtTaskName.Text + "," + txtTaskDescription.Text + "," + txtDateCreated.Text + "," + txtDateCompleted.Text); 
        } 

      } 

    } 

private void butDeleteTask_Click(object sender, EventArgs e) 
    { 
     File.Delete(path); 
     StreamWriter sw = new StreamWriter(path, true);  
     for (int i = 0; i < displayEventsBox.Items.Count; i++)  
      { 
       if (i != index)// write from listbox  
        { 
         sw.WriteLine(displayEventsBox.Items[i].ToString()); 
        }  

      }  
       sw.Close();  
       readFile();  

    } 

可能有人請幫我找出爲什麼它不會讓我刪除或更新記錄

在情況下,它可以幫助澄清這裏是在代碼 http://collabedit.com/83rev

+0

林困惑,爲什麼你使用流無處不在,但eitherway ......你永遠*你 – Sayse

+0

張貼*錯誤沒有錯誤,它根本不會做任何事情。 我選擇的記錄,按刪除按鈕,它重置爲初始狀態 – Calsolum

+0

也是我使用流的唯一原因是因爲它是我學到的唯一方法,如果有一個更簡單的方法來做到這一點,相同的結果然後我我對任何建議都開放 – Calsolum

回答

1

使用File.ReadAllLinesFile.WriteAllLines會讓你的生活更輕鬆。在你的閱讀方法中,File.ReadAllLines應該很簡單。然後在你的刪除方法中,而不是寫入文件,然後更新列表框,我會按照相反的順序進行操作。首先從列表框中刪除該項目(listBox1.Items.Remove(listBox1.SelectedItem);),然後將其餘文件寫入文件File.WriteAllLines(filename, listBox1.Items.Cast<string>());。然後,由於您已經更新了列表框,所以最後不需要撥打readFile()。類似於添加和編輯。

此外,由於沒有其他功能正在調用readFile了,您可以直接將所有代碼直接放入Form1_Load。當表單關閉時,您也可以保存一次。

下面是固定後的功能。

private void Form1_Load(object sender, EventArgs e) { 
     displayEventsBox.Items.AddRange(File.ReadAllLines(path)); 
    } 

    string Format() { 
     return txtTaskName.Text + "," + txtTaskDescription.Text + "," + txtDateCreated.Text + "," + txtDateCompleted.Text; 
    } 

    private void butAddTask_Click(object sender, EventArgs e) { 
     displayEventsBox.Items.Add(Format()); 
    } 

    private void butDeleteTask_Click(object sender, EventArgs e) { 
     displayEventsBox.Items.RemoveAt(displayEventsBox.SelectedIndex); 
    } 

    private void butEditTask_Click(object sender, EventArgs e) { 
     displayEventsBox.Items[displayEventsBox.SelectedIndex] = Format(); 
    } 

    protected override void OnClosing(CancelEventArgs e) { 
     File.WriteAllLines(path, displayEventsBox.Items.Cast<string>()); 
     base.OnClosing(e); 
    } 
+0

完美地工作謝謝你的幫助 – Calsolum

+0

@Calsolum我更新了它,使它更加容易:在表單關閉之前,你實際上並不需要保存文件,所以最好將保存功能放在'OnClosing'處理程序中。這樣你就不必在每次編輯後都記得調用'Save'。它也「感覺」更加對稱:當你載入表單時讀取一次,關閉時寫入一次。 –

2

就像我說的一切的鏈接,你的問題可能是readfile永遠只能加載一個項目,嘗試

using(StreamReader sr = new StreamReader(path)) 
{ 
    string record; 
    while((record = sr.ReadLine()) != null) 
    { 
     displayEventsBox.Items.Add(record); 
    } 
}  

除此之外,你不應該關心一旦你加載一次就加載。而不是添加一個項目...

只需嘗試以下操作。

string s = txtTaskName.Text + "," + txtTaskDescription.Text + "," 
       + txtDateCreated.Text + "," + txtDateCompleted.Text; 
displayEventsBox.Items.Add(s); 

會有類似的方法進行編輯,刪除等

繼承人當前的方法的細分...

string record = sr.ReadLine();    //read a line 
{           //Open a block that doesn't do much 
    displayEventsBox.Items.Add(record); //Add the item 
    record = sr.ReadLine();    //Read the next item but ignore it 
}           //Close the random block 
+0

好吧,所以我改變了閱讀文件,並創建了一個新的添加文件和刪除文件的方法使用ADD和REMOVE,但我不知道如何編輯記錄,並有辦法讓文本框自動填寫任何選擇列表框 – Calsolum

+0

您可以創建一個更新記錄按鈕,您已經有一種方法可以根據所選索引更改文本框,只需在選中時使用新的文本框值更改該項目 – Sayse

+0

感謝您提供的所有幫助,尤其是解釋和分解我出錯的地方 – Calsolum