/C#/Assignment VII/ORS/ORS/CustomerForm.cs

https://github.com/nrkkalyan/padma-projects · C# · 243 lines · 152 code · 23 blank · 68 comment · 18 complexity · 5a6836e2dde816ec2bda4972146c5d19 MD5 · raw file

  1. // File Name: CustomerForm.cs
  2. // Created By: Padma Priya Duvvuri
  3. // Created On: 24-Dec-2011
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Windows.Forms;
  12. using System.Collections;
  13. using System.Text.RegularExpressions;
  14. namespace ORS
  15. {
  16. public partial class CustomerForm : Form
  17. {
  18. //customer object receiving input and/or sending output
  19. private CustomerContacts m_customer;
  20. //flag to handle the closing of the form
  21. private bool closeForm;
  22. private string[] m_fullConact; //to store value of customer details
  23. /// <summary>
  24. /// Property CustomerData to read customer values, with access to
  25. /// read and write
  26. /// </summary>
  27. /// <value></value>
  28. /// <returns>Customer object</returns>
  29. public CustomerContacts CustomerData
  30. {
  31. get { return m_customer; }
  32. set
  33. {
  34. if (value != null)
  35. m_customer = value;
  36. //update input controls
  37. UpdateGUI();
  38. }
  39. }
  40. /// <summary>
  41. /// update the controls of customerForm to the m_customer object values
  42. /// </summary>
  43. private void UpdateGUI()
  44. {
  45. txtCustomerEmail.Text = m_customer.Email;
  46. txtCustomerFirstName.Text = m_customer.FirstName;
  47. txtCustomerLastName.Text = m_customer.LastName;
  48. txtCustomerPhone.Text = m_customer.Phone;
  49. }
  50. //constructor with one parameter (title of the form)
  51. public CustomerForm(string title)
  52. {
  53. InitializeComponent();
  54. //Other initalizations
  55. this.Text = title;
  56. closeForm = true;
  57. }
  58. /// <summary>
  59. /// Write only property to set the value of m_fullContact
  60. /// </summary>
  61. public string[] Details
  62. {
  63. set
  64. {
  65. if (value != null)
  66. m_fullConact = value;
  67. }
  68. }
  69. /// <summary>
  70. /// Constructor with 2 paramerts with custom title and details of customer
  71. /// </summary>
  72. /// <param name="title"></param>
  73. /// <param name="details"></param>
  74. public CustomerForm(string title, string[] details)
  75. {
  76. InitializeComponent();
  77. //Other initalizations
  78. this.Text = title;
  79. closeForm = true;
  80. m_customer = new CustomerContacts(details);
  81. UpdateGUI();
  82. }
  83. /// <summary>
  84. /// Event hadler for formclosing event event of the form
  85. /// </summary>
  86. /// <param name="sender"></param>
  87. /// <param name="e"></param>
  88. private void Customer_FormClosing(object sender, FormClosingEventArgs e)
  89. {
  90. if (closeForm)
  91. e.Cancel = false;
  92. else
  93. e.Cancel = true;
  94. }
  95. /// <summary>
  96. /// Event-handler to handle click event of btnCustomer
  97. /// </summary>
  98. /// <param name="sender"></param>
  99. /// <param name="e"></param>
  100. private void btnCustomer_Click(object sender, EventArgs e)
  101. {
  102. //validate the user given values for Name fields and phone number
  103. if (!ValidateInputFields())
  104. {
  105. return;
  106. }
  107. //if no customer is present then create a new customer
  108. if (m_customer == null)
  109. {
  110. m_customer = new CustomerContacts();
  111. }
  112. //set the values inputted by the user to m_customer object
  113. m_customer.Email = txtCustomerEmail.Text;
  114. m_customer.Phone = txtCustomerPhone.Text ;
  115. m_customer.FirstName = txtCustomerFirstName.Text;
  116. m_customer.LastName = txtCustomerLastName.Text;
  117. UpdateGUI();
  118. this.DialogResult = DialogResult.OK;
  119. }
  120. /// <summary>
  121. /// Validates the firstname, lastname and telephone numbers.
  122. /// </summary>
  123. /// <returns>returns true if all the three fields are validated, false
  124. /// otherwise</returns>
  125. private bool ValidateInputFields()
  126. {
  127. //calls checkstring method of inpututility class
  128. if (!InputUtility.CheckString(txtCustomerFirstName.Text))
  129. {
  130. MessageBox.Show("First name cannot be empty", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  131. lblFirstName.Text = "First Name*";
  132. return false;
  133. }
  134. if (!InputUtility.CheckString(txtCustomerLastName.Text))
  135. {
  136. MessageBox.Show("Last name cannot be empty", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  137. lblLastName.Text = "Last Name*";
  138. return false;
  139. }
  140. //validation for phone numbers
  141. if (!ValidationForPhoneNumbers())
  142. {
  143. return false;
  144. }
  145. //to validate email
  146. if (!ValidateEmail())
  147. {
  148. MessageBox.Show("Enter a valid E-mail address", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  149. lblLastName.Text = "Email*";
  150. return false;
  151. }
  152. return true;
  153. }
  154. /// <summary>
  155. /// It validates the email.
  156. /// </summary>
  157. /// <returns>true if email is valid and false otherwise</returns>
  158. private bool ValidateEmail()
  159. {
  160. string pattern = @"^[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|" +
  161. @"0-9]+([_][a-z|0-9]+)*)?@[a-z][a-z|0-9|]*\.([a-z]" +
  162. @"[a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$";
  163. System.Text.RegularExpressions.Match match =
  164. Regex.Match(txtCustomerEmail.Text.Trim(), pattern, RegexOptions.IgnoreCase);
  165. if (match.Success)
  166. return true;
  167. else
  168. return false;
  169. }
  170. /// <summary>
  171. /// checks whehter atleast one telepone number is provided or not.And validates the
  172. /// presented by the user
  173. /// </summary>
  174. /// <returns>true if atleast one phone number is given and validates the given
  175. /// user input</returns>
  176. private bool ValidationForPhoneNumbers()
  177. {
  178. if ( txtCustomerPhone.Text == string.Empty)
  179. {
  180. MessageBox.Show("Telephone number should be provided", "Information!", MessageBoxButtons.OK, MessageBoxIcon.Information);
  181. lblPhone.Text = "Phone/Moble*";
  182. return false;
  183. }
  184. if (txtCustomerPhone.Text != string.Empty)
  185. {
  186. return ValidatePhoneNumber(txtCustomerPhone.Text);
  187. }
  188. return true;
  189. }
  190. /// <summary>
  191. /// If atleast one phone number is provided, converts the value to long
  192. /// by calling GetLong method of InputUtility
  193. /// </summary>
  194. /// <param name="phone">string value of the user input for phone number</param>
  195. /// <returns>true if conversion is successful</returns>
  196. private bool ValidatePhoneNumber(string phone)
  197. {
  198. long number;
  199. bool isValid = InputUtility.GetLong(phone, out number);
  200. if (isValid )
  201. {
  202. return true;
  203. }
  204. else
  205. {
  206. MessageBox.Show("Invalid Phone Format", "Info!", MessageBoxButtons.OK, MessageBoxIcon.Information);
  207. return false;
  208. }
  209. }
  210. /// <summary>
  211. /// Event handler for the click event of btnCancel to close the form.
  212. /// </summary>
  213. /// <param name="sender"></param>
  214. /// <param name="e"></param>
  215. private void btnCancel_Click(object sender, EventArgs e)
  216. {
  217. this.Close();
  218. }
  219. }
  220. }