0

I made a listview that display names using a streamreader. When I select a name and delete it the name that was below in the list will then take the information of the deleted name when it's displayed in the textboxes which shows name, address and email. How can I delete a name from the list so that it's gets completely deleted and dont leave behind any information? Im thinking it has something to to with the streamwriter and streamreader.

2
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by csharp_user
0

Sounds like somewhere along the line you are assigning the information to a certain point in the listview...

post code if you can....

0
private void UppdateraLista()
        {
            listViewKunder.Items.Clear();
            for (int i = 0; i < lkunder.Count; i++)
            {
                listViewKunder.Items.Add(lkunder[i].Namn);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           
            //använd streamReader


            StreamReader sr = new StreamReader("Kunder.txt");
            string lineinfo = "";

            while ((lineinfo = sr.ReadLine()) != null)
            {
                Kunder tempkunder = new Kunder();
                tempkunder.Namn = lineinfo;
                lineinfo = sr.ReadLine();

                tempkunder.Adress = lineinfo;
                lineinfo = sr.ReadLine();

                tempkunder.Epost = lineinfo;
                lkunder.Add(tempkunder);

            }
            sr.Close();
            UppdateraLista();
            

        }

then the streamwriter looks like this:

private void SparaTillFil()
        {
             try
            { StreamWriter sw = new StreamWriter("Kunder.txt");
            for (int i = 0; i < lkunder.Count; i++)
            {
                sw.WriteLine(lkunder[i].Namn);
                sw.WriteLine(lkunder[i].Adress);
                sw.WriteLine(lkunder[i].Epost);
            }
            sw.Close();
            }
             catch { }
        }

maybe this can effect:

private void listViewKunder_SelectedIndexChanged(object sender, EventArgs e)
        {
           
            

            if (listViewKunder.SelectedItems.Count > 0)
            {
                txtKundNamn.Text = lkunder[listViewKunder.SelectedItems[0].Index].Namn;
                txtKundAdress.Text = lkunder[listViewKunder.SelectedItems[0].Index].Adress;
                txtKundEpost.Text = lkunder[listViewKunder.SelectedItems[0].Index].Epost;
            }

            //SparaTillFil(); gick ej
            
        }

        private void TaBort()

        {
            try
            {
                if (listViewKunder.SelectedItems.Count > 0)
                {
                    listViewKunder.Items.Remove(listViewKunder.SelectedItems[0]);
                    lkunder.RemoveAt(listViewKunder.SelectedItems[0].Index);
                }
            }
            catch { }
            SparaTillFil();
            
        }

hope you find whats wrong with it...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.