1

如果我使用了错误的术语,我提前道歉,请随时要求我澄清这个问题是否有任何令人困惑的地方

我正在尝试用 C# 编写一个程序,它做 3 件事:将 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中所有内容的链接

4

2 回答 2

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
于 2013-07-28T18:39:33.000 回答
1

使用File.ReadAllLines并将File.WriteAllLines让您的生活更轻松。在您的阅读方法中,File.ReadAllLines应该使这一点变得简单。然后在您的删除方法中,而不是写入文件然后更新列表框,我会以相反的顺序进行。首先从列表框 ( ) 中删除项目listBox1.Items.Remove(listBox1.SelectedItem);,然后将其余部分写入 file 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);
    }
于 2013-07-28T18:55:33.090 回答