昨天写到加载电影信息,今天讲下如何生成电影座位,就是这个
当我们点击购票成功之后,座位会变红
在主窗体里建Dictionary<string, Label> labels = new Dictionary<string, Label>();
label的集合
//显示座位的方法
public void Seat()
{
int seatRow = 5; //行
int seatLine = 7; //列
for (int i = 0; i < seatRow; i++)
{
for (int j = 0; j < seatLine; j++)
{
Label label = new Label();
//设置背景色
label.BackColor = Color.Yellow;
//设置字体
// label.Font = new System.Drawing.Font("宋体",14.25F,System.Drawing.FontStyle.Regular,System.Drawing.GraphicsUnit.Point,((byte)(134)));
//设置尺寸
label.AutoSize = false;
label.Size = new System.Drawing.Size(50, 25);
//设置座位号
label.Text = (j + 1).ToString() + "-" + (i + 1).ToString();
label.TextAlign = ContentAlignment.MiddleCenter;
//设置位置
label.Location = new Point(60 + (i * 90), 60 + (j * 60));
//所有标签绑定到同一事件
label.Click += new System.EventHandler(labelSeat_Click); //labelSeat_Click是我们自己写的事件
this.tabPage2.Controls.Add(label);
//这里添加回我们刚刚建的集合里去
labels.Add(label.Text, label);
//新建座位类对象,并添加到座位集合里去
seat = new Seat((j + 1).ToString() + "-" + (i + 1).ToString(), Color.Yellow);
c.Seats.Add(seat.SeatNum, seat);
}
}
}
labelSeat_click里面是这样写的
private void labelSeat_Click(object sender, EventArgs e)
{
if (label8.Text == "")
{
MessageBox.Show("您还没选择电影!", "提示");
return;
}
tickets++;
try
{
string seatNum = ((Label)sender).Text.ToString();
string customerName = this.textBox1.Text.ToString();
int discount = 0;//折扣
string type = "";//类型
if (this.radioButton3.Checked)
{
type = "student";
if (this.comboBox1.Text == "")
{
MessageBox.Show("请输入折扣数!", "提示");
return;
}
else
{
discount = Convert.ToInt32(this.comboBox1.Text);
}
}
else if (this.radioButton2.Checked)
{
if (this.textBox1.Text == "")
{
MessageBox.Show("请输入赠票者姓名!", "提示");
return;
}
else
{
type = "free";
}
}
else if (this.radioButton1.Checked)
{
type = "putong";
}
//调用工具类创建票
Ticket Ticket = TicketUtil.CreateTicket(c.Schedule.Items[key], c.Seats[seatNum], discount, customerName, type);
if (c.Seats[seatNum].Color == Color.Yellow)
{
//打印
DialogResult result = MessageBox.Show("是否购买?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (result == DialogResult.Yes)
{
c.Seats[seatNum].Color = Color.Red;
UpdateSeat();
Ticket.CalcPrice();
c.SoldTickets.Add(Ticket);
label6.Text = Ticket.Price.ToString();
Ticket.Print();
}
else if (result == DialogResult.No)
{
return;
}
}
else
{
//显示当前售出票的信息
foreach (Ticket item in c.SoldTickets)
{
//判断是否为同场次、同电影、同座位号
if (item.Seat.SeatNum == seatNum && item.ScheduleItem.Time == this.treeView1.SelectedNode.Text && item.ScheduleItem.Movie.MovieName == this.treeView1.SelectedNode.Parent.Text)
{
item.Show();
}
}
}
}catch(Exception ex){}
}
ClearSeat()用于把红色的座位变成黄色,UpdateSeat()调用集合里不同电影的座位信息
private void ClearSeat()
{
foreach (Seat seat in c.Seats.Values)
{
seat.Color = Color.Yellow;
}
}
/// <summary>
/// 更新座位状态
/// </summary>
private void UpdateSeat()
{
//根据影院类里面的座位来更新座位的颜色
foreach (string key in c.Seats.Keys)
{
labels[key].BackColor = c.Seats[key].Color;
}
}
}