Ejemplo n.º 1
0
        /// <summary>
        /// This button handler creates instance of Person, validates it, 
        /// adds instance to ListBox, clears TextBoxes.
        /// </summary>
        /// <param name="sender">Object that generates this event.</param>
        /// <param name="e">Arguments.</param>
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            Person person = new Person
            {
                FirstName = textFirstName.Text,
                LastName = textLastName.Text,
                EMail = textEMail.Text,
                Phone = textPhone.Text
            };

            Validator validator = new Validator();
            try
            {
                validator.Validate(person);
            }
            catch (Exception ex)
            {
                person = null;
                GC.Collect();
                MessageBox.Show(ex.Message);
                return;
            }
            listBox1.DisplayMember = "LastName";
            listBox1.Items.Add(person);
            ClearTextBoxes();
        }
Ejemplo n.º 2
0
 /// <summary>
 /// It creates Person's instance and adds it to ListBox.
 /// </summary>
 /// <param name="reader">Stream Reader.</param>
 /// <param name="listBox1">Instance of ListBox.</param>
 private void CreatePerson(StreamReader reader, ListBox listBox1)
 {
     Person person = new Person
     {
         FirstName = reader.ReadLine().Trim(),
         LastName = reader.ReadLine().Trim(),
         EMail = reader.ReadLine().Trim(),
         Phone = reader.ReadLine().Trim()
     };
     listBox1.DisplayMember = "LastName";
     listBox1.Items.Add(person);
 }
Ejemplo n.º 3
0
 /// <summary>
 /// It creates Person's instance, assigns it's fields, adds it to ListBox.
 /// </summary>
 /// <param name="node">Node of Xml document.</param>
 /// <param name="listBox1">Instance of ListBox.</param>
 private void CreatePerson(XmlNode node, ListBox listBox1)
 {
     if (node.HasChildNodes)
     {
         XmlNodeList children = node.ChildNodes;
         Person person = new Person
         {
             FirstName = children[0].InnerText,
             LastName = children[1].InnerText,
             EMail = children[2].InnerText,
             Phone = children[3].InnerText,
         };
         listBox1.DisplayMember = "LastName";
         listBox1.Items.Add(person);
     }
 }
Ejemplo n.º 4
0
        internal void Validate(Person person)
        {
            Regex regex = new Regex(@"^[a-zA-Z][a-zA-Z]*");
            if (regex.IsMatch(person.FirstName) == false)
                throw new Exception("Fist name is not valid.");

            if (regex.IsMatch(person.LastName) == false)
                throw new Exception("Last name is not valid.");

            regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
            if (regex.IsMatch(person.EMail) == false)
                throw new Exception("e-mail is not valid.");

            regex = new Regex(@"^[+0-9(][0-9 ()-]*");
            if (regex.IsMatch(person.Phone) == false)
                throw new Exception("Phone number is not valid.");
        }
Ejemplo n.º 5
0
        /// <summary>
        /// It bilds nodes of Xml tree.
        /// </summary>
        /// <param name="p">Instance of Person.<param>
        /// <param name="doc">Instance of Xml Document.<param>
        private void Bild(Person p, XmlDocument doc)
        {
            XmlElement StartElement = doc.CreateElement("Person");
            doc.DocumentElement.AppendChild(StartElement);

            XmlElement newElem = doc.CreateElement("FirstName");
            newElem.InnerText = p.FirstName;
            StartElement.AppendChild(newElem);

            newElem = doc.CreateElement("LastName");
            newElem.InnerText = p.LastName;
            StartElement.AppendChild(newElem);

            newElem = doc.CreateElement("EMail");
            newElem.InnerText = p.EMail;
            StartElement.AppendChild(newElem);

            newElem = doc.CreateElement("Phone");
            newElem.InnerText = p.Phone;
            StartElement.AppendChild(newElem);
        }