/TravelAgency/TravelAgency/BusTravelAgency/Administrations/frmMain.cs
# · C# · 232 lines · 197 code · 35 blank · 0 comment · 14 complexity · ae0a7bf0814d7c59a5c85ad6403140b0 MD5 · raw file
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using TravelAgencyDAL;
- using System.Text.RegularExpressions;
-
- namespace Beginning
- {
- public partial class frmMain : Form
- {
- private DataGridViewCellCollection current;
-
- public frmMain()
- {
- InitializeComponent();
- BindControls();
- BusesBindControls();
- }
-
- private void BindControls()
- {
- var data = from d in DAO.GetAllDestinations()
- select new
- {
- ID = d.DestinationID,
- Destination = d.DestistionName,
- WeekSchedule = DAO.GetWeekSchedule(d.DestinationID)
- };
-
- dgvMain.DataSource = data.ToList();
- dgvMain.Columns[0].Visible = false;
- }
-
- private void BusesBindControls()
- {
- var data = from b in DAO.GetAllBuses()
- select new
- {
- ID = b.BusID,
- RegNum = b.BusRegNumber,
- Capacity = b.BusCapacity
- };
-
- dgvBuses.DataSource = data.ToList();
- dgvBuses.Columns[0].Visible = false;
- }
-
- private void btnAddDestinationn_Click(object sender, EventArgs e)
- {
- AddDestination frmAdd = new AddDestination();
- frmAdd.Show();
- this.Hide();
- }
-
- private void btnShow_Click(object sender, EventArgs e)
- {
- current = dgvMain.CurrentRow.Cells;
- frmDestinationDetails frmDetails = new frmDestinationDetails(
- Convert.ToInt64(current["ID"].Value.ToString()));
- frmDetails.Show();
- this.Hide();
- }
-
- private void btnDelete_Click(object sender, EventArgs e)
- {
- current = dgvMain.CurrentRow.Cells;
-
- long id = Convert.ToInt64(current["ID"].Value.ToString());
-
- bool confirm =
- MessageBox.Show("Delete the selected destination?", "Confirmation", MessageBoxButtons.YesNo,
- MessageBoxIcon.Question) == DialogResult.Yes;
-
- if (id != 0)
- {
- if (confirm)
- {
- DAO.DeleteDestination(id);
- }
- }
-
- BindControls();
- }
-
- private void btnAddBus_Click(object sender, EventArgs e)
- {
- gbAddNewBus.Enabled = true;
- }
-
- private void btnCancel_Click(object sender, EventArgs e)
- {
- gbAddNewBus.Enabled = false;
- txtRegNum.Text = string.Empty;
- txtCapacity.Text = string.Empty;
- }
-
- private void btnOK_Click(object sender, EventArgs e)
- {
- Buse newBus = new Buse();
- newBus.BusCapacity = Convert.ToInt32(txtCapacity.Text);
- newBus.BusRegNumber = txtRegNum.Text;
-
- bool confirm =
- MessageBox.Show("Add this bus?", "Confirmation", MessageBoxButtons.OKCancel,
- MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.OK;
-
- if (confirm)
- {
- DAO.AddNewBus(newBus);
- MessageBox.Show("A new Bus Added!");
- BusesBindControls();
- gbAddNewBus.Enabled = false;
- }
- }
-
- private void btnDeleteBus_Click(object sender, EventArgs e)
- {
- current = dgvBuses.CurrentRow.Cells;
-
- long id = Convert.ToInt64(current["ID"].Value.ToString());
-
- if (id != 0)
- {
- bool confirm =
- MessageBox.Show("Delete this bus?", "Confirmation", MessageBoxButtons.OKCancel,
- MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.OK;
-
- if (confirm)
- {
- DAO.DeleteBus(id);
- MessageBox.Show("The Bus was Deleted!");
- }
- }
-
- BusesBindControls();
- }
-
- private void button1_Click(object sender, EventArgs e)
- {
- frmBusesSchedules busSchedules = new frmBusesSchedules();
- busSchedules.Show();
- this.Hide();
- }
-
- private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
- {
- Application.Exit();
- }
-
- #region Validation
-
- private void txtRegNum_Validating(object sender, CancelEventArgs e)
- {
- ValidateRegNum();
- }
-
- private void txtCapacity_Validating(object sender, CancelEventArgs e)
- {
- ValidateCapacity();
- }
-
- private bool ValidateRegNum()
- {
- if (IsRegNumValid(txtRegNum.Text))
- {
- epValidateData.SetError(txtRegNum, "");
- return true;
- }
- else
- {
- epValidateData.SetError(txtRegNum, "Not a valid reg. number!");
- return false;
- }
- }
-
- private bool IsRegNumValid(string regNum)
- {
- string pattern = @"[A-Z]{1,2}\s{1}[0-9]{4}\s{1}[A-Z]{1,2}";
-
- Match match = Regex.Match(regNum, pattern);
-
- if (match.Success)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
-
- private bool ValidateCapacity()
- {
- if (IsCapacityValid(txtCapacity.Text))
- {
- epValidateData.SetError(txtCapacity, "");
- return true;
- }
- else
- {
- epValidateData.SetError(txtCapacity, "Not a valid capacity!");
- return false;
- }
- }
-
- private bool IsCapacityValid(string capacity)
- {
- try
- {
- if (Decimal.Parse(capacity) > 0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- catch (FormatException)
- {
- return false;
- }
- }
-
- #endregion
- }
- }