2014-02-19 100 views
-2

完整的代碼是下面有兩個小問題,我搜索,並沒有什麼。第一個問題是,當我從列表中刪除一個選定的項目時,它將它刪除,但是列表並沒有正確地命令它自己,因爲它刪除了它,但之前的那個是那個那個,而不是我刪除的那個。接下來是我的第二個問題,當我關閉表單時,我從列表中刪除的內容不會保存到我的xml文檔中。所以當我打開表格時,我所刪除的東西根本就沒有刪除,一切都還在。窗體關閉並從窗體中刪除項目

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.IO; 
using System.Xml; 
using System.Resources; 

namespace SaveOpenXmlTest1._2 
{ 

public partial class Form1 : Form 
{ 
    //ListViewItem employeer = new ListViewItem(); 
    //List<Employer> employer = new List<Employer>(); 
    List<Employeer> employeer = new List<Employeer>(); 

    DialogResult result; 
    string filename; 
    string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); 
    //private StreamWriter filewriter; 
    //private StreamReader fileReader; 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    //List<Employ> employ = new List<Employ>(); 
    //List<Employ> employeer = new List<Employ>(); 

    private void Form1_Load(object sender, EventArgs e) 
    { 

    } 

    private void saveToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     using (SaveFileDialog filechooser = new SaveFileDialog()) 
     { 
      filechooser.Filter = "Xml Files (*.xml)|*.xml"; 
      filechooser.CheckFileExists = false; 
      result = filechooser.ShowDialog(); 
      filename = filechooser.FileName; 
     } 

     if (result == DialogResult.OK) 
     { 

      if (filename == string.Empty) 
      { 
       MessageBox.Show("Invalid File Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
      } 
      else 
      { 
       try 
       { 
        //FileStream output = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write); 

        //filewriter = new StreamWriter(output); 

        ///////////////////////////////////////////////////////////////// 

        //if (!Directory.Exists(filename + "Employment History")) 
        // Directory.CreateDirectory(filename + "Empoyment History"); 

        if (!File.Exists(filename)) 
        { 
         XmlTextWriter xW = new XmlTextWriter(filename, Encoding.UTF8); 

         xW.Formatting = Formatting.Indented; 
         xW.WriteStartDocument(); 
         xW.WriteStartElement("Employeer"); 
         xW.WriteEndElement(); 
         xW.Close(); 

         XmlDocument xDoc = new XmlDocument(); 
         xDoc.Load(filename); 

         foreach(XmlNode xNode in xDoc.SelectNodes("Employeer/Employeer")) 
         { 
          Employeer em = new Employeer(); 
          em.Employeerc = xNode.SelectSingleNode("Name").InnerText; 
          em.StartDate = DateTime.FromFileTime(Convert.ToInt64(xNode.SelectSingleNode("StartDate").InnerText)); 
          em.EndDate = DateTime.FromFileTime(Convert.ToInt64(xNode.SelectSingleNode("EndDate").InnerText)); 

          employeer.Add(em); 
          listView1.Items.Add(em.Employeerc); 

         } 

         //xW = WriteStartElement("Emplyment"); 

        } 

        saveToolStripMenuItem.Enabled = false; 
        saveAsToolStripMenuItem.Enabled = true; 

       } 
       catch (IOException) 
       { 
        MessageBox.Show("Error Openning File", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
       } 
      } 

     } 

    } 

    private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) 
    { 

    } 

    class Employeer 
    { 
     public string Employeerc { get; set; } 
     public DateTime StartDate { get; set; } 
     public DateTime EndDate { get; set; }    
    } 


    private void button1_Click(object sender, EventArgs e) 
    { 
     Employeer em = new Employeer(); 
     em.Employeerc = textBox1.Text; 
     em.StartDate = dateTimePicker1.Value; 
     em.EndDate = dateTimePicker2.Value; 

     employeer.Add(em); 

     listView1.Items.Add(em.Employeerc); 
     textBox1.Text = ""; 
     dateTimePicker1.Value = DateTime.Now; 
     dateTimePicker2.Value = DateTime.Now; 
    } 


    private void listView1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     if(listView1.SelectedItems.Count == 0) 
      return; 

     textBox1.Text = employeer[listView1.SelectedItems[0].Index].Employeerc; 
     dateTimePicker1.Value = employeer[listView1.SelectedItems[0].Index].StartDate; 
     dateTimePicker2.Value = employeer[listView1.SelectedItems[0].Index].EndDate; 

    } 


    private void Form1_FormClosing(object sender, FormClosingEventArgs e) 
    { 
     //try 
     //{ 
      XmlDocument xDoc = new XmlDocument(); 
      xDoc.Load(filename); 

      XmlNode xNode = xDoc.SelectSingleNode("Employeer"); 
      xNode.RemoveAll(); 

      foreach (Employeer em in employeer) 
      { 
       XmlNode xTop = xDoc.CreateElement("Employeer"); 
       XmlNode xName = xDoc.CreateElement("Name"); 
       XmlNode xStartDate = xDoc.CreateElement("StartDate"); 
       XmlNode xEndDate = xDoc.CreateElement("EndDate"); 

       xName.InnerText = em.Employeerc; 
       xStartDate.InnerText = em.StartDate.ToFileTime().ToString(); 
       xEndDate.InnerText = em.EndDate.ToFileTime().ToString(); 

       xTop.AppendChild(xName); 
       xTop.AppendChild(xStartDate); 
       xTop.AppendChild(xEndDate); 

       xDoc.DocumentElement.AppendChild(xTop); 
      } 
      xDoc.Save(filename); 

     //} 
     //catch 
     //{ 

     //} 
    } 


    private void openToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     using (OpenFileDialog fileChooser = new OpenFileDialog()) 
     { 
      fileChooser.Filter = "Xml Files (*.xml)|*.xml"; 
      fileChooser.CheckFileExists = false; 
      result = fileChooser.ShowDialog(); 
      filename = fileChooser.FileName; 
     } 
     if (result == DialogResult.OK) 
     { 

      if (filename == string.Empty) 
      { 
       MessageBox.Show("Invalid File Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
      } 
      else 
      { 
       try 
       { 
        //FileStream output = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write); 

        //filewriter = new StreamWriter(output); 

        ///////////////////////////////////////////////////////////////// 

        //if (!Directory.Exists(filename + "Employment History")) 
        // Directory.CreateDirectory(filename + "Empoyment History"); 

        //if (!File.Exists(filename)) 
        //{ 
        // XmlTextWriter xW = new XmlTextWriter(filename, Encoding.UTF8); 

        // xW.Formatting = Formatting.Indented; 
        // xW.WriteStartDocument(); 
        // xW.WriteStartElement("Employeer"); 
        // xW.WriteEndElement(); 
        // xW.Close(); 

         XmlDocument xDoc = new XmlDocument(); 
         xDoc.Load(filename); 

         foreach (XmlNode xNode in xDoc.SelectNodes("Employeer/Employeer")) 
         { 
          Employeer em = new Employeer(); 
          em.Employeerc = xNode.SelectSingleNode("Name").InnerText; 
          em.StartDate = DateTime.FromFileTime(Convert.ToInt64(xNode.SelectSingleNode("StartDate").InnerText)); 
          em.EndDate = DateTime.FromFileTime(Convert.ToInt64(xNode.SelectSingleNode("EndDate").InnerText)); 

          employeer.Add(em); 
          listView1.Items.Add(em.Employeerc); 

         } 

         //xW = WriteStartElement("Emplyment"); 

        //} 

        saveToolStripMenuItem.Enabled = false; 
        saveAsToolStripMenuItem.Enabled = true; 

       } 
       catch (IOException) 
       { 
        MessageBox.Show("Error Openning File", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
       } 
      } 

     } 


    } 


    private void exitToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     Close(); 
     //MessageBox.Show("Save and Exit", "Cancel", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); 
    } 


    private void textBox2_TextChanged(object sender, EventArgs e) 
    { 


    } 


    void Remove() 
    { 
     try 
     { 

      //listView1.Items.RemoveAt(listView1.SelectedItems[0].Index); 
      //employeer.RemoveRange(listView1.SelectedItems[0].Index, 1); 



      if (listView1.SelectedItems.Count >= 1) 
      { 
       //return; 
       listView1.Items.Remove(listView1.SelectedItems[0]); 
       employeer.RemoveAt((int)listView1.SelectedItems[0].Index); 

       textBox1.Text = ""; 
       dateTimePicker1.Value = DateTime.Now; 
       dateTimePicker2.Value = DateTime.Now; 
      } 
     } 
     catch 
     { 

     } 
    } 


    private void button3_Click(object sender, EventArgs e) 
    { 
     Remove(); 
    } 

    } 
} 
+7

[This](http://www.sscce.org/)總是有助於更快更好地回答問題。 –

+0

在調試器中逐步執行'button3_Click'事件和'Remove()'函數。在'RemoveAt'調用之前和之後,特別注意'employeers'列表的長度。它會改變嗎? –

+0

我試着移動東西,並用其他操作員替換它,但仍然沒有骰子。我可以添加到XML文件,但是當我刪除某些東西並關閉它,然後當我打開它時,所有東西都還在那裏。即使它從列表中刪除,也沒有被刪除。 – user3329960

回答

1
Employers em = new Employers(); 

employers.RemoveAt(listView1.SelectedItems[0].Index); 

foreach (ListViewItem eachItem in listView1.SelectedItems) 
{ 
    listView1.Items.Remove(eachItem); 
} 

染指玲周圍,一些幫助後,找到了答案。

相關問題