Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello guys,

I wish that when I remove the selected item in listView1 that then this same item be removed in a txt.File (at the same time).
Something like the code below, but this code does not work. Please Help. Thank you.
(App, target framework: NETFramework 3.5)


What I have tried:

private void remove()
        {
            string filedata = File.ReadAllText(@"D:\Test.txt");
            string replacedata = "";
            DialogResult result = MessageBox.Show("Are you sure to delete?", "Confirm Delete", MessageBoxButtons.YesNo);

            if (result.Equals(DialogResult.Yes))
            {
                for (int i = 0; i < listView1.Items.Count; i++)
                {
                    if (listView1.Items[i].Selected)
                    {

                        replacedata = filedata.Replace(listView1.SelectedItems[0] + Environment.NewLine, "");
                        listView1.Items[i].Remove();
                        i--;
                    }
                }
                File.WriteAllText(@"D:\Test.txt", replacedata);
            }
            else if (result == DialogResult.No)
            {
                return;
            }
        }
Posted
Updated 23-Feb-17 11:50am
Comments
Graeme_Grant 23-Feb-17 16:36pm    
Wouldn't it be better to let the user delete one or more items, then when they're happy click a save changes button and then export the list back to the text file? This way if the user makes a mistake they can click an undo or cancel button to avoid losing the information forever....
Member 10410972 23-Feb-17 16:57pm    
I agree with you.
But in this case I need only what I put in my question. Thank you.
[no name] 23-Feb-17 17:12pm    
"but this code does not work", then what is the problem. We have no idea what "does not work" means to you.
"Please Help", with what?
Graeme_Grant 23-Feb-17 17:24pm    
Please explain what does not work. If an error is thrown, what is the error that you are seeing? Which line is the error occurring on?

If I had to take a stab at the problem, without seeing the rest of the relevant code, my guess is that you are seeing an ArgumentOutOfRangeException error. The rest of the error details would say something like:
Quote:
Additional information: InvalidArgument=Value of '0' is not valid for 'index'.


In WinForms, the Listview.SelectedIndexChanged event will fire twice when changing selection. Once to say that an item is not selected, then again to say that an item was selected. Unless you are looking for this, the above error will occur.

Without more information I/we can't help you any further.
 
Share this answer
 
Hello,

It is a very simple bug... :) You are not accessing the Text property of the SelectedItem, therefore the replacement fails to produce the expected results.

you should write:
replacedata = filedata.Replace(listView1.SelectedItems[0].Text + Environment.NewLine, "");


NOT
replacedata = filedata.Replace(listView1.SelectedItems[0] + Environment.NewLine, "");


that's it.

Thanks,
Valery.
 
Share this answer
 
Comments
Graeme_Grant 23-Feb-17 18:07pm    
This would be an easy spot if a breakpoint was used...
Valery Possoz 23-Feb-17 18:59pm    
Yep! exactly...
Member 10410972 24-Feb-17 2:27am    
Thanks Valery.
but this code is deleted only item in listView1, but not deleted the same item in txt.File, and I want that when i deleted an item in listView1 to simultaneously delete the same item in txt.File. Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900