C#实现简易点餐功能
作者:小白你咋让人拴住了 时间:2021-10-20 22:14:31
本文实例为大家分享了C#实现简易点餐功能的具体代码,供大家参考,具体内容如下
图示效果
实现过程
1.设计界面
2.设计控件及其属性
3.实现点击事件、显示事件以及运算
4实现功能
代码如下
private void Form1_Load(object sender, EventArgs e)
{
// 给FlowLayoutPanel控件添加tag属性,编号为index, 菜名为name,价格为money
//根据菜品的数量 添加对应的序号按钮
foreach (Control item in flowLayoutPanel1.Controls)//找到菜单中所有的panel控件
{ //就是创建Button对象 使用关键new创建对象
Button btn = new Button(); // 实例化button对象
// 将每一个子控件赋值给对应按钮的Tag属性
btn.Tag = item;
btn.Size = new Size(50, 25);//设置点击按钮的大小
btn.Font = new Font("宋体", 18F);//设置按钮显示字体及其字体大小
foreach (Control control in item.Controls)
{
//根据控件的Tag属性值来判断以及获取对应控件中的文本从而设置给btn.Text
// 如果Tag为index 那就是编号
if (control.Tag.ToString() == "index")
{
btn.Text = control.Text;
}
}
flowLayoutPanel2.Controls.Add(btn);
//加载时显示按钮
btn.Click += Btn_Click; // 添加点击事件+=
}
}
int count = 0; // 价格金额
private void Btn_Click(object sender, EventArgs e)
{
Button clickBtn = (Button)sender;
Label lab = new Label();
//获取菜单中每一个子控件中的菜名与价格
// 获取到编号按钮对应的菜单子控件
Control menu = (Control)clickBtn.Tag;
// 遍历的是菜单中的子控件
foreach (Control item in menu.Controls)
{
if (item.Tag.ToString() == "name")
{
lab.Text = item.Text;
}
if (item.Tag.ToString() == "money")
{
//int.Parse(需要转换的字符串)
//将字符串转换为数字的方法
count += int.Parse(item.Text);
}
}
flowLayoutPanel3.Controls.Add(lab);//(点击事件)显示点击过的菜名
label18.Text = count + "元";//总共点过菜的金额总和
}
来源:https://blog.csdn.net/weixin_44024993/article/details/105623320
标签:C#,点餐
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java中的static关键字修饰属性和方法(推荐)
2021-09-29 05:46:20
![](https://img.aspxhome.com/file/2023/5/66035_0s.jpg)
Java 7大常见排序方法实例详解
2022-01-09 05:16:46
Java通俗易懂系列设计模式之装饰模式
2023-08-07 15:41:28
![](https://img.aspxhome.com/file/2023/4/83414_0s.png)
C# TcpClient网络编程传输文件的示例
2021-10-16 16:01:13
![](https://img.aspxhome.com/file/2023/2/81062_0s.jpg)
Java和C#输入输出流的方法(详解)
2022-06-24 09:21:02
使用String类型小数值转换为Long类型
2023-04-14 10:34:56
Java常量池详解
2023-05-19 19:36:27
浅谈Java工程读取resources中资源文件路径的问题
2021-07-20 19:13:45
![](https://img.aspxhome.com/file/2023/8/67668_0s.png)
JavaWeb项目Servlet无法访问问题解决
2022-10-10 03:33:52
Spring Boot支持Crontab任务改造的方法
2023-08-08 20:20:24
java实现多线程的两种方式继承Thread类和实现Runnable接口的方法
2022-07-24 04:27:18
c#基于WinForm的Socket实现简单的聊天室 IM
2021-11-27 04:47:57
![](https://img.aspxhome.com/file/2023/6/81066_0s.png)
详解JAVA Spring 中的事件机制
2022-01-13 05:26:33
Spring Cloud项目前后端分离跨域的操作
2022-05-20 08:11:16
Mybatis如何解决sql中like通配符模糊匹配问题
2023-12-22 19:39:52
如何基于java实现Gauss消元法过程解析
2023-12-15 21:51:08
![](https://img.aspxhome.com/file/2023/3/66953_0s.png)
C#中String StringBuilder StringBuffer类的用法
2023-05-21 02:35:50
![](https://img.aspxhome.com/file/2023/6/91346_0s.png)
Spring Boot中操作使用Redis实现详解
2023-11-24 02:39:29
![](https://img.aspxhome.com/file/2023/9/85399_0s.png)
java 如何实现正确的删除集合中的元素
2022-08-03 17:44:31
![](https://img.aspxhome.com/file/2023/0/67680_0s.jpg)
Java编程用栈来求解汉诺塔问题的代码实例(非递归)
2023-01-13 21:41:25