C#如何为整个GUI创建类实例

时间:2015-11-08 20:58:34

标签: c# winforms

我正在制作一个简单的预订系统,该系统使用GUI进行输入和显示,其中包含所有逻辑和存储可用/可用座位的类。我的问题我不知道如何在整个GUI使用表单加载时如何在类的实例上创建。但是我按下了按钮,但是每次单击按钮时,类中存储的数据都会重置。当表单加载时,如何在单击按钮时不重置数据,如何创建类的实例。下面是我的GUI代码,可以解决我的数据重置问题。感谢您的帮助,如果我在发布前搜索时错过了相关主题,请告诉我。

namespace AirLine
{
    public partial class ReservationSystem : Form
    {
        public ReservationSystem()
        {
            InitializeComponent();
            radioFirst.Checked = true; // start program with button checked
        }

        private void buttonBook_Click(object sender, EventArgs e)
        {

            Label[] labelFirst = new Label[] { labelFirst1, labelFirst2, labelFirst3, labelFirst4, labelFirst5 };
            Label[] labelEcon = new Label[] { labelEcon1, labelEcon2, labelEcon3, labelEcon4, labelEcon5 };
            CheckBox[] checkFirst = new CheckBox[] { checkFirst1, checkFirst2, checkFirst3, checkFirst4, checkFirst5 };
            CheckBox[] checkEcon = new CheckBox[] { checkEcon1, checkEcon2, checkEcon3, checkEcon4, checkEcon5 };
            Reservation res = new Reservation();

            int x;
            int i = 0; // counter for bool update
            int k = 0; // counter for manual seat slecetion FirstClass
            int c = 0; // counter for manual seat selection Econ
            int j = 0; // counter to reset unchecked boxes


            while (i < checkFirst.Length) //update fseats bool
            {
                if (checkFirst[i].Checked == true) //if box chceked correspsonding bool change to true
                {
                    res.reserveFirstClassSeat(i);
                }
                if (checkEcon[i].Checked == true) //if box chceked correspsonding bool change to true
                {
                    res.reserveEconomyClassSeat(i);
                }
                ++i; // tick i
            }

            //booking of first avialible seat
           try
            {
                string firstname = textFirst.Text;
                string lastname = textLast.Text;
                int age = Convert.ToInt32(textAge.Text);
                string name = firstname + lastname + age;

                if (textFirst.Text != "" && textLast.Text != "" && Enumerable.Range(1, 150).Contains(age)) //validate user input
                {
                    if (radioFirst.Checked == true)  //User selected first class
                    {
                        if (res.isFirstClassAvailable() == false && res.isEconomyClassAvailable() == true)
                        {
                            System.Windows.Forms.MessageBox.Show("First Class is full, Only Economy left"); // if first class is full inform user
                        }
                        if (res.isFirstClassAvailable() == true ) // verify a seat is open in first class 
                        {
                            x = res.nextAvailableFirstClassSeat(); // store next availible seat number
                            res.reserveFirstClassSeat(x); // resevere next availible seat
                            checkFirst[x].Checked = true; // check checkbox of seat
                            labelFirst[x].Text = name; //Change label to reflect name and age of passenger

                        }
                    }
                    if (radioEcon.Checked == true)  //User selected Econ class
                    {
                        if (res.isFirstClassAvailable() == true && res.isEconomyClassAvailable() == false)
                        {
                            System.Windows.Forms.MessageBox.Show("Economy is full, Only First Class left"); // if econ is full inform user
                        }
                        if (res.isEconomyClassAvailable() == true) // verify a seat is open in first class 
                        {
                            x = res.nextAvailableEconomyClassSeat(); // store next availible seat number
                            res.reserveEconomyClassSeat(x); // resevere next availible seat
                            checkEcon[x].Checked = true; // check checkbox of seat
                            labelEcon[x].Text = name; //Change label to reflect name and age of passenger

                        }
                    }
                }
                else // if input is not valid
                {
                System.Windows.Forms.MessageBox.Show("Please Check Name, Age, and Seat #");
                }


            }
            catch (FormatException)
            {
                System.Windows.Forms.MessageBox.Show("Please Check Name, Age, and Seat #");
            }
            // manual check of seat
            try
            {
                string firstname = textFirst.Text;
                string lastname = textLast.Text;
                int age = Convert.ToInt32(textAge.Text);
                string name = firstname + lastname + age;

                if (textFirst.Text != "" && textLast.Text != "" && Enumerable.Range(1, 150).Contains(age)) //validate user input
                {
                    if (radioManual.Checked == true)
                    {
                        while (k < checkFirst.Length)
                        {
                            if (checkFirst[k].Checked == true && labelFirst[k].Text == "Open") // see if box was checked and label is still "Open"
                            {
                                res.reserveFirstClassSeat(k);
                                labelFirst[k].Text = name;
                            }
                            ++k;
                        }
                        while (c < checkEcon.Length)
                        {
                            if (checkEcon[c].Checked == true && labelEcon[c].Text == "Open")
                            {
                                res.reserveEconomyClassSeat(c);
                                labelEcon[c].Text = name;
                            }
                            ++c;
                        }
                    }
                }
                else // if input is not valid
                {
                    System.Windows.Forms.MessageBox.Show("Please Check Name, Age, and Seat #");
                }
            }
            catch (FormatException)
            {
                System.Windows.Forms.MessageBox.Show("Please Check Name, Age, and Seat #");
            }


            if (res.isFirstClassAvailable() == false && res.isEconomyClassAvailable() == false) // if flight is full inform user
                System.Windows.Forms.MessageBox.Show("Flight is Full");

            {  // clear text boxes
                textFirst.Text = "";
                textLast.Text = "";
                textAge.Text = "";
            }


        }

        private void buttonUpdate_Click(object sender, EventArgs e)
        {
            Label[] labelFirst = new Label[] { labelFirst1, labelFirst2, labelFirst3, labelFirst4, labelFirst5 };
            Label[] labelEcon = new Label[] { labelEcon1, labelEcon2, labelEcon3, labelEcon4, labelEcon5 };
            CheckBox[] checkFirst = new CheckBox[] { checkFirst1, checkFirst2, checkFirst3, checkFirst4, checkFirst5 };
            CheckBox[] checkEcon = new CheckBox[] { checkEcon1, checkEcon2, checkEcon3, checkEcon4, checkEcon5 };
            int j = 0;

            while (j < checkFirst.Length)
            {
                if (checkFirst[j].Checked == false)
                {
                    labelFirst[j].Text = "Open";
                }
                if (checkEcon[j].Checked == false)
                {
                    labelEcon[j].Text = "Open";
                }
                ++j;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

要在整个GUI中使用myClass的实例,请执行以下操作。

 public partial class MainWindow : Window
 {
    MyClass myClass;
    public MainWindow()
    {
        myClass = new MyClass();
        InitializeComponent();

    }
 }