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

本文关键字:列表 删除列 记录 删除 何编辑 编辑 | 更新日期: 2023-09-27 18:25:10

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

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

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

正如我所说,您的问题可能是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

使用File.ReadAllLinesFile.WriteAllLines会让你的生活更轻松。在您的读取方法中,File.ReadAllLines应该使其变得简单明了。然后在您的delete方法中,我会按照相反的顺序执行,而不是写入文件然后更新列表框。首先,只需从列表框中删除该项(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);
    }