表格控件的绑定以及事件绑定

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Threading.Tasks;
  9 using System.Windows.Forms;
 10 
 11 namespace Tablelayout
 12 {
 13     public partial class Form1 : Form
 14     {
 15         public Form1()
 16         {
 17             InitializeComponent();
 18         }
 19 
 20         private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
 21         {
 22 
 23         }
 24 
 25         private void Form1_Load(object sender, EventArgs e)
 26         {
 27             //绑定Tablelayout事件
 28             BangdingTablelayout();
 29         }
 30 
 31         private void BangdingTablelayout()
 32         {
 33             //清空tablelayout格式
 34             this.tblp_DataInfos.Controls.Clear();
 35             this.tblp_DataInfos.ColumnCount = 0;
 36             this.tblp_DataInfos.ColumnStyles.Clear();
 37             this.tblp_DataInfos.RowCount = 0;
 38             this.tblp_DataInfos.RowStyles.Clear();
 39             //指定表格大小
 40             this.tblp_DataInfos.Width = 608;
 41             this.tblp_DataInfos.Height = 240;
 42             this.tblp_DataInfos.ColumnCount = 10;
 43             //指定每列宽度
 44             //ColumnStyle cs = new ColumnStyle(SizeType.Absolute, 60.00f);
 45             this.tblp_DataInfos.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 60.00f));
 46             this.tblp_DataInfos.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 60.00f));
 47             this.tblp_DataInfos.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 60.00f));
 48             this.tblp_DataInfos.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 60.00f));
 49             this.tblp_DataInfos.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 60.00f));
 50             this.tblp_DataInfos.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 60.00f));
 51             this.tblp_DataInfos.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 60.00f));
 52             this.tblp_DataInfos.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 60.00f));
 53             this.tblp_DataInfos.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 60.00f));
 54             this.tblp_DataInfos.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 60.00f));
 55             
 56 
 57             for (int i=0;i<8;i++)
 58             {
 59                 for(int j=0;j<10;j++)
 60                 {
 61                     //Label lblname = new Label();
 62                     //实例化button对象,指定button对象名称和显示文本,并绑定click事件方法名:lblname_click
 63                     Button lblname = new Button();
 64                     lblname.Name="btn"+ ((j + 1) + (i * 10)).ToString();
 65                     lblname.Text = ((j + 1) + (i * 10)).ToString();
 66                     lblname.Click += new System.EventHandler(this.lblname_click);
 67                     //给表格对象的每个单元格绑定控件
 68                     this.tblp_DataInfos.Controls.Add(lblname, j, i);
 69                 }
 70             }
 71         }
 72 
 73         //button click事件对应的方法功能实现
 74         private void lblname_click(object sender, EventArgs e)
 75         {
 76             
 77             int sum = 0;
 78             //sender转换为button对象,获得text属性的值之后转换成字符串格式
 79             if (((Button)sender).Text.ToString() == "10")
 80             {
 81                 for (int i=0;i<10;i++)
 82                 {
 83                     /*
 84                     for (int j = 0; j < 8; j++)
 85                     {
 86                         sum = sum + Convert.ToInt32(((Button)(this.Controls.Find("btn" + ((j + 1) + (i * 10)).ToString(), true)[0])).Text);
 87                     }*/
 88                     //实现1到i求和
 89                     sum = sum + Convert.ToInt32(((Button)(this.Controls.Find("btn" + (i + 1).ToString(), true)[0])).Text);
 90                 }
 91                 MessageBox.Show("1到"+ ((Button)sender).Text+"的和是"+sum.ToString());
 92 
 93                 if (sum % 2 == 0)
 94                 {
 95 
 96                     MessageBox.Show(sum+"是偶数");
 97                                     }
 98                 MessageBox.Show(sum + "是奇数");
 99                 //return;
100             }
101             //MessageBox.Show(((Button)sender).Text.ToString());
102             
103             //int num = Convert.ToInt32(((Button)sender).Text);
104             if (Convert.ToInt32(((Button)sender).Text) % 2 == 0)
105             {
106                
107                 MessageBox.Show(((Button)sender).Text+"是偶数");
108                 return;
109             }
110             MessageBox.Show(((Button)sender).Text+"是奇数");
111         }
112     }
113 }

 

posted @ 2019-06-26 13:16  淡蓝星空  阅读(379)  评论(0编辑  收藏  举报