显示有关在文本框中的列表框中选择的对象的信息

时间:2012-11-27 00:48:10

标签: c# visual-studio-2010

我想显示有关在旁边的文本框中列表框中选择的任何航班的信息。我的问题是我无法让我的curFlight变量正常工作给我InvalidCastException是未处理的错误并且说当铸造一个数字时,值必须是一个小于无穷大的数字。

继承我的表格代码

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 Reservations
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Flight curFlight;

        Flight flight1 = new Flight("Cessna Citation X", "10:00AM", "Denver", 6, 2);
        Flight flight2 = new Flight("Piper Mirage", "10:00PM", "Kansas City", 3, 2);

        private void Form1_Load(object sender, EventArgs e)
        {
            MakeReservations();
            DisplayFlights();

        }

        private void lstFlights_SelectedIndexChanged(object sender, EventArgs e)
        {
            curFlight = (Flight)lstFlights.SelectedItem;
            txtDepart.Text = curFlight.DepartureTime;
            txtDestination.Text = curFlight.Destination;
        }

        private void MakeReservations()
        {
            flight1.MakeReservation("Dill", 12);
            flight1.MakeReservation("Deenda", 3);
            flight1.MakeReservation("Schmanda", 11);
            flight2.MakeReservation("Dill", 4);
            flight2.MakeReservation("Deenda", 2);
        }

        private void DisplayFlights()
        {
            lstFlights.Items.Clear();
            lstFlights.Items.Add(flight1.Plane);
            lstFlights.Items.Add(flight2.Plane);
        }

        private void btnMakeReservation_Click(object sender, EventArgs e)
        {
            string name;
            int seatNum;

            name = txtCustomerName.Text;
            seatNum = Convert.ToInt16(txtSeatNum.Text);

            curFlight.MakeReservation(name, seatNum);
        }
    }
}

HERES THE CLASS CODE

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Reservations
{
    class Flight
    {
        private string mPlane;
        private string mDepartureTime;
        private string mDestination;
        private int mRows;
        private int mSeats;
        private string[] mSeatChart;

        public Flight()
        {
        }

        public Flight(string planeType, string departureTime, string destination, int numRows, int numSeatsPerRow)
        {
            this.Plane = planeType;
            this.DepartureTime = departureTime;
            this.Destination = destination;
            this.Rows = numRows;
            this.Seats = numSeatsPerRow;

            // create the seat chart array
            mSeatChart = new string[Rows * Seats];

            for (int seat = 0; seat <= mSeatChart.GetUpperBound(0); seat++)
            {
                mSeatChart[seat] = "Open";
            }
        }

        public string Plane
        {
            get { return mPlane; }
            set { mPlane = value; }
        }

        public string DepartureTime
        {
            get { return mDepartureTime; }
            set { mDepartureTime = value; }
        }

        public string Destination
        {
            get { return mDestination; }
            set { mDestination = value; }
        }

        public int Rows
        {
            get { return mRows; }
            set { mRows = value; }
        }

        public int Seats
        {
            get { return mSeats; }
            set { mSeats = value; }
        }

        public string[] SeatChart
        {
            get { return mSeatChart; }
            set { mSeatChart = value; }
        }


        public void MakeReservation(string name, int seat)
        {
            if (seat <= (Rows * Seats) && mSeatChart[seat - 1] == "Open")
            {
                mSeatChart[seat - 1] = name;
            }
            else
            {
                //let calling program know it didnt work.

            }
        }

        public bool IsFull()
        {
            return false;
        }
    }
}

2 个答案:

答案 0 :(得分:2)

您收到错误的原因是您将字符串分配给列表框,然后尝试将该字符串强制转换为Flight对象

lstFlights.Items.Add(flight1.Plane);   // Just say it is named "Plane 1"
lstFlights.Items.Add(flight2.Plane);   // Just say it is named "Plane 2"


curFlight = (Flight)lstFlights.SelectedItem; // Now you are trying to convert "Plane 2" into a flight object which is incorrect

试试这个

添加航班类型的全球列表

List<Flight> flightList = new List<Flight>();

Flight flight1 = new Flight("Cessna Citation X", "10:00AM", "Denver", 6, 2);
Flight flight2 = new Flight("Piper Mirage", "10:00PM", "Kansas City", 3, 2);

在表单中加载SetupFlights

private void Form1_Load(object sender, EventArgs e)
{
        MakeReservations();
        DisplayFlights();
        SetupFlights();  // added this line
}

 private void SetupFlights()
 {
        flightList.Add(flight1);
        flightList.Add(flight2);
 }

并且您选择的Index Changed应该是这样的

private void lstFlights_SelectedIndexChanged(object sender, EventArgs e)
{
        curFlight = flightList.FirstOrDefault(x => x.Plane == lstFlights.SelectedItem.ToString()); // Changed this line
        txtDepart.Text = curFlight.DepartureTime;
        txtDestination.Text = curFlight.Destination;
}

答案 1 :(得分:1)

尽管MVCKarl的答案更为一般,但我会展示一种更简单的方法,这可能适用于简单的情况。

您可以简单地覆盖ToString类的Flight

class Flight
{
    //your code

    public override string ToString()
    {
        //or anything you want to display
        return this.Plane;
    }        
}

然后修改DisplayFlights方法,将航班添加到列表中:

private void DisplayFlights()
{
    lstFlights.Items.Clear();
    lstFlights.Items.Add(flight1);
    lstFlights.Items.Add(flight2);
}

在此之后,lstFlights_SelectedIndexChanged将按预期开始工作:

private void lstFlights_SelectedIndexChanged(object sender, EventArgs e)
{
    curFlight = (Flight)lstFlights.SelectedItem;
    textBox1.Text = curFlight.DepartureTime;
    textBox2.Text = curFlight.Destination;
}