您的位置:首页 > 编程语言 > C#

C#循环创建按钮,并可以通过点击判断出是哪个按钮

2017-07-23 12:52 591 查看
吐槽一下,网上找信息真是一门功夫

,做个小学期电影院订票系统(点击座位那种),不想一个个拉按钮就找个循环创建按钮就良莠不齐(我没系统学过C#),然后特么的找到循环创建你就没告诉我怎么监听那个按钮被点击啊好不好!C#双击出来的按钮but_Click函数总不能随便改名吧!

总之现在我终于掌握了这种“核心科技”

,在《ASP.NET项目开发详解》里找到了一句函数。

效果就是这样:





C#源码(就是你项目中的CS源码):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 小学期
{
public partial class Computers : Form
{
string bnID="";

void refurbish()
{
Button[] btn = new Button[21]; //生成按钮
int j = 0;
for (int i = 1; i <= 20; i++)//5列
{
if ((i - 1) % 5 == 0)
{ j++; }
btn[i] = new Button();
btn[i].Text = i.ToString();   //设置按钮显示的文字
btn[i].Location = new Point(75 * ((i - 1) % 5) + 30, 35 * j + 50);
btn[i].Name = "btn" + i.ToString();
btn[i].Height = 35;

//btn[i].BackColor = Color.GreenYellow;

this.Controls.Add(btn[i]);  //向前台页面添加生成的按钮,from1是你前台页面的ID
btn[i].Click += new System.EventHandler(this.btn_Click);
}
}

public Computers()
{
InitializeComponent();

//for (int i = 0; i < btn.Length; i++)
//{ btn[i].Click += new System.EventHandler(this.btn_Click); }//事件归一!

}

private void btn_Click(object sender, System.EventArgs e)
{
int i = int.Parse(((Button)sender).Text);//!!!!!!2017.7.23,终于看书找到了===============就是这句
this.Text = i.ToString();
}

private void button1_Click(object sender, EventArgs e)
{
refurbish();
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