C#實(shí)現(xiàn)簡易點(diǎn)餐功能
更新時間:2021年07月21日 15:12:55 作者:小白你咋讓人拴住了
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)簡易點(diǎn)餐功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了C#實(shí)現(xiàn)簡易點(diǎn)餐功能的具體代碼,供大家參考,具體內(nèi)容如下
圖示效果
實(shí)現(xiàn)過程
1.設(shè)計(jì)界面
2.設(shè)計(jì)控件及其屬性
3.實(shí)現(xiàn)點(diǎn)擊事件、顯示事件以及運(yùn)算
4實(shí)現(xiàn)功能
代碼如下
private void Form1_Load(object sender, EventArgs e) { // 給FlowLayoutPanel控件添加tag屬性,編號為index, 菜名為name,價格為money //根據(jù)菜品的數(shù)量 添加對應(yīng)的序號按鈕 foreach (Control item in flowLayoutPanel1.Controls)//找到菜單中所有的panel控件 { //就是創(chuàng)建Button對象 使用關(guān)鍵new創(chuàng)建對象 Button btn = new Button(); // 實(shí)例化button對象 // 將每一個子控件賦值給對應(yīng)按鈕的Tag屬性 btn.Tag = item; btn.Size = new Size(50, 25);//設(shè)置點(diǎn)擊按鈕的大小 btn.Font = new Font("宋體", 18F);//設(shè)置按鈕顯示字體及其字體大小 foreach (Control control in item.Controls) { //根據(jù)控件的Tag屬性值來判斷以及獲取對應(yīng)控件中的文本從而設(shè)置給btn.Text // 如果Tag為index 那就是編號 if (control.Tag.ToString() == "index") { btn.Text = control.Text; } } flowLayoutPanel2.Controls.Add(btn); //加載時顯示按鈕 btn.Click += Btn_Click; // 添加點(diǎn)擊事件+= } } int count = 0; // 價格金額 private void Btn_Click(object sender, EventArgs e) { Button clickBtn = (Button)sender; Label lab = new Label(); //獲取菜單中每一個子控件中的菜名與價格 // 獲取到編號按鈕對應(yīng)的菜單子控件 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(需要轉(zhuǎn)換的字符串) //將字符串轉(zhuǎn)換為數(shù)字的方法 count += int.Parse(item.Text); } } flowLayoutPanel3.Controls.Add(lab);//(點(diǎn)擊事件)顯示點(diǎn)擊過的菜名 label18.Text = count + "元";//總共點(diǎn)過菜的金額總和 }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#使用Windows Service的簡單教程(創(chuàng)建、安裝、卸載、調(diào)試)
這篇文章主要為大家詳細(xì)介紹了C#創(chuàng)建、安裝、卸載、調(diào)試Windows Service(Windows 服務(wù))的簡單教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01NGUI實(shí)現(xiàn)滑動翻頁效果實(shí)例代碼
本文通過一段實(shí)例代碼給大家介紹NGUI實(shí)現(xiàn)滑動翻頁效果,代碼簡單易懂,對ngui 滑動翻頁相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧2016-04-04MessageBox的Buttons和三級聯(lián)動效果
這篇文章主要介紹了MessageBox的Buttons和三級聯(lián)動的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-11-11