To Delete a Specified Part of a Text File

Asked 2 years ago, Updated 2 years ago, 35 views

I am currently using .NET Framework 4.5 to create a Windows form application.
It reads the specified text file, displays it in the list box line by line, selects the displayed item (multiple choices allowed), and then presses the button to delete the selected item from the text file displayed in the list box.

I don't think it's implemented to remove the specified part directly from the text file, so I thought I'd read the text file once, output the data except for the items specified in the list box, and overwrite the output file with the first text file.

First, I was able to read the text file, print the data directly to another file, and overwrite the original file.
However, I still don't know how to look at the selected items in the list box and print them out.

This is part of the code written below.

StreamReader cReader=(new StreamReader(@"C:\", Encoding.GetEncoding("shift_jis"));
stringResult = string.Empty;
string tempFile=@ "C:/temp.file"
string removeTxt = listBox2.SelectedItems.ToString();
while(cReader.Peek()>=0)
{
     US>string stBuffer=cReader.ReadLine();
     StreamWriter writer = new StreamWriter(tempFile, false, Encoding.GetEncoding("shift-jis"));
     writer.WriteLine(stBuffer);
     writer.Close();
     stResult+=stBuffer+Environment.NewLine;<br>
}
cReader.Close();
File.Delete(tempFile);

The above code is a set of code that excludes the action of deleting the selected part of the list box.

 if(cReader.ReadLine()!=listBox2.SelectedItems.ToString())

I tried to write to tempFile when it didn't match the selected item, but it didn't work.

If there is any good way, please let me know.

*The above code is not a multiple choice item, but a single choice item is excluded from the text file, but I am thinking of working on multiple choice items after this question is solved.

The .NET version is 4.5

c#

2022-09-30 20:30

3 Answers

Assume Forms, but since SelectedItems are of the type SelectedObjectCollection, ToString does not result in a string. You may need to access it with SelectedItems[0] or SelectedItem.


2022-09-30 20:30

Assuming a form, assume a ListBox control named listBox1 is deployed.
Save with some button click event.

private void button1_Click(object sender1, EventArgse1){
    var buf = new StringBuilder();
    for(inti=0;i<this.listBox1.Items.Count;i++){
        if (this.listBox1.SelectedIndices.Contains(i))
            continue;
        buf.AppendLine(this.listBox1.Items[i]as string);
    }
    File.WriteAllText(@"c:\test.txt", buf.ToString();
}

Note that the type of item in the list box is string.
when using a different type This.listBox1.Items[i]as string in the code below returns null.
If you implement your own type, override the ToString method. Call this.listBox1.Items[i].ToStirng().


2022-09-30 20:30

The Selected Items in ListBox are a collection (like an array) to access all selected items rather than a single selection.

For MSDN,

Retrieves a collection containing the currently selected items in ListBox.

is described as

ToString returns the string System.Windows.Forms.ListBox+SelectedObjectCollection.
(This is a text representation of the type of SelectedItems.Classes that cannot be represented as characters return these values.)

To access what is actually selected, you must retrieve the SelectedItems element.

Rewrite the if statement presented in the question to the if statement to compare with the first item when the item is selected.

 if(listBox2.SelectedItems.Count>0) {// At least one is selected
     if(cReader.ReadLine()!=(string)listBox2.SelectedItems[0])//Compared to the first selected one

ListBox also has the contents of a pre-loaded text file, so if you trust it (if you can say that the file hasn't been rewritten after that), you can print it out. This is already possible in PITA's way, but I will include a method of determination other than SelectedIndices.

ListBox has a method called GetSelected(int), which allows you to determine if an item with a specified index is selected.

This allows you to loop while determining if an item has been selected.

for(vari=0;i<listBox2.Items.Count;i++){
    if(listBox2.GetSelected(i)){
        // listBox2.Items[i] is selected
    }
    else{
        // listBox2.Items[i] is not selected
    }
}


2022-09-30 20:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.