该文采用C#编写winform窗体应用程序,实现一个简单的电影院购票系统,其中有不少的使用技巧可能值得初学者学习:定时器的使用、进度条的实现、不同windows窗体的切换、txt写入内容保存、txt内容的读取…… 下面将介绍具体的实现。 初始化,使用定时器结合进度条的使用,效果图如下:
然后是登录界面:
进入以后分为用户模块和管理模块:
首先是管理模块对电影信息进行填写,在用户模块就能对添加的电影进行购票操作(包括作为的选择)。 涉及的源码如下:
//进度条的实现
for (int i = 0; i <= 100; i++)
{
progressBar1.Value = i;
System.Threading.Thread.Sleep(30);
}
//窗体之间的切换
ProtectSystem form1 = new ProtectSystem();
form1.Show();
this.Hide();
//添加电影信息(写入txt文档)
private void Movies_Add_Click(object sender, EventArgs e)
{
string path = @"F:\a";
;
try
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
}
catch (Exception ee)
{
Console.WriteLine("The process failed: {0}", ee.ToString());
}
if (Movies_Name.Text == "" || Movies_Time.Text == "" || Movies_Place.Text==""||
Movies_Price.Text == "" || Movies_Picture.Text == "" || Movies_Introduction.Text=="")
{
MessageBox.Show("请完善信息!");
}
//写入信息
else
{
if (!File.Exists("F:\\a\\Movie1.txt"))
{
StreamWriter strmsave = new StreamWriter("F:\\a\\Movie1.txt", false, System.Text.Encoding.Default); ;//创建写入文件
strmsave.Write("1/" + this.Movies_Name.Text + "|" + this.Movies_Time.Text + "|" + this.Movies_Place.Text
+ "|" + this.Movies_Price.Text + "|" +
this.Movies_Picture.Text + "|" + this.Movies_Introduction.Text);//开始写入值
StreamWriter strmsave_table = new StreamWriter("F:\\a\\Movie1_Table1.txt", false, System.Text.Encoding.Default); ;//创建写入文件
strmsave_table.Write("1/0|0|0|0|0|0|0|0|0|0");//开始写入值
strmsave_table.Close();
strmsave.Close();
}
else
if (!File.Exists("F:\\a\\Movie2.txt"))
{
StreamWriter strmsave = new StreamWriter("F:\\a\\Movie2.txt", false, System.Text.Encoding.Default); ;//创建写入文件
strmsave.Write("1/" + this.Movies_Name.Text + "|" + this.Movies_Time.Text + "|" + this.Movies_Place.Text
+ "|" + this.Movies_Price.Text + "|" +
this.Movies_Picture.Text + "|" + this.Movies_Introduction.Text);//开始写入值
StreamWriter strmsave_table = new StreamWriter("F:\\a\\Movie1_Table2.txt", false, System.Text.Encoding.Default); ;//创建写入文件
strmsave_table.Write("1/0|0|0|0|0|0|0|0|0|0");//开始写入值
strmsave_table.Close();
strmsave.Close();
}
else
if (!File.Exists("F:\\a\\Movie3.txt"))
{
StreamWriter strmsave = new StreamWriter("F:\\a\\Movie3.txt", false, System.Text.Encoding.Default); ;//创建写入文件
strmsave.Write("1/" + this.Movies_Name.Text + "|" + this.Movies_Time.Text + "|" + this.Movies_Place.Text
+ "|" + this.Movies_Price.Text + "|" +
this.Movies_Picture.Text + "|" + this.Movies_Introduction.Text);//开始写入值
StreamWriter strmsave_table = new StreamWriter("F:\\a\\Movie1_Table3.txt", false, System.Text.Encoding.Default); ;//创建写入文件
strmsave_table.Write("1/0|0|0|0|0|0|0|0|0|0");//开始写入值
strmsave_table.Close();
strmsave.Close();
}
else
if (!File.Exists("F:\\a\\Movie4.txt"))
{
StreamWriter strmsave = new StreamWriter("F:\\a\\Movie4.txt", false, System.Text.Encoding.Default); ;//创建写入文件
strmsave.Write("1/" + this.Movies_Name.Text + "|" + this.Movies_Time.Text + "|" + this.Movies_Place.Text
+ "|" + this.Movies_Price.Text + "|" +
this.Movies_Picture.Text + "|" + this.Movies_Introduction.Text);//开始写入值
StreamWriter strmsave_table = new StreamWriter("F:\\a\\Movie1_Table4.txt", false, System.Text.Encoding.Default); ;//创建写入文件
strmsave_table.Write("1/0|0|0|0|0|0|0|0|0|0");//开始写入值
strmsave_table.Close();
strmsave.Close();
}
else
if (!File.Exists("F:\\a\\Movie5.txt"))
{
StreamWriter strmsave = new StreamWriter("F:\\a\\Movie5.txt", false, System.Text.Encoding.Default); ;//创建写入文件
strmsave.Write("1/" + this.Movies_Name.Text + "|" + this.Movies_Time.Text + "|" + this.Movies_Place.Text
+ "|" + this.Movies_Price.Text + "|" +
this.Movies_Picture.Text + "|" + this.Movies_Introduction.Text);//开始写入值
StreamWriter strmsave_table = new StreamWriter("F:\\a\\Movie1_Table5.txt", false, System.Text.Encoding.Default); ;//创建写入文件
strmsave_table.Write("1/0|0|0|0|0|0|0|0|0|0");//开始写入值
strmsave_table.Close();
strmsave.Close();
}
else
if (!File.Exists("F:\\a\\Movie6.txt"))
{
StreamWriter strmsave = new StreamWriter("F:\\a\\Movie6.txt", false, System.Text.Encoding.Default); ;//创建写入文件
strmsave.Write("1/" + this.Movies_Name.Text + "|" + this.Movies_Time.Text + "|" + this.Movies_Place.Text
+ "|" + this.Movies_Price.Text + "|" +
this.Movies_Picture.Text + "|" + this.Movies_Introduction.Text);//开始写入值
StreamWriter strmsave_table = new StreamWriter("F:\\a\\Movie1_Table6.txt", false, System.Text.Encoding.Default); ;//创建写入文件
strmsave_table.Write("1/0|0|0|0|0|0|0|0|0|0");//开始写入值
strmsave_table.Close();
strmsave.Close();
}
else
{
MessageBox.Show("内存不足!");
}
if(D_Movie1.Visible == false && DD_Movie1.Visible == false)
{
D_Movie1.Text = Movies_Name.Text + "---" +Movies_Place.Text + "---" + Movies_Time.Text;
D_Movie1.Visible = true;
DD_Movie1.Visible = true;
MessageBox.Show("影片添加成功!");
}
else
if(D_Movie2.Visible == false && DD_Movie2.Visible == false)
{
D_Movie2.Text = Movies_Name.Text + "---" +Movies_Place.Text + "---" + Movies_Time.Text;
D_Movie2.Visible = true;
DD_Movie2.Visible = true;
MessageBox.Show("影片添加成功!");
}
else
if(D_Movie3.Visible == false && DD_Movie3.Visible == false)
{
D_Movie3.Text = Movies_Name.Text + "---" +Movies_Place.Text + "---" + Movies_Time.Text;
D_Movie3.Visible = true;
DD_Movie3.Visible = true;
MessageBox.Show("影片添加成功!");
}
else
if(D_Movie4.Visible == false && DD_Movie4.Visible == false)
{
D_Movie4.Text = Movies_Name.Text + "---" +Movies_Place.Text + "---" + Movies_Time.Text;
D_Movie4.Visible = true;
DD_Movie4.Visible = true;
MessageBox.Show("影片添加成功!");
}
else
if(D_Movie5.Visible == false && DD_Movie5.Visible == false)
{
D_Movie5.Text = Movies_Name.Text + "---" +Movies_Place.Text + "---" + Movies_Time.Text;
D_Movie5.Visible = true;
DD_Movie5.Visible = true;
MessageBox.Show("影片添加成功!");
}
else
if(D_Movie6.Visible == false && DD_Movie6.Visible == false)
{
D_Movie6.Text = Movies_Name.Text + "---" +Movies_Place.Text + "---" + Movies_Time.Text;
D_Movie6.Visible = true;
DD_Movie6.Visible = true;
MessageBox.Show("影片添加成功!");
}
else
{
MessageBox.Show("内存不足!");
}
Movies_Name.Text = "";
Movies_Time.Text = "";
Movies_Place.Text = "";
Movies_Price.Text = "";
Movies_Picture.Text = "";
Movies_Introduction.Text = "";
}
}
//加载txt内写入的电影信息实现图如下:
if (File.Exists("F:\\a\\Movie1.txt"))
{
List<string> list_Get1 = Read("F:\\a\\Movie1.txt");
foreach (string s in list_Get1)
{
string[] arr1 = s.Split('/');
listKeys1.Add(arr1[0]);
string[] arr_value1 = arr1[1].Split('|');
dic1.Add(arr1[0], arr_value1);
}
A5.Text = dic1["1"][0];
A1.Text = dic1["1"][1] + "---" + dic1["1"][2];
A4.Image = Image.FromFile(str + "\\Image\\" + dic1["1"][4] + ".jpg");
StreamReader srr = new StreamReader("F:\\a\\Movie1.txt", Encoding.Default);
srr.Close();
}
然后在用户界面就能加载到在管理模块添加的信息:
private void BuySystem_Load(object sender, EventArgs e)
{
string str = System.Windows.Forms.Application.StartupPath;
if (File.Exists("F:\\a\\Movie1.txt"))
{
List<string> list_Get1 = Read("F:\\a\\Movie1.txt");
foreach (string s in list_Get1)
{
string[] arr1 = s.Split('/');
listKeys1.Add(arr1[0]);
string[] arr_value1 = arr1[1].Split('|');
dic1.Add(arr1[0], arr_value1);
}
A5.Text = dic1["1"][0];
A1.Text = dic1["1"][1] + "---" + dic1["1"][2];
A4.Image = Image.FromFile(str + "\\Image\\" + dic1["1"][4] + ".jpg");
StreamReader srr = new StreamReader("F:\\a\\Movie1.txt", Encoding.Default);
srr.Close();
}
if (File.Exists("F:\\a\\Movie2.txt"))
{
List<string> list_Get2 = Read("F:\\a\\Movie2.txt");
foreach (string s in list_Get2)
{
string[] arr2 = s.Split('/');
listKeys2.Add(arr2[0]);
string[] arr_value2 = arr2[1].Split('|');
dic2.Add(arr2[0], arr_value2);
}
B5.Text = dic2["1"][0];
B1.Text = dic2["1"][1] + "-" + dic2["1"][2];
B4.Image = Image.FromFile(str + "\\Image\\" + dic2["1"][4] + ".jpg");
StreamReader srr = new StreamReader("F:\\a\\Movie2.txt", Encoding.Default);
srr.Close();
}
if (File.Exists("F:\\a\\Movie3.txt"))
{
List<string> list_Get = Read("F:\\a\\Movie3.txt");
foreach (string s in list_Get)
{
string[] arr3 = s.Split('/');
listKeys3.Add(arr3[0]);
string[] arr_value3 = arr3[1].Split('|');
dic3.Add(arr3[0], arr_value3);
}
C1.Text = dic3["1"][1] + "-" + dic3["1"][2];
C4.Image = Image.FromFile(str + "\\Image\\" + dic3["1"][4] + ".jpg");
C5.Text = dic3["1"][0];
StreamReader srr = new StreamReader("F:\\a\\Movie3.txt", Encoding.Default);
srr.Close();
}
if (File.Exists("F:\\a\\Movie4.txt"))
{
List<string> list_Get = Read("F:\\a\\Movie4.txt");
foreach (string s in list_Get)
{
string[] arr4 = s.Split('/');
listKeys4.Add(arr4[0]);
string[] arr_value4 = arr4[1].Split('|');
dic4.Add(arr4[0], arr_value4);
}
D1.Text = dic4["1"][1] + "-" + dic4["1"][2];
D4.Image = Image.FromFile(str + "\\Image\\" + dic4["1"][4] + ".jpg");
D5.Text = dic4["1"][0];
StreamReader srr = new StreamReader("F:\\a\\Movie4.txt", Encoding.Default);
srr.Close();
}
if (File.Exists("F:\\a\\Movie5.txt"))
{
List<string> list_Get = Read("F:\\a\\Movie5.txt");
foreach (string s in list_Get)
{
string[] arr5 = s.Split('/');
listKeys5.Add(arr5[0]);
string[] arr_value5 = arr5[1].Split('|');
dic5.Add(arr5[0], arr_value5);
}
E1.Text = dic5["1"][1] + "-" + dic5["1"][2];
E4.Image = Image.FromFile(str + "\\Image\\" + dic5["1"][4] + ".jpg");
E5.Text = dic5["1"][0];
StreamReader srr = new StreamReader("F:\\a\\Movie5.txt", Encoding.Default);
srr.Close();
}
if (File.Exists("F:\\a\\Movie6.txt"))
{
List<string> list_Get = Read("F:\\a\\Movie6.txt");
foreach (string s in list_Get)
{
string[] arr6 = s.Split('/');
listKeys6.Add(arr6[0]);
string[] arr_value6 = arr6[1].Split('|');
dic6.Add(arr6[0], arr_value6);
}
F1.Text = dic6["1"][1] + "-" + dic6["1"][2];
F4.Image = Image.FromFile(str + "\\Image\\" + dic6["1"][4] + ".jpg");
F5.Text = dic6["1"][0];
StreamReader srr = new StreamReader("F:\\a\\Movie6.txt", Encoding.Default);
srr.Close();
}
}
视图效果如下:
最后是选座界面,效果如下:
源代码资源:http://download.csdn.net/download/laozhuxinlu/9381196