dimanche 23 octobre 2016

How to select and un-select checkboxes to "reserve" them?

Hey guys so I'm pretty new to C# although I've had a little bit of experience with Java so C# seems familiar and sometimes easier in a way. I'm having a hard time figuring out how to interact with the GUI and although I got through it a little bit I am now stuck. Below I created a small project where you have 10 seats on an airplane (I will eventually put in more) and you can select either "First Class" or "Economy Class", click " Reserve" and the box will be checked. Well what if for example I want the user to select a seat on their own and have that seat reserved? At the moment each seat is reserved by itself but I want the user to select and un-cselect whatever seat they want...keeping the program the way it is. Thank you in advance :)

namespace AirplaneReservationSystem
{
    public partial class Form1 : Form
    {

        public CheckBox[] BusinessClass = new CheckBox[5];
        public CheckBox[] EconomyClass = new CheckBox[5];
        public Form1()
        {
            InitializeComponent();

            for (var i=0; i < BusinessClass.Length; i++)
            {
                BusinessClass[0] = seatOneCheck;
                BusinessClass[1] = seatTwoCheck;
                BusinessClass[2] = seatThreeCheck;
                BusinessClass[3] = seatFourCheck;
                BusinessClass[4] = seatFiveCheck;
            }

            for (var i=0; i < EconomyClass.Length; i++)
            {
                EconomyClass[0] = seatSixCheck;
                EconomyClass[1] = seatSevenCheck;
                EconomyClass[2] = seatEightCheck;
                EconomyClass[3] = seatNineCheck;
                EconomyClass[4] = seatTenCheck;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void reserveBtn_Click(object sender, EventArgs e)
        {

            if (firstClassBtn.Checked)
            {
                BusinessClassChecked();
            }
            else if (econClassBtn.Checked)

                EconomyClassChecked();


        }

        private void BusinessClassChecked()
        {
            for(var i = 0; i < BusinessClass.Length; i++)
            {
                if (!BusinessClass[i].Checked)
                {
                    BusinessClass[i].Checked = true;
                    int seat = i + 1;
                    MessageBox.Show("Seat has been confirmed!");
                    break;

                }

                if ( i == BusinessClass.Length - 1)
                {
                    MessageBox.Show("There are no more seats left");
                }
            }
        }

        private void EconomyClassChecked()
        {
             for(var i = 0; i < EconomyClass.Length; i++)
            {
                if (!EconomyClass[i].Checked)
                {
                    EconomyClass[i].Checked = true;
                    int seat = i + 1;
                    MessageBox.Show("Seat has been confirmed!");
                    break;
                }

                if (i == EconomyClass.Length - 1)
                {
                    MessageBox.Show("There are no more seats left!");
                }
            }
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void econClassBtn_CheckedChanged(object sender, EventArgs e)
        {

        }
     }
}




Aucun commentaire:

Enregistrer un commentaire