码迷,mamicode.com
首页 > 其他好文 > 详细

lab7源代码

时间:2015-04-19 21:20:28      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

  1 //--------------form1.cs
  2 using System;
  3 using System.Collections.Generic;
  4 using System.ComponentModel;
  5 using System.Data;
  6 using System.Drawing;
  7 using System.Linq;
  8 using System.Text;
  9 using System.Windows.Forms;
 10 using System.Threading;
 11 
 12 
 13 namespace 点菜
 14 {
 15     public partial class Form1 : Form
 16     {
 17         public Form1()
 18         {
 19             InitializeComponent();
 20         }
 21         private void Form1_Load(object sender, EventArgs e)        //窗体加载过程
 22         {
 23             //窗体属性初始化
 24             this.Text = "点菜系统";
 25             this.StartPosition = FormStartPosition.CenterScreen;
 26 
 27             //标签属性初始化
 28             label1.Text = "菜品类名";
 29             label2.Text = "菜名";
 30             label3.Text = "已点菜";
 31 
 32             //列表视图属性初始化
 33             listView1.View = listView2.View = View.Details;
 34             listView1.GridLines = listView2.GridLines = true;
 35             listView1.Columns.Add("菜名", 150); listView1.Columns.Add("单价", 90);
 36             listView2.Columns.Add("菜名", 100); listView2.Columns.Add("单价", 70); listView2.Columns.Add("份数", 70);
 37 
 38             //列表视图属性初始化
 39             treeView1.Nodes.Add("菜名");
 40             treeView1.Nodes[0].Nodes.Add("蔬菜类"); treeView1.Nodes[0].Nodes.Add("荤菜类"); treeView1.Nodes[0].Nodes.Add("汤类类");
 41         }
 42 
 43         private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)   //在对应的类中显示具体项目
 44         {
 45             listView1.Items.Clear();
 46             if (treeView1.Nodes[0].Nodes[0].IsSelected)
 47             {
 48                 listView1.Items.Add("大白菜"); listView1.Items[0].SubItems.Add("6");
 49                 listView1.Items.Add("土豆丝"); listView1.Items[1].SubItems.Add("12");
 50                 listView1.Items.Add("空心菜"); listView1.Items[2].SubItems.Add("18");
 51                 listView1.Items.Add("日本豆腐"); listView1.Items[3].SubItems.Add("24");
 52             }
 53 
 54             if (treeView1.Nodes[0].Nodes[1].IsSelected)
 55             {
 56                 listView1.Items.Add("红烧肉"); listView1.Items[0].SubItems.Add("8");
 57                 listView1.Items.Add("宫保鸡丁"); listView1.Items[1].SubItems.Add("14");
 58                 listView1.Items.Add("红烧鲫鱼"); listView1.Items[2].SubItems.Add("20");
 59                 listView1.Items.Add("水煮鱼"); listView1.Items[3].SubItems.Add("26");
 60             }
 61 
 62             if (treeView1.Nodes[0].Nodes[2].IsSelected)
 63             {
 64                 listView1.Items.Add("西红柿鸡蛋汤"); listView1.Items[0].SubItems.Add("3");
 65                 listView1.Items.Add("紫菜蛋汤"); listView1.Items[1].SubItems.Add("7");
 66                 listView1.Items.Add("三鲜汤"); listView1.Items[2].SubItems.Add("11");
 67                 listView1.Items.Add("鱼头豆腐汤"); listView1.Items[3].SubItems.Add("15");
 68             }
 69         }
 70 
 71         private void 点菜ToolStripMenuItem_Click(object sender, EventArgs e)     //点菜事件
 72         {
 73             try
 74             {
 75                 ListViewItem item = listView1.SelectedItems[0];   //考虑到没有选的情况
 76             }
 77             catch (Exception)
 78             {
 79                 MessageBox.Show("请先选菜!","提示");
 80                 return;
 81             }
 82             for (int i = 0; i <listView2.Items.Count; i++)   //先看看是否重复
 83             {
 84                 if (listView2.Items[i].Text == listView1.SelectedItems[0].Text)
 85                 {
 86                     int num = int.Parse(listView2.Items[i].SubItems[2].Text);
 87                     num++;
 88                     listView2.Items[i].SubItems[2].Text = num.ToString();
 89                     return;
 90                 }
 91             }
 92             listView2.Items.Add(listView1.SelectedItems[0].Text);
 93             listView2.Items[listView2.Items.Count - 1].SubItems.Add(listView1.SelectedItems[0].SubItems[1].Text);
 94             listView2.Items[listView2.Items.Count - 1].SubItems.Add("1");
 95         }   
 96 
 97         private void 清空ToolStripMenuItem_Click(object sender, EventArgs e)  //清空事件
 98         {
 99             listView1.Items.Clear();
100             listView2.Items.Clear();
101         }
102 
103         private void 全部删除ToolStripMenuItem_Click(object sender, EventArgs e)  //全部删除事件
104         {
105             listView2.Items.Clear();
106         }
107 
108         private void 删除一份ToolStripMenuItem_Click(object sender, EventArgs e)  //删除一份事件
109         {
110             try
111             {
112                 ListViewItem item = listView2.SelectedItems[0];   //考虑到没有选中的情况
113             }
114             catch (Exception)
115             {
116                 MessageBox.Show("请选择要删除的菜!","提示");
117                 return;
118             }
119             int num = int.Parse(listView2.SelectedItems[0].SubItems[2].Text);
120             num = num - 1;
121 
122             if (num == 0) listView2.Items.Remove(listView2.SelectedItems[0]);
123             else listView2.SelectedItems[0].SubItems[2].Text = num.ToString();
124            
125         }
126 
127         private void 结账ToolStripMenuItem_Click(object sender, EventArgs e)    //结账按钮事件
128         {
129             if (listView2.Items.Count > 0)
130             {
131                 Form2 f = new Form2();
132                 int all = 0, alone;
133                 for (int i = 0; i < listView2.Items.Count; i++)
134                 {
135                     alone = int.Parse(listView2.Items[i].SubItems[1].Text) * int.Parse(listView2.Items[i].SubItems[2].Text);
136                     f.add(listView2.Items[i].Text, alone);
137                     all += alone;
138                 }
139                 f.kong(all);
140                 this.Hide();
141                 f.ShowDialog();
142                 this.Show();
143                 
144             }
145             else MessageBox.Show("请先选菜!", "提示");
146           
147         }
148 
149         private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)    //退出按钮事件
150         {
151             this.Hide();
152             Form f=new Form();
153             f.Width = 500;
154             f.Height = 300;
155             f.StartPosition = FormStartPosition.CenterScreen;
156             f.FormBorderStyle = FormBorderStyle.None;
157             Label a = new Label();
158             a.AutoSize = false;
159             a.Width = 300;
160             a.Height = 100;
161             a.Left = 150;
162             a.Top = 150;
163             a.Text = "谢谢品尝!";
164             Font pp = new System.Drawing.Font("楷体", 30);
165             a.Font = pp;
166             a.ForeColor = Color.Blue;
167             f.Controls.Add(a);
168             f.Show();
169             Thread.Sleep(2000);
170             f.Close();
171             
172             Application.Exit();
173         }
174 
175 
176     }
177 }
 1 //---------------form2.cs
 2 using System;
 3 using System.Collections.Generic;
 4 using System.ComponentModel;
 5 using System.Data;
 6 using System.Drawing;
 7 using System.Linq;
 8 using System.Text;
 9 using System.Windows.Forms;
