C# 我想编辑我的程序,而不是让用户在文本框中输入座位号进行预订,他们可以在列表框中选择座位号

C# 我想编辑我的程序,而不是让用户在文本框中输入座位号进行预订,他们可以在列表框中选择座位号,c#,visual-studio-2010,C#,Visual Studio 2010,我将如何更改代码以消除接受座位号值并进行预订的文本框?他们只需从列表框中的座位表中选择想要的座位,然后单击“进行预订”。我知道这里有一些未使用的代码需要清理,忽略它,我一直在用不同的方法来处理 这是表格代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using Sys

我将如何更改代码以消除接受座位号值并进行预订的文本框?他们只需从列表框中的座位表中选择想要的座位,然后单击“进行预订”。我知道这里有一些未使用的代码需要清理,忽略它,我一直在用不同的方法来处理

这是表格代码

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;
        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);

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


        }

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

        private void DisplaySeatChart(string[] seating)
        {
            lstSeatChart.Items.Clear();
            for (int seat = 0; seat <= seating.GetUpperBound(0); seat++)
            {
                lstSeatChart.Items.Add("Seat: " + (seat + 1) + "       " + seating[seat]);
            }
        }

        private void SetupFlights()
        {


            //flightlist.Add(flight1);
            //flightlist.Add(flight2);


        }

        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);
            lstFlights.Items.Add(flight2);
        }

        private void btnMakeReservation_Click(object sender, EventArgs e)
        {
            string name;
            int seatNum;
            string[] seatChart = curFlight.SeatChart;

            if (txtCustomerName.Text != "" && txtSeatNum.Text != "")
            {
                name = txtCustomerName.Text;
                seatNum = Convert.ToInt16(txtSeatNum.Text);
                curFlight.MakeReservation(name, seatNum);

                lstSeatChart.Items.Clear();

                DisplaySeatChart(seatChart);
            }
            else
            {
                MessageBox.Show("Please Fill out Name and Seat Number.", "Reservation Error");
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            string name;
            int seatNum;
            string[] seatChart = curFlight.SeatChart;

            if (txtCustomerName.Text != "" && txtSeatNum.Text != "")
            {
                name = txtCustomerName.Text;
                seatNum = Convert.ToInt16(txtSeatNum.Text);
                curFlight.EditReservation(name, seatNum);

                lstSeatChart.Items.Clear();

                DisplaySeatChart(seatChart);
            }
            else
            {
                MessageBox.Show("Please Fill out Name and Seat Number.", "Reservation Error");
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
命名空间保留
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
飞行;飞行;
List flightlist=新列表();
航班1=新航班(“塞斯纳引文X”,“上午10:00”,“丹佛”,6,2);
Flight flight2=新航班(“Piper Mirage”,“晚上10:00”,“堪萨斯城”,3,2);
私有void Form1\u加载(对象发送方、事件参数e)
{
作出保留();
显示航班();
设置航班();
}
私用void lstfights\u SelectedIndexChanged(对象发送方,事件参数e)
{
curflights=(flights)lstfights.SelectedItem;
txtdeive.Text=curFlight.de出发时间;
txtDestination.Text=curFlight.Destination;
字符串[]seatChart=curFlight.seatChart;
显示seatChart(seatChart);
}
专用void DisplaySeatChart(字符串[]座位)
{
lstSeatChart.Items.Clear();

对于(int seat=0;seat,座位图表集合中的项目首先应该有一个标识符。一旦有了标识符,该标识符(或描述、名称等)所有“开放”座位都应添加到用户可以选择的列表框中。在预订时,您只需确定选择了哪些列表框项目。您可以使用或不使用数据绑定来执行此操作

    List<Seat> seats;

    private void button6_Click(object sender, EventArgs e)
    {
        seats = new List<Seat>
        {
            new Seat { Identifier = "A1", Description = "A1 - Window" },
            new Seat { Identifier = "A2", Description = "A2 - Center" },
            new Seat { Identifier = "A3", Description = "A3 - Aisle" } 
        };

        listBox1.DataSource = seats;
        listBox1.DisplayMember = "Description";
        listBox1.ValueMember = "Identifier"; 
    }

    private void button7_Click(object sender, EventArgs e)
    {
        // get selected seat
        foreach (Seat selectedSeat in listBox1.SelectedItems)
        {
            MessageBox.Show(selectedSeat.Identifier);
        }
    }
列出座位;
私有无效按钮6_单击(对象发送者,事件参数e)
{
席位=新名单
{
新座位{Identifier=“A1”,Description=“A1-窗口”},
新座位{Identifier=“A2”,Description=“A2-Center”},
新座位{Identifier=“A3”,Description=“A3-过道”}
};
listBox1.DataSource=座位;
listBox1.DisplayMember=“Description”;
listBox1.ValueMember=“标识符”;
}
私有无效按钮7_单击(对象发送者,事件参数e)
{
//选择座位
foreach(座椅选择列表框1中的座椅。选择编辑项)
{
MessageBox.Show(selectedSeat.Identifier);
}
}

当然,你可以围绕这个例子来玩,只显示那些免费的座位。你可以通过几种方式来实现,其中之一是在你的座位类上添加一个标志。

答案很简单,而不是简单

seatNum = Convert.ToInt16(txtSeatNum.Text);
使用


我正试图确切地理解您的意思,是否有办法将seatNum=Convert.ToInt16(txtSeatNum.Text)更改为类似seatNum=Convert.ToInt16(lstSeatChart.SelectedItem)的内容;我真的不明白如何创建标识符,或者您的确切意思是什么。
seatNum = Convert.ToInt16(txtSeatNum.Text);
seatNum = lst.SeatChart.SelectedIndex + 1;