📄 form1.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace CinemaTicket
{
public partial class MainFrom : Form
{
public MainFrom()
{
InitializeComponent();
}
Schedule sc = new Schedule();
Cinema cinema = new Cinema();
string key;
Dictionary<string, Label> labels = new Dictionary<string, Label>();
private void 获取新列表ToolStripMenuItem_Click(object sender, EventArgs e)
{
sc.LoadItems(tvCinema);
tabControl2.TabPages[0].Controls.Clear();
}
/// <summary>
/// Treeview切换选择事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tvCinema_AfterSelect(object sender, TreeViewEventArgs e)
{
if (tvCinema.SelectedNode.Level != 0)
{
key = tvCinema.SelectedNode.Text;
tabControl2.TabPages[0].Controls.Clear();
cinema.Seats.Clear();
labels.Clear();
GetXiangQing();
ClearSeat();
//遍历座位销售情况
foreach (Ticket tt in cinema.SoldTickets)
{
foreach (Seat seat in cinema.Seats.Values)
{
if ((tt.ScheduleItem.Time == key) && (tt.Seat.SeatNum == seat.SeatNum))
{
seat.Color = Color.Red;
}
}
}
UpdateSeat();
}
}
/// <summary>
/// 更新
/// </summary>
/// <param name="key"></param>
private void UpdateSeat()
{
foreach (string key in cinema.Seats.Keys)
{
Console.WriteLine(cinema.Seats[key].Color);
labels[key].BackColor = cinema.Seats[key].Color;
}
}
/// <summary>
/// 清空座位
/// </summary>
private void ClearSeat()
{
foreach (Seat seat in cinema.Seats.Values)
{
seat.Color = Color.Yellow;
}
}
/// <summary>
/// 显示详情框中内容
/// </summary>
private void GetXiangQing()
{
foreach (ScheduleItem cc in sc.Items.Values)
{
if (tvCinema.SelectedNode.Text == cc.Time)
{
lblTime.Text = cc.Time;
lblMovieName.Text = cc.Movie.MovieName;
picPoster.Image = Image.FromFile(cc.Movie.Poster);
lblActor.Text = cc.Movie.Actor;
lblDirector.Text = cc.Movie.Director;
lblPrice.Text = cc.Movie.Price.ToString();
lblNewPrice.Text = cc.Movie.Price.ToString();
lblMoveType.Text = cc.Movie.MovieType.ToString();
//加载座位
InitSeats(7, 5, tabControl2.TabPages[0]);
}
}
}
/// <summary>
/// 动态加载座位
/// </summary>
private void InitSeats(int seatRow, int seatLine, TabPage tb)
{
Label label = null;
Seat seat = null;
for (int i = 0; i < seatRow; i++)
{
for (int j = 0; j < seatLine; j++)
{
label = new Label();
label.BackColor = Color.Yellow;
label.AutoSize = false;
label.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
label.Location = new System.Drawing.Point(59, 60);
label.Name = "lbl" + (j + 1) + "_" + (i + 1);
label.Size = new System.Drawing.Size(50, 25);
//计算座位号
label.Text = (j + 1) + "-" + (i + 1);
label.TextAlign = ContentAlignment.MiddleCenter;
//确定座位的位置
label.Location = new Point(60 + (i * 90), 60 + (j * 60));
//绑定到一个单击事件
label.Click += new EventHandler(this.lblBuy_Click);
//在放映厅TabLontrol控件上添加Label
tb.Controls.Add(label);
labels.Add(label.Text, label);
seat = new Seat((j + 1) + "-" + (i + 1));
cinema.Seats.Add(seat.SeatNum, seat);
}
}
}
TicketFactory tf = new TicketFactory();
/// <summary>
/// 座位单击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void lblBuy_Click(object sender, EventArgs e)
{
//为判断取值
int cboValues = 0;
//进行判断验证
if (radioButton2.Checked == true && textBox1.Text == "")
{
MessageBox.Show("请输入赠送者姓名!");
return;
}
if (radioButton3.Checked == true && comboBox1.Text == "")
{
MessageBox.Show("请选择折扣率!");
return;
}
if (comboBox1.Text != "")
{
try
{
cboValues = int.Parse(comboBox1.Text);
}
catch
{
MessageBox.Show("请选择正确数字!5-9之间");
return;
}
}
if (cboValues < 5 || cboValues > 9)
{
MessageBox.Show("请选择正确数字!5-9之间");
return;
}
//初始化一个Label
Label l = (Label)sender;
if (l.BackColor == Color.Red)
{
MessageBox.Show("改座位已售出,请选择其他座位!");
return;
}
DialogResult result = MessageBox.Show("确定要购买?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
if (result == DialogResult.OK)
{
//初始化一个Seat
Seat s = new Seat(l.Text);
//s.Color = Color.Red;
cinema.Seats[l.Text].Color = Color.Red;
l.BackColor = cinema.Seats[l.Text].Color;
//循环遍历放映场次集合
foreach (ScheduleItem scdi in sc.Items.Values)
{
//判断取出本场次
if (scdi.Time == lblTime.Text)
{
//将取得的本场次信息scdi,座位信息s,按照radioButton的Text传递给票务简单工厂,进行对象创建
if (radioButton1.Checked == true)
{
lblNewPrice.Text = (tf.ticketOOP(scdi, s, int.Parse(comboBox1.Text), textBox1.Text, radioButton1.Text, cinema)).ToString();
}
if (radioButton2.Checked == true)
{
lblNewPrice.Text = (tf.ticketOOP(scdi, s, int.Parse(comboBox1.Text), textBox1.Text, radioButton2.Text, cinema)).ToString();
}
if (radioButton3.Checked == true)
{
lblNewPrice.Text = (tf.ticketOOP(scdi, s, int.Parse(comboBox1.Text), comboBox1.Text, radioButton3.Text, cinema)).ToString();
}
break;
}
}
}
}
/// <summary>
/// 窗体加载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MainFrom_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 单选按钮更改事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
textBox1.Text = "";
comboBox1.Text = "5";
if (radioButton1.Checked == true)
{
comboBox1.Enabled = false;
textBox1.Enabled = false;
}
if (radioButton2.Checked == true)
{
comboBox1.Enabled = false;
textBox1.Enabled = true;
}
if (radioButton3.Checked == true)
{
comboBox1.Enabled = true;
textBox1.Enabled = false;
}
}
/// <summary>
/// 保存按钮事件,进行序列化保存
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
cinema.Save();
}
/// <summary>
/// 继续销售事件,读取已保存的座位信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 继续销售ToolStripMenuItem_Click(object sender, EventArgs e)
{
cinema.Load();
sc.LoadItems(tvCinema);
tabControl2.TabPages[0].Controls.Clear();
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -