/TravelAgency/TravelAgency/BusTravelAgency/Administrations/frmMain.cs

# · C# · 232 lines · 197 code · 35 blank · 0 comment · 14 complexity · ae0a7bf0814d7c59a5c85ad6403140b0 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using TravelAgencyDAL;
  10. using System.Text.RegularExpressions;
  11. namespace Beginning
  12. {
  13. public partial class frmMain : Form
  14. {
  15. private DataGridViewCellCollection current;
  16. public frmMain()
  17. {
  18. InitializeComponent();
  19. BindControls();
  20. BusesBindControls();
  21. }
  22. private void BindControls()
  23. {
  24. var data = from d in DAO.GetAllDestinations()
  25. select new
  26. {
  27. ID = d.DestinationID,
  28. Destination = d.DestistionName,
  29. WeekSchedule = DAO.GetWeekSchedule(d.DestinationID)
  30. };
  31. dgvMain.DataSource = data.ToList();
  32. dgvMain.Columns[0].Visible = false;
  33. }
  34. private void BusesBindControls()
  35. {
  36. var data = from b in DAO.GetAllBuses()
  37. select new
  38. {
  39. ID = b.BusID,
  40. RegNum = b.BusRegNumber,
  41. Capacity = b.BusCapacity
  42. };
  43. dgvBuses.DataSource = data.ToList();
  44. dgvBuses.Columns[0].Visible = false;
  45. }
  46. private void btnAddDestinationn_Click(object sender, EventArgs e)
  47. {
  48. AddDestination frmAdd = new AddDestination();
  49. frmAdd.Show();
  50. this.Hide();
  51. }
  52. private void btnShow_Click(object sender, EventArgs e)
  53. {
  54. current = dgvMain.CurrentRow.Cells;
  55. frmDestinationDetails frmDetails = new frmDestinationDetails(
  56. Convert.ToInt64(current["ID"].Value.ToString()));
  57. frmDetails.Show();
  58. this.Hide();
  59. }
  60. private void btnDelete_Click(object sender, EventArgs e)
  61. {
  62. current = dgvMain.CurrentRow.Cells;
  63. long id = Convert.ToInt64(current["ID"].Value.ToString());
  64. bool confirm =
  65. MessageBox.Show("Delete the selected destination?", "Confirmation", MessageBoxButtons.YesNo,
  66. MessageBoxIcon.Question) == DialogResult.Yes;
  67. if (id != 0)
  68. {
  69. if (confirm)
  70. {
  71. DAO.DeleteDestination(id);
  72. }
  73. }
  74. BindControls();
  75. }
  76. private void btnAddBus_Click(object sender, EventArgs e)
  77. {
  78. gbAddNewBus.Enabled = true;
  79. }
  80. private void btnCancel_Click(object sender, EventArgs e)
  81. {
  82. gbAddNewBus.Enabled = false;
  83. txtRegNum.Text = string.Empty;
  84. txtCapacity.Text = string.Empty;
  85. }
  86. private void btnOK_Click(object sender, EventArgs e)
  87. {
  88. Buse newBus = new Buse();
  89. newBus.BusCapacity = Convert.ToInt32(txtCapacity.Text);
  90. newBus.BusRegNumber = txtRegNum.Text;
  91. bool confirm =
  92. MessageBox.Show("Add this bus?", "Confirmation", MessageBoxButtons.OKCancel,
  93. MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.OK;
  94. if (confirm)
  95. {
  96. DAO.AddNewBus(newBus);
  97. MessageBox.Show("A new Bus Added!");
  98. BusesBindControls();
  99. gbAddNewBus.Enabled = false;
  100. }
  101. }
  102. private void btnDeleteBus_Click(object sender, EventArgs e)
  103. {
  104. current = dgvBuses.CurrentRow.Cells;
  105. long id = Convert.ToInt64(current["ID"].Value.ToString());
  106. if (id != 0)
  107. {
  108. bool confirm =
  109. MessageBox.Show("Delete this bus?", "Confirmation", MessageBoxButtons.OKCancel,
  110. MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.OK;
  111. if (confirm)
  112. {
  113. DAO.DeleteBus(id);
  114. MessageBox.Show("The Bus was Deleted!");
  115. }
  116. }
  117. BusesBindControls();
  118. }
  119. private void button1_Click(object sender, EventArgs e)
  120. {
  121. frmBusesSchedules busSchedules = new frmBusesSchedules();
  122. busSchedules.Show();
  123. this.Hide();
  124. }
  125. private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
  126. {
  127. Application.Exit();
  128. }
  129. #region Validation
  130. private void txtRegNum_Validating(object sender, CancelEventArgs e)
  131. {
  132. ValidateRegNum();
  133. }
  134. private void txtCapacity_Validating(object sender, CancelEventArgs e)
  135. {
  136. ValidateCapacity();
  137. }
  138. private bool ValidateRegNum()
  139. {
  140. if (IsRegNumValid(txtRegNum.Text))
  141. {
  142. epValidateData.SetError(txtRegNum, "");
  143. return true;
  144. }
  145. else
  146. {
  147. epValidateData.SetError(txtRegNum, "Not a valid reg. number!");
  148. return false;
  149. }
  150. }
  151. private bool IsRegNumValid(string regNum)
  152. {
  153. string pattern = @"[A-Z]{1,2}\s{1}[0-9]{4}\s{1}[A-Z]{1,2}";
  154. Match match = Regex.Match(regNum, pattern);
  155. if (match.Success)
  156. {
  157. return true;
  158. }
  159. else
  160. {
  161. return false;
  162. }
  163. }
  164. private bool ValidateCapacity()
  165. {
  166. if (IsCapacityValid(txtCapacity.Text))
  167. {
  168. epValidateData.SetError(txtCapacity, "");
  169. return true;
  170. }
  171. else
  172. {
  173. epValidateData.SetError(txtCapacity, "Not a valid capacity!");
  174. return false;
  175. }
  176. }
  177. private bool IsCapacityValid(string capacity)
  178. {
  179. try
  180. {
  181. if (Decimal.Parse(capacity) > 0)
  182. {
  183. return true;
  184. }
  185. else
  186. {
  187. return false;
  188. }
  189. }
  190. catch (FormatException)
  191. {
  192. return false;
  193. }
  194. }
  195. #endregion
  196. }
  197. }