如何编辑或删除列表框中的记录

时间:2013-07-28 18:24:01

标签: c# visual-studio

如果我使用错误的术语,请提前道歉,请随意请我澄清这个问题是否有任何混淆

我正在尝试用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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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);
    }