日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

按钮事件的统一处理

發(fā)布時(shí)間:2025/4/5 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 按钮事件的统一处理 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

    • 1 按鈕事件的統(tǒng)一處理
      • 1.1 項(xiàng)目UI及所需實(shí)現(xiàn)的功能簡(jiǎn)要介紹
      • 1.2 功能實(shí)現(xiàn)

1 按鈕事件的統(tǒng)一處理

1.1 項(xiàng)目UI及所需實(shí)現(xiàn)的功能簡(jiǎn)要介紹

項(xiàng)目UI如下:

所需實(shí)現(xiàn)的功能:

  • 除“保存所選課程”按鈕外,其他的按鈕點(diǎn)擊事件統(tǒng)一處理。按鈕攜帶的信息通過(guò)Tag進(jìn)行傳遞。
  • 將所選擇的課程通過(guò)實(shí)體類(lèi)的封裝存儲(chǔ)到泛型集合中。
  • 1.2 功能實(shí)現(xiàn)

    首先來(lái)看下實(shí)體類(lèi)的代碼,非常簡(jiǎn)單:

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace xiketang.com.WinformBase {/// <summary>/// 課程實(shí)體類(lèi)/// </summary>public class Course{public Course() { }public Course(int courseId, string courseName, int classHour, string teacher){this.CourseId = courseId;this.CourseName = courseName;this.ClassHour = classHour;this.Teacher = teacher;}public int CourseId { get; set; }public string CourseName { get; set; }public int ClassHour { get; set; }//課時(shí)public string Teacher { get; set; }//主講老師} }

    窗體相關(guān)代碼如下:

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;namespace xiketang.com.WinformBase {public partial class FrmEventApp : Form{//用來(lái)封裝課程對(duì)象的容器private List<Course> courseList = new List<Course>();public FrmEventApp(){InitializeComponent();//多個(gè)按鈕響應(yīng)同一個(gè)事件,在此做事件關(guān)聯(lián)//this.btn01.Click += new System.EventHandler(this.btn_Click);//this.btn02.Click += new System.EventHandler(this.btn_Click);//this.btn03.Click += new System.EventHandler(this.btn_Click);//this.btn04.Click += new System.EventHandler(this.btn_Click);//this.btn05.Click += new System.EventHandler(this.btn_Click);//this.btn06.Click += new System.EventHandler(this.btn_Click);//this.btn07.Click += new System.EventHandler(this.btn_Click);//this.btn08.Click += new System.EventHandler(this.btn_Click);//this.btn09.Click += new System.EventHandler(this.btn_Click);//this.btn10.Click += new System.EventHandler(this.btn_Click);//this.btn11.Click += new System.EventHandler(this.btn_Click);//this.btn12.Click += new System.EventHandler(this.btn_Click);//以上方法,如果你這么寫(xiě)程序,會(huì)被別人認(rèn)為你什么都不懂!foreach (Control item in this.Controls){//if (item is Button)//通過(guò)控件類(lèi)型過(guò)濾我們不需要的控件//{// Button btn = item as Button;// if (btn.Tag.ToString() != "Save")//過(guò)濾我們不需要的按鈕,請(qǐng)大家特別注意Tag的使用// {// btn.Click += new System.EventHandler(this.btn_Click);// }//}if (item is Button && item.Tag.ToString() != "Save"){item.Click += new System.EventHandler(this.btn_Click);}}}//事件集中處理方法private void btn_Click(object sender, EventArgs e){Button btn = sender as Button;//將當(dāng)前按鈕Tag屬性中封裝的課程信息,通過(guò)字符串分割得到string[] info = btn.Tag.ToString().Split(',');//將當(dāng)前課程信息封裝到課程對(duì)象,并將課程對(duì)象封裝到集合中this.courseList.Add(new Course{CourseName = btn.Text,CourseId = Convert.ToInt32(info[0]),ClassHour = Convert.ToInt32(info[1])});//改變當(dāng)前按鈕的背景色btn.BackColor = Color.Green;//請(qǐng)大家思考:如果避免用戶(hù)多次添加同一個(gè)課程按鈕,而導(dǎo)致多次添加的問(wèn)題...}//保存所選課private void btnSave_Click(object sender, EventArgs e){//實(shí)際開(kāi)發(fā)中,保存可以到數(shù)據(jù)庫(kù)、文件...//測(cè)試看看所選擇的課程foreach (var item in this.courseList){Console.WriteLine(item.CourseId+"\t"+item.ClassHour+"\t"+item.CourseName);}}} }

    參考資料:

  • .NET/C#工控上位機(jī)VIP系統(tǒng)學(xué)習(xí)班【喜科堂互聯(lián)教育】
  • 總結(jié)

    以上是生活随笔為你收集整理的按钮事件的统一处理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。