完整的代码低于我搜索的两个小问题,没有。第一个问题是当我从列表中删除一个选定的项目时,它会将其删除,但是列表没有正确排序,因为它会删除它,但之前的那个是那里的那个而不是我删除的那个。接下来是我的第二个问题,当我关闭表单时,我从列表中删除的内容未保存到我的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();
}
}
}
答案 0 :(得分:1)
Employers em = new Employers();
employers.RemoveAt(listView1.SelectedItems[0].Index);
foreach (ListViewItem eachItem in listView1.SelectedItems)
{
listView1.Items.Remove(eachItem);
}
在干涉后,一些帮助找到答案。