Hello ...

I am trying to make an address book with windows forms, and would like to add a birthday reminder feature.

The name, address, bithdate go into the xml file ( I think!), and I have put a combo box on the form to say yes or no to birthday reminders.

Eventually I would like to have a more sophisticated reminder sytem, but as I don't want to run before walking and because I have already tripped up, I'm just after something basic for now.

So, if the birthday reminder option is chosen and the birthdate is within a month of now I would like the name and birthdate to show in a lsitview, listbox, textbox (or whatever works really). Then, if the birthdate is after 'now', that it is removed or does not show.

I thought that I may be able to do this, but have discovered that I haven't got a clue! I have tried lots of things but this is what I am trying now:

 foreach (XmlNode Node in xDoc.SelectNodes("Contacts/Person"))// searches throuh xml file
         //If combo box'yes please' I want a birthday reminder selected and date Today is birthday minus thirty days so being warned a month before
           if (cboBirthday.Text == "Yes please" && DateTime.FromFileTime(Convert.ToInt64(xNode.SelectSingleNode("Birthday").InnerText)) == dateTimePicker1.Value)
           {
               //list with name and birthdays
                 lstBirthday.Text = "Birthday List " + '\n' + Name + DateTime.FromFileTime(Convert.ToInt64(xNode.SelectSingleNode("Birthday").InnerText));
                 txtBirthday.Text = "Birthday List " + '\n' + Name + DateTime.FromFileTime(Convert.ToInt64(xNode.SelectSingleNode("Birthday").InnerText));
            }

            //If the date of the birthday has passed then remove it from the list
             if ( DateTime.FromFileTime(Convert.ToInt64(xNode.SelectSingleNode("Birthday").InnerText)) <  dateTimePicker1.Value)
            {
                lstBirthday.Text = contacts[lvContacts.SelectedItems[0].Index].Name = ""; //REMOVE
                lstBirthday.Text = contacts[lvContacts.SelectedItems[0].Index].Birthday.; /// ???
             }

As you can see, I am not even sure where I am supposed to be selecting from, the file or the list, so any help that will help clear my messed up mind will be very much appreciated.

Thank you ... John.

I guess you want to create a list of people and display this list whose birth date is equal to DateTimePicker date (day and month). I suggest you to use LINQ to XML to populate List<Person>.

Here is a working sample :

Create a TYPE Person

 public class Person
 {
   public string Name { get; set; }
   public DateTime BirthDate { get; set; }
 }

Code to read XML doc and populate the List<Person>

     XDocument doc = XDocument.Load(fileName);

     var list = doc.Descendants("Person")
                   .Where(x => DateTime.Parse
                         (x.Element("BirthDate").Value).Day == DateTime.Today.Day 
                         && DateTime.Parse(x.Element("BirthDate").Value).Month == DateTime.Today.Month)
                   .Select(x => new Person()
                    {
                     Name = x.Element("Name").Value,
                     BirthDate = DateTime.Parse(x.Element("BirthDate").Value)
                }).ToList();

Bind the result to DataGridView

DataGridView1.DataSource = list;

Edited 3 Years Ago by __avd

Hello __avd

Thank yopu for your help, I am grateful for any assistance. Perhaps I should have explained a little more. I am making the address book for a relative to sit on their desktop, and so I do not really want a grid view as in my understanding (which is limited) is that it would look a bit 'industrial'. That is why I said 'I would like the name and birthdate to show in a lsitview, listbox, textbox (or whatever works really). I should have been more specific and said whichever of these work - sorry for that.

That is why I was attempting it the way I was. I have an element for name and birthday and a drop down option to refer to for a birthday reminder and a Person class (See below) just not sure what I should be referingg to out of these!

XmlDocument xDoc = new XmlDocument();
            xDoc.Load(path + "\\ Address Book\\settings.xml");
            foreach (XmlNode xNode in xDoc.SelectNodes("Contacts/Person"))
            {
                Person p = new Person();

                p.Title = xNode.SelectSingleNode("Title").InnerText;
                p.Name = xNode.SelectSingleNode("Name").InnerText;
                p.Telephone = xNode.SelectSingleNode("Telephone").InnerText;
                p.Email = xNode.SelectSingleNode("Email").InnerText;
                p.Address1 = xNode.SelectSingleNode("Address1").InnerText;
                p.Address2 = xNode.SelectSingleNode("Address2").InnerText;
                p.Town = xNode.SelectSingleNode("Town").InnerText;
                p.County = xNode.SelectSingleNode("County").InnerText;
                p.Postcode = xNode.SelectSingleNode("Postcode").InnerText;
                p.Country = xNode.SelectSingleNode("Country").InnerText;
                p.Notes = xNode.SelectSingleNode("Notes").InnerText;
                p.Birthday = DateTime.FromFileTime(Convert.ToInt64(xNode.SelectSingleNode("Birthday").InnerText));
                p.BirthdayReminder = xNode.SelectSingleNode("BirthdayReminder").InnerText;

                contacts.Add(p);
                lvContacts.Items.Add(p.Name);


              }


                class Person
        {
            public string Title
            {
                get;
                set;
            }
            public DateTime Birthday
            {
                get;
                set;
            }
            public string Name
            {
                get;
                set;
            }
            public string BirthdayReminder
            {
                get;
                set;
            }


            Person c = new Person();
            c.Title = txtTitle.Text;
            c.Birthday = dateTimePicker1.Value;
            c.Name = txtName.Text;
            c.Telephone = txtTelephone.Text;
            c.Email = txtEmail.Text;
            c.Address1 = txtAddress1.Text;
            c.Address2 = txtAddress2.Text;
            c.Town = txtTown.Text;
            c.County = txtCounty.Text;
            c.Postcode = txtPostcode.Text;
            c.Country = txtCountry.Text;
            c.Notes = txtNotes.Text;
            c.BirthdayReminder = cboBirthday.Text;

            contacts.Add(c);
            lvContacts.Items.Add(c.Name);

             XmlNode xTop = xDoc.CreateElement("Person");
                XmlNode xTitle = xDoc.CreateElement("Title");
                XmlNode xName = xDoc.CreateElement("Name");
                XmlNode xTelephone = xDoc.CreateElement("Telephone");
                XmlNode xEmail = xDoc.CreateElement("Email");
                XmlNode xAddress1 = xDoc.CreateElement("Address1");
                XmlNode xAddress2 = xDoc.CreateElement("Address2");
                XmlNode xTown = xDoc.CreateElement("Town");
                XmlNode xCounty = xDoc.CreateElement("County");
                XmlNode xPostcode = xDoc.CreateElement("Postcode");
                XmlNode xCountry = xDoc.CreateElement("Country");
                XmlNode xNotes = xDoc.CreateElement("Notes");
                XmlNode xBirthday = xDoc.CreateElement("Birthday");
                XmlNode xBirthdayReminder = xDoc.CreateElement("BirthdayReminder");

                xTitle.InnerText = c.Title;
                xName.InnerText = c.Name;
                xTelephone.InnerText = c.Telephone;
                xEmail.InnerText = c.Email;
                xAddress1.InnerText = c.Address1;
                xAddress2.InnerText = c.Address2;
                xTown.InnerText = c.Town;
                xCounty.InnerText = c.County;
                xPostcode.InnerText = c.Postcode;
                xCountry.InnerText = c.Country;
                xNotes.InnerText = c.Notes;
                xBirthday.InnerText = c.Birthday.ToFileTime().ToString();
                xBirthdayReminder.InnerText = c.BirthdayReminder;

Edited 3 Years Ago by johnt68

This article has been dead for over six months. Start a new discussion instead.