Thread: ListView - Delete and Save

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    17

    ListView - Delete and Save

    Well, this code is probably going to look like something Frankenstein built, since this is my first attempt at using ListViews. It was all working GREAT until I decided "you know, I should add a delete function so people can erase unwanted entries."

    Anyway, here's the basic idea. I'm creating a little mini database program so people can store character information, including passwords, for a game. It takes in 5 values and stores them in a file (using Soap Format) . Then it'll take the entries you've saved and load them into the ListView for all to see. That works great.

    Now I want to add in a delete function. That's.. not so great. I figured out how to make it erase it physically from the list, but here's the deal: when I hit "add" or close and reload the program the deleted values are back in there. The reason is because it's not writing it back to the file. That's where I'm having problems. I can't figure out how to make it write it back into the file in SOAP format (overwriting the file).

    Some other things I might want to add to help explain my (probably poor) logic in this program. There's a class called "Character" that has a sole purpose of storing the character information (name, level, password, the items used, and the creator). Then there's an ArrayList that's filled with "Characters" objects.


    First, here's the add command code. It works the way it should. I'm just posting it for reference.
    Code:
            private void btnAdd_Click(object sender, EventArgs e)
            {
                    if (cmbCharacters.Text == "")
                    {
                        MessageBox.Show("You must choose a Character", "Choose a Character");
                        cmbCharacters.Focus();
                        return;
                    }
    
                    if (txtLevel.Text == "")
                    {
                        MessageBox.Show("You must enter a level", "Enter a level");
                        txtLevel.Focus();
                        return;
                    }
    
                    if (txtItems.Text == "")
                    {
                        MessageBox.Show("You must enter the capsules the char is using",
                            "Enter the capsules used");
                        txtItems.Focus();
                        return;
                    }
    
                    if (txtPassword1.Text == "")
                    {
                        MessageBox.Show("Password is invalid", "Invalid Password");
                        txtPassword1.Focus();
                        return;
                    }
    
                    if (txtPassword2.Text == "")
                    {
                        MessageBox.Show("Password is invalid", "Invalid Password");
                        txtPassword2.Focus();
                        return;
                    }
    
                    if (txtPassword3.Text == "")
                    {
                        MessageBox.Show("Password is invalid", "Invalid Password");
                        txtPassword3.Focus();
                        return;
                    }
    
                    if (txtPassword4.Text == "")
                    {
                        MessageBox.Show("Password is invalid", "Invalid Password");
                        txtPassword4.Focus();
                        return;
                    }
    
                    if (txtPassword5.Text == "")
                    {
                        MessageBox.Show("Password is invalid", "Invalid Password");
                        txtPassword5.Focus();
                        return;
                    }
    
                    if (txtPassword6.Text == "")
                    {
                        MessageBox.Show("Password is invalid", "Invalid Password");
                        txtPassword6.Focus();
                        return;
                    }
    
                    if (txtCreator.Text == "")
                    {
                        MessageBox.Show("Creator is invalid", "Invalid Creator");
                        txtCreator.Focus();
                        return;
                    }
    
                    else
                    {
                        string strFilename = "Characters.bdb";
                        Character character = new Character();
    
                        character.characterName = cmbCharacters.Text;
                        character.level = txtLevel.Text;
                        character.capsules = txtItems.Text;
                        character.creator = txtCreator.Text;
                        character.password = txtPassword1.Text + " " + txtPassword2.Text +
                            " " + txtPassword3.Text + " " + txtPassword4.Text + " " +
                            txtPassword5.Text + " " + txtPassword6.Text;
    
    
                        Budokai_3_Password_Holder.Program.mainForm.lstCharacters.Add(character);
    
                        FileStream bdbStream = new FileStream(strFilename, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write);
                        SoapFormatter bdbSoap = new SoapFormatter();
                        bdbSoap.Serialize(bdbStream, Budokai_3_Password_Holder.Program.mainForm.lstCharacters);
                        bdbStream.Close();
    
                        // Clear out this form so we can enter another char
                        cmbCharacters.Text = "";
                        txtLevel.Text = "";
                        txtItems.Text = "";
                        txtCreator.Text = "";
                        txtPassword1.Text = txtPassword2.Text = txtPassword3.Text = txtPassword4.Text = 
                            txtPassword5.Text = txtPassword6.Text = "";
    
                        // Update the list on the frmMain
                        Budokai_3_Password_Holder.Program.mainForm.showCharacters();
    
                    }
            }
    Now here's the code when the program is first opened, i.e. the "load" command. It also works


    Code:
            private void frmMain_Load(object sender, EventArgs e)
            {
                lstCharacters = new ArrayList();
                SoapFormatter bdbSoap = new SoapFormatter();
                string strFilename = "Characters.bdb";
    
                if (File.Exists(strFilename))
                {
                    FileStream bdbStream = new FileStream(strFilename, FileMode.Open, FileAccess.Read, FileShare.Read);
                    lstCharacters = (ArrayList)bdbSoap.Deserialize(bdbStream);
                    bdbStream.Close();
                }
    
                showCharacters();
            }


    This is the code that runs when I need to refresh the list, or show the characters.
    Code:
            internal void showCharacters()
            {
                SoapFormatter bdbSoap = new SoapFormatter();
                String strFilename = "Characters.bdb";
    
                if (File.Exists(strFilename))
                {
                    FileStream bdbStream = new FileStream(strFilename, FileMode.Open, FileAccess.Read, FileShare.Read);
                    ArrayList lstChar = (ArrayList)bdbSoap.Deserialize(bdbStream);
                   
                    Character characters;
    
    
                    lstDatabase.Items.Clear();
                    for (int i = 0; i < lstChar.Count; i++)
                    {
                        characters = (Character)lstChar[i];
                        ListViewItem lviCharacters = new ListViewItem(characters.characterName);
    
                        lviCharacters.SubItems.Add(characters.level);
                        lviCharacters.SubItems.Add(characters.capsules);
                        lviCharacters.SubItems.Add(characters.password);
                        lviCharacters.SubItems.Add(characters.creator);
                        lstDatabase.Items.Add(lviCharacters);
                    }
    
                    bdbStream.Close();
    
                }
    
            }




    AND FINALLY, here's there code I'm working on and ready to rip my hair over. It's NOT working. At least it doesn't throw an exception...

    Code:
            private void deleteEntry()
            {
                SoapFormatter bdbSoap = new SoapFormatter();
                String strFilename = "Characters.bdb";
    
                if (File.Exists(strFilename))
                {
                    FileStream bdbReadStream = new FileStream(strFilename, FileMode.Open, FileAccess.Read, FileShare.Read);
                    ArrayList lstChar = (ArrayList)bdbSoap.Deserialize(bdbReadStream);
                    bdbReadStream.Close();     
    
    
    
    
                    // Because lstDatabase.SelectedItems is a COLLECTION of items (all the various columns)
                    // we must remove each item from that particular row using the foreach command.
                    foreach (ListViewItem item in lstDatabase.SelectedItems)
                    {
                        lstDatabase.Items.Remove(item);
                    }
    
    
                    // Let's create a new stream that'll write over the old file.
                    FileStream bdbCreate = new FileStream(strFilename, FileMode.Create, FileAccess.Write, FileShare.Write);
    
    
                   // Write the array to the file
                    bdbSoap.Serialize(bdbCreate, lstCharacters);
    
                    // Close the stream when we're done with it.
                    bdbCreate.Close();
                }
            }

    I'm at a loss on how to fix this. Er, well, let me say this.. I have an idea on how to fix it but I don't know how to implement it. I think the easiest thing to do would be to detect which index value that was erased (store it in an int) then tell the ArrayList to remove the same index (since they should synch up). However, I don't know how to get the index value from the ListView for the entry that's being removed. And I don't even know if this method of thinking would even work.

    And I hope this long post made some kind of sense. I really appreciate any help anyone can offer.
    Last edited by Iyouboushi; 07-20-2006 at 10:00 PM.

  2. #2
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Place this before lstDatabase.Items.Remove(item);
    Code:
    ind++;
    arr[ind]=lstDatabase.Items.IndexOf(item);
    Remmeber to define ind and arr before using them.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I Want to save the info that was input.
    By Djanvk in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2008, 09:06 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. new and delete
    By anthonye in forum C++ Programming
    Replies: 11
    Last Post: 07-08-2003, 09:15 AM
  5. updating and deleting a record
    By Burritt in forum C Programming
    Replies: 2
    Last Post: 04-19-2003, 04:58 PM