Pri kraju sam sa projektom koji radim za svoje potrebe, u pitanju je simple phone book. Uploadovao sam ceo solution ovde, pa ko ima malo vremena može da se iscima da pogleda.
Interfejs izgleda ovako, a evo i problema ujedno:

Naizgled sve radi lepo, međutim, verujem da je problem nastao kada sam podesio:
listView1.Sorting = SortOrder.Ascending;
Neko možda uviđa u čemu je stvar. Dakle:
1) Ako uzmemo za primer da su u listi 6 kontakta, a da Kontakt 1 živi u gradu 1, ulici 1, sa brojem telefona 1...a Kontakt 2 živi u gradu 2, ulici 2... [niz se nastavlja] - i, na primer, želim da editujem Kontakt 4 tako da postane Kontakt 7 - jednostavno se podaci pomešaju, ne znam po kom obrascu. Dakle prvobitni Kontakt 4 (koji je trebalo da postane Kontakt 7) je postao Kontakt 6 ... i tako dalje - sve ispremeštano.
Čini se da je u pitanju Save button (toolStripButton1_Click), nisam pametan.
2) Ako uzmemo za primer da su u listi 6 kontakta, i hoću da obrišem svih 6 - izbrisaće njih petoro, a jedan će ostati. Isto tako, ako ih je 100 - obrisaće 99, a ostaće 1.
Čini se da je u pitanju Remove metoda.
Evo i koda:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System;
using System.Xml.Xsl;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<Person> people = new List<Person>();
private void Form1_Load(object sender, EventArgs e)
{
toolStripButton5.Enabled = false;
listView1.Sorting = SortOrder.Ascending;
string path = "E:\\Cloud\\GDrive\\Backup\\System Files\\Phone Book\\Contacts.xml";
if (!File.Exists(path))
{
XmlTextWriter xW = new XmlTextWriter(path, Encoding.UTF8);
xW.WriteStartElement("People");
xW.WriteEndElement();
xW.Close();
}
XmlDocument xDoc = new XmlDocument();
xDoc.Load(path);
foreach (XmlNode xNode in xDoc.SelectNodes("People/Person"))
{
Person p = new Person();
p.Name = xNode.SelectSingleNode("Name").InnerText;
p.Hometown = xNode.SelectSingleNode("Hometown").InnerText;
p.Address = xNode.SelectSingleNode("Address").InnerText;
p.Birthday = DateTime.FromFileTime(Convert.ToInt64(xNode.SelectSingleNode("Birthday").InnerText));
p.Phone = xNode.SelectSingleNode("Phone").InnerText;
p.Email = xNode.SelectSingleNode("Email").InnerText;
p.AdditionalInfo = xNode.SelectSingleNode("AdditionalInfo").InnerText;
people.Add(p);
listView1.Items.Add(p.Name);
UserCount();
}
}
private void button2_Click(object sender, EventArgs e)
{
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count == 0) return;
textBox1.Text = people[listView1.SelectedItems[0].Index].Name;
textBox2.Text = people[listView1.SelectedItems[0].Index].Hometown;
textBox3.Text = people[listView1.SelectedItems[0].Index].Address;
textBox4.Text = people[listView1.SelectedItems[0].Index].Phone;
textBox5.Text = people[listView1.SelectedItems[0].Index].Email;
textBox6.Text = people[listView1.SelectedItems[0].Index].AdditionalInfo;
dateTimePicker1.Value = people[listView1.SelectedItems[0].Index].Birthday;
textBox1.ReadOnly = true;
textBox2.ReadOnly = true;
textBox3.ReadOnly = true;
textBox4.ReadOnly = true;
textBox5.ReadOnly = true;
textBox6.ReadOnly = true;
dateTimePicker1.Enabled = false;
toolStripButton5.Enabled = true;
}
private void button3_Click(object sender, EventArgs e)
{
}
void Remove()
{
try
{
if (listView1.SelectedItems.Count > 0)
{
people.RemoveAt(listView1.SelectedItems[0].Index);
listView1.SelectedItems[0].Remove();
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
dateTimePicker1.Value = DateTime.Now;
if (listView1.SelectedItems.Count == 0)
{
textBox1.ReadOnly = false;
textBox2.ReadOnly = false;
textBox3.ReadOnly = false;
textBox4.ReadOnly = false;
textBox5.ReadOnly = false;
textBox6.ReadOnly = false;
dateTimePicker1.Enabled = true;
}
}
else
{
MessageBox.Show("Nothing is selected! ", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch { }
}
void UserCount()
{
try
{
if ((listView1.Items.Count) == 1)
{
toolStripLabel1.Text = "Total: " + Convert.ToString(listView1.Items.Count) + "& contact";
}
else
{
toolStripLabel1.Text = "Total: " + Convert.ToString(listView1.Items.Count) + "& contacts";
}
}
catch { }
}
private void button1_Click(object sender, EventArgs e)
{
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
XmlDocument xDoc = new XmlDocument();
string path = "E:\\Cloud\\GDrive\\Backup\\System Files\\Phone Book\\Contacts.xml";
xDoc.Load(path);
XmlNode xNode = xDoc.SelectSingleNode("People");
xNode.RemoveAll();
foreach (Person p in people)
{
XmlNode xTop = xDoc.CreateElement("Person");
XmlNode xName = xDoc.CreateElement("Name");
XmlNode xHometown = xDoc.CreateElement("Hometown");
XmlNode xAddress = xDoc.CreateElement("Address");
XmlNode xBirthday = xDoc.CreateElement("Birthday");
XmlNode xPhone = xDoc.CreateElement("Phone");
XmlNode xEmail = xDoc.CreateElement("Email");
XmlNode xAdditionalInfo = xDoc.CreateElement("AdditionalInfo");
xName.InnerText = p.Name;
xHometown.InnerText = p.Hometown;
xAddress.InnerText = p.Address;
xBirthday.InnerText = p.Birthday.ToFileTime().ToString();
xPhone.InnerText = p.Phone;
xEmail.InnerText = p.Email;
xAdditionalInfo.InnerText = p.AdditionalInfo;
xTop.AppendChild(xName);
xTop.AppendChild(xHometown);
xTop.AppendChild(xAddress);
xTop.AppendChild(xBirthday);
xTop.AppendChild(xPhone);
xTop.AppendChild(xEmail);
xTop.AppendChild(xAdditionalInfo);
xDoc.DocumentElement.AppendChild(xTop);
xDoc.Save(path);
}
}
private void button4_Click(object sender, EventArgs e)
{
}
private void button5_Click(object sender, EventArgs e)
{
this.Close();
}
private void removeToolStripMenuItem_Click(object sender, EventArgs e)
{
Remove();
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
people[listView1.SelectedItems[0].Index].Name = textBox1.Text;
people[listView1.SelectedItems[0].Index].Hometown = textBox2.Text;
people[listView1.SelectedItems[0].Index].Address = textBox3.Text;
people[listView1.SelectedItems[0].Index].Phone = textBox4.Text;
people[listView1.SelectedItems[0].Index].Email = textBox5.Text;
people[listView1.SelectedItems[0].Index].Birthday = dateTimePicker1.Value;
people[listView1.SelectedItems[0].Index].AdditionalInfo = textBox6.Text;
listView1.SelectedItems[0].Text = textBox1.Text;
textBox1.ReadOnly = true;
textBox2.ReadOnly = true;
textBox3.ReadOnly = true;
textBox4.ReadOnly = true;
textBox5.ReadOnly = true;
textBox6.ReadOnly = true;
dateTimePicker1.Enabled = false;
}
else
{
MessageBox.Show("Nothing is selected ", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
UserCount();
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
MessageBox.Show("Please make sure you have no contacts selected!", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else if (textBox1.ReadOnly == true) { MessageBox.Show("Please make sure you refresh fields first!", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Information); }
else
{
if ((textBox1.Text.Trim().Length == 0)) { MessageBox.Show("Please enter contact's name!", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Information); }
else
{
Person p = new Person();
p.Name = textBox1.Text;
p.Hometown = textBox2.Text;
p.Address = textBox3.Text;
p.Phone = textBox4.Text;
p.Email = textBox5.Text;
p.Birthday = dateTimePicker1.Value;
p.AdditionalInfo = textBox6.Text;
people.Add(p);
listView1.Items.Add(p.Name);
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
dateTimePicker1.Value = DateTime.Now;
UserCount();
}
}
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
Remove();
UserCount();
}
private void toolStripButton4_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count == 0)
{
UserCount();
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
dateTimePicker1.Value = DateTime.Now;
textBox1.ReadOnly = false;
textBox2.ReadOnly = false;
textBox3.ReadOnly = false;
textBox4.ReadOnly = false;
textBox5.ReadOnly = false;
textBox6.ReadOnly = false;
dateTimePicker1.Enabled = true;
toolStripButton5.Enabled = false;
}
}
private void toolStripLabel1_Click(object sender, EventArgs e)
{
}
private void toolStripLabel1_Click_1(object sender, EventArgs e)
{
}
private void toolStripButton5_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
textBox1.ReadOnly = false;
textBox2.ReadOnly = false;
textBox3.ReadOnly = false;
textBox4.ReadOnly = false;
textBox5.ReadOnly = false;
textBox6.ReadOnly = false;
dateTimePicker1.Enabled = true;
}
else
{
MessageBox.Show("Nothing is selected! ", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Information);
toolStripButton5.Enabled = false;
textBox1.ReadOnly = true;
textBox2.ReadOnly = true;
textBox3.ReadOnly = true;
textBox4.ReadOnly = true;
textBox5.ReadOnly = true;
textBox6.ReadOnly = true;
dateTimePicker1.Enabled = false;
}
}
}
class Person
{
public string Name
{
get;
set;
}
public string Hometown
{
get;
set;
}
public string Email
{
get;
set;
}
public string Address
{
get;
set;
}
public string Phone
{
get;
set;
}
public string AdditionalInfo
{
get;
set;
}
public DateTime Birthday
{
get;
set;
}
}
}
Da li neko ima rešenje? Please, help.
[Ovu poruku je menjao Cal Lightman dana 18.11.2013. u 17:51 GMT+1]