C#从列表和输入框中选择并传递参数

时间:2019-02-23 23:02:23

标签: c# wpf

我正在尝试创建一个小型应用。其中一个从列表框中选取选择的内容,在此示例中为租车。然后,它根据所选汽车显示租车价格,并存储此选择。然后,用户输入他们希望租用汽车的天数,然后在单独的页面上显示成本,购车选择和租用天数的结果。

我已经填充了列表,并且能够按一下按钮导航到下一页。我的主要问题是将数据传递到新页面上显示。

下面是主窗口中的代码:

namespace CarHireLtd
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        //Declare variables to be used throughout the program
        int cost;
        string carSelection;
        string hireLength;



        public MainWindow()
        {
            InitializeComponent();
            // we need to populate the listbox with the selection of
            //vehicles to hire when the window loads

            lstCars.Items.Add("TOYOTA IQ");
            lstCars.Items.Add("MINI COOPER S");
            lstCars.Items.Add("VW POLO");
            lstCars.Items.Add("AUDI TT");
            lstCars.Items.Add("MERCEDES A 220");
            lstCars.Items.Add("RANGE ROVER EVOQUE");
            lstCars.Items.Add("JAGUAR XJ");


        }

        private void LstCars_SelectionChanged(object sender, 
SelectionChangedEventArgs e)
    {
            // find out which item the user has selected
            int table = lstCars.SelectedIndex + 1;

            // initialise the textblock to be empty
            txtCost.Text = "";
            if (table == 1) //If the table matches a certain item, display 
                           //  the cost of 
                            //the rental and store the cost in the 
                               //variable 'cost'
            {
                carSelection = "TOYOTA IQ";
                cost = 10;
                txtCost.Text = "Your rental for this vehicle is £10 per 
day";
            }
            else if (table == 2)
            {
                {
                    carSelection = "MINI COOPER S";
                    cost = 18;
                    txtCost.Text = "Your rental for this vehicle is £18 
per day";
                }
            }
            else if (table == 3)
            {
                {
                    carSelection = "VW POLO";
                    cost = 15;
                    txtCost.Text = "Your rental for this vehicle is £15 
per day";
                }
            }
            else if (table == 4)
            {
                {
                    carSelection = "AUDI TT";
                    cost = 30;
                    txtCost.Text = "Your rental for this vehicle is £30 
per day";
                }
            }
            else if (table == 5)
            {
                {
                    carSelection = "MERCEDES A 220";
                    cost = 40;
                    txtCost.Text = "Your rental for this vehicle is £40 
per day";
                }
            }
            else if (table == 6)
            {
                {
                    carSelection = "RANGE ROVER EVOQUE";
                    cost = 50;
                    txtCost.Text = "Your rental for this vehicle is £50 
per day";
                }
            }
            else if (table == 7)
            { 
                {
                    carSelection = "JAGUAR XJ";
                    cost = 55;
                    txtCost.Text = "Your rental for this vehicle is £55 
per day";
                }
            }





        }

        private void TxtHireLength_TextChanged(object sender, 
TextChangedEventArgs e)
        {
            hireLength = txtHireLength.Text; //Read in amount of hired 
days from input box
        }

        public void BtnNextAge_Click(object sender, RoutedEventArgs e)
        {
            AgeWindow Window2 = new AgeWindow();
            Window2.Show();
            this.Close();

        }
    }
}

,以下是来自ResultsWindow的信息:

namespace CarHireLtd
{
    /// <summary>
    /// Interaction logic for ResultsWindow.xaml
    /// </summary>
    public partial class ResultsWindow : Window
    {
        public ResultsWindow(string cost, string carSelection, string 
hireLength)
        {
            InitializeComponent();

            blkResults.Text = "You selected a " + carSelection + "\nfor: " 
+ hireLength + "\nthe total hire cost will be " + cost +hireLength;
        }
    }

}

我对此完全是个菜鸟,只是想尽一切办法。任何帮助,将不胜感激。这也是Stackoverflow上的第一篇文章,因此似乎到处都是。

谢谢

0 个答案:

没有答案