我是这样做的“泛型类”--- 然后序列化 这个“泛型类” + 反序列化。
1)但是运行时报错: private void FrmSeat_Load(object sender, EventArgs e) 里的 ReadSeat()下面出现红线,
提示:“当前上下文中不存在名称为“ReadSeat()””
可是这个“ReadSeat()” 明明就在上面呀,到底哪出错了呢?
2) 我这样做对吗?
求老师大侠指教,多谢了!!!
=========================
public partial class FrmSeat : Form
{
public FrmSeat()
{
InitializeComponent();
}
[Serializable]
public class List<Seat>
{
private static void InitSeats(TabPage tp)
{
//用Label控件来表示座位
Label label;
//清空Label
tp.Controls.Clear();//tb 是 InitSeat (TabPage tb) 方法的参数
int line = 0;
int column = 0;
//if (txtLine.Text.Trim() !="" && txtColumn.Text.Trim() != "")
//{
//行
FrmSeat fs = new FrmSeat();
line = Convert.ToInt32(fs.txtLine.Text.Trim());
column = Convert.ToInt32(fs.txtColumn.Text.Trim());
// line = Convert.ToInt32(FrmSeat.txtLine.Text.Trim());
//列
//column = Convert.ToInt32(FrmSeat.txtColumn.Text.Trim());
//}
//添加座位
for (int i = 0; i < line; i++)
{
for (int j = 0; j <= column; j++)
{
label = new Label();
//设置背景颜色
label.BackColor = Color.Orange;
//设置尺寸
label.AutoSize = false;
label.Size = new System.Drawing.Size(35, 15);
//设置座位号
label.Text = (i + 1).ToString() + "-" + (j + 1).ToString();
label.TextAlign = ContentAlignment.MiddleCenter;
//设置边距位置
label.Location = new Point(50 + j * 38, 50 + (i * 25));
//所有的标签都绑定到同一事件
label.Click += new System.EventHandler(lblSeat_Click);//把lblSeat_Click()方法作为参数放进来
//添加Label
tp.Controls.Add(label);
}
}
}
private static void lblSeat_Click(object sender, EventArgs e) // 这个方法作为参数 放到new System.EventHandler(lblSeat_Click)- 实现该功能
{
if (((Label)sender).BackColor == Color.Red)
{
MessageBox.Show("此票已经卖出:" + ((Label)sender).Text.ToString(), "提示");
return;
}
MessageBox.Show("选择了:" + ((Label)sender).Text.ToString(), "提示");
//修改座位的颜色
((Label)sender).BackColor = Color.Red;
}
//序列化 - 保存生成的座位
public static void SerializeSeat(Seat seat)
{
List<Seat> seats = new List<Seat>();
using (FileStream fs = new FileStream("seats.bin", FileMode.Create))
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, seats);
}
}
// 反序列化 - 读取生成的座位
public List<Seat> ReadSeat()
// 需要给参数吗?
{
List<Seat> list = new List<Seat>();
if (!File.Exists("seats.bin"))
{
return list;
}
FileStream fs = new FileStream("seats.bin", FileMode.Open);
BinaryFormatter bf = new BinaryFormatter();
list = (List<Seat>)bf.Deserialize(fs);
fs.Close();
return list;
}
}
private void FrmSeat_Load(object sender, EventArgs e)
{
FrmSeat fs = new FrmSeat();
fs.txtLine.Text = null;
fs.txtColumn.Text = null;
ReadSeat();
}
}