10 
11 namespace 点菜
12 {
13     public partial class Form2 : Form
14     {
15         public Form2()
16         {
17             InitializeComponent();
18         }
19 
20         private void Form2_Load(object sender, EventArgs e)
21         {
22             button1.Text = "返回";
23             button2.Text = "退出";
24             listView1.View = View.Details;
25             listView1.GridLines = true;
26             this.StartPosition = FormStartPosition.CenterScreen;
27             listView1.Columns.Add("菜名" ,100);
28             listView1.Columns.Add("价格(元)",100);
29         }
30 
31         public void add(string name,int count) {
32             listView1.Items.Add(name);
33             listView1.Items[listView1.Items.Count-1].SubItems.Add(count.ToString());
34         }
35 
36         public void kong(int all) { 
37             for(int i=0;i<=3;i++) listView1.Items.Add("");
38             listView1.Items.Add("总价:");
39             listView1.Items[listView1.Items.Count - 1].SubItems.Add(all.ToString());
40         }
41  
42         private void button2_Click(object sender, EventArgs e)
43         {
44             Application.Exit();
45         }
46 
47         private void button1_Click(object sender, EventArgs e)
48         {
49             this.Close();
50         }
51     }
52 }

 

lab7源代码

标签:

原文地址:http://www.cnblogs.com/2014fhj/p/4439890.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!