Expand|Select|Wrap|Line Numbers
- 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;
- namespace Assignment2
- {
- public partial class Form1 : Form
- {
- public MotorVehicle[] arr_MotorVehicles = new MotorVehicle[0];
- private MotorVehicle newVehicle;
- public Form1()
- {
- InitializeComponent();
- }
- private void button_addnewvehicle_Click(object sender, EventArgs e)
- {
- try
- {
- if (textBox_Manufacturer.Text.Trim() == "")
- throw new Exception("Please enter a Manufacturer");
- try
- {
- int.Parse(textBox_YearMade.Text);
- }
- catch (Exception)
- {
- throw new Exception("Invalid format for \'Year Made\'");
- }
- try
- {
- int.Parse(textBox_Occupants.Text);
- }
- catch (Exception)
- {
- throw new Exception("Invalid format for \'Occupants\'");
- }
- newVehicle = new MotorVehicle(textBox_Manufacturer.Text.Trim(), int.Parse(textBox_YearMade.Text), int.Parse(textBox_Occupants.Text));
- //Resize array
- Array.Resize(ref arr_MotorVehicles, arr_MotorVehicles.Length + 1);
- //Insert vehicle
- arr_MotorVehicles[arr_MotorVehicles.GetUpperBound(0)] = newVehicle;
- //Update Age
- label_age.Text = arr_MotorVehicles[arr_MotorVehicles.GetUpperBound(0)].Age.ToString();
- numericUpDown_IndexNumber.Maximum = arr_MotorVehicles.GetUpperBound(0);
- numericUpDown_IndexNumber.Value = arr_MotorVehicles.GetUpperBound(0);
- groupBox_controlcenter.Enabled = true;
- populateRecord();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, "ERROR");
- }
- }
- private void button_Delete_Click(object sender, EventArgs e)
- {
- MotorVehicle[] arr_TempArray = new MotorVehicle[arr_MotorVehicles.GetLength(0) - 1];
- int MaximumNewArrayIndex = arr_TempArray.GetLength(0) - 1;
- for (int i = 0, j = 0; i <= MaximumNewArrayIndex; i++, j++)
- {
- if (j == numericUpDown_IndexNumber.Value) j++;
- arr_TempArray[i] = arr_MotorVehicles[j];
- }
- arr_MotorVehicles = arr_TempArray;
- numericUpDown_IndexNumber.Maximum = Math.Max(arr_MotorVehicles.GetUpperBound(0), 0);
- if (arr_MotorVehicles.Length == 0)
- {
- groupBox_controlcenter.Enabled = false;
- clearRecord();
- }
- else
- {
- populateRecord();
- }
- }
- private void button_Exit_Click(object sender, EventArgs e)
- {
- Close();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- groupBox_controlcenter.Enabled = false;
- textBox_Manufacturer.Clear();
- textBox_Occupants.Clear();
- textBox_YearMade.Clear();
- label_age.Text = null;
- numericUpDown_IndexNumber.Minimum = 0;
- }
- private void numericUpDown_IndexNumber_ValueChanged(object sender, EventArgs e)
- {
- if (arr_MotorVehicles.Length == 0)
- {
- clearRecord();
- }
- else
- {
- populateRecord();
- }
- }
- private void populateRecord()
- {
- textBox_Manufacturer.Text = arr_MotorVehicles[(int)numericUpDown_IndexNumber.Value].Manufacturer;
- textBox_YearMade.Text = arr_MotorVehicles[(int)numericUpDown_IndexNumber.Value].YearMade.ToString();
- textBox_Occupants.Text = arr_MotorVehicles[(int)numericUpDown_IndexNumber.Value].Occupants.ToString();
- label_age.Text = arr_MotorVehicles[(int)numericUpDown_IndexNumber.Value].Age.ToString();
- }
- private void clearRecord()
- {
- textBox_Manufacturer.Text = null;
- textBox_YearMade.Text = null;
- textBox_Occupants.Text = null;
- label_age.Text = null;
- }
- }
- public class MotorVehicle
- {
- private string str_Manufacturer;
- private int int_YearMade;
- private int int_Occupants;
- private int int_Age;
- public MotorVehicle(string str_Manufacturer, int int_YearMade, int int_Occupants)
- {
- Manufacturer = str_Manufacturer;
- YearMade = int_YearMade;
- Occupants = int_Occupants;
- int_Age = System.DateTime.Today.Date.Year - int_YearMade;
- }
- public string Manufacturer
- {
- set
- {
- if (value.Length == 0) throw new Exception("Please enter a Manufacturer");
- str_Manufacturer = value;
- }
- get
- {
- return str_Manufacturer;
- }
- }
- public int YearMade
- {
- set
- {
- if (value < 1769) throw new Exception("\'Year Made\' cannot be earlier than 1769.");
- if (value > System.DateTime.Today.Date.Year) throw new Exception("\'Year Made\' cannot be later than current year (" + System.DateTime.Today.Date.Year.ToString() + ").");
- int_YearMade = value;
- }
- get
- {
- return int_YearMade;
- }
- }
- public int Occupants
- {
- set
- {
- if (value < 1) throw new Exception("Occupants cannot be less than 1");
- if (value > 100) throw new Exception("Occupants cannot be more than 100");
- int_Occupants = value;
- }
- get
- {
- return int_Occupants;
- }
- }
- public int Age
- {
- get
- {
- return int_Age;
- }
- }
- }
- }
- Here's my program.cs code
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Windows.Forms;
- namespace WindowsFormsApplication1
- {
- static class Program
- {
- /// <summary>
- /// The main entry point for the application.
- /// </summary>
- [STAThread]
- static void Main()
- {
- Application.EnableVisualStyles();
- Application.SetCompatibleTextRenderingDefault(false);
- Application.Run(new Assignment2.Form1());
- }
- }
- }