C#BUTTON控件的增加和delete删除

来源:互联网 发布:网络驱动精灵 编辑:程序博客网 时间:2024/06/09 21:56

增加和delete button控件
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 Form1 : Form
{
int i=0;
public Form1()
{
InitializeComponent();
this.KeyPreview = true;//这里一定要初始化键盘触发
}
//**“增加”按键点击事件**//
private void button1_Click(object sender, EventArgs e)
{
Button but = new Button();
if (Convert.ToInt32(line_txt.Text) > 0)
{
but.Location = new Point(2, 24 * (Convert.ToInt32(line_txt.Text)-1));
but.Text = “按键” + line_txt.Text;
but.Name = “key” + line_txt.Text;
textBox1.AppendText(“增加的是”+ but.Name.ToString());
textBox1.AppendText(“\r\n”);
}
else
{
but.Location = new Point(2, 24 * i);
i = i + 1;
but.Text = “按键” + i.ToString();
but.Name = “key” + i.ToString();
textBox1.AppendText(“增加的是” + but.Name.ToString());
textBox1.AppendText(“\r\n”);
}
this.Controls.Add(but);
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
for (int i = 0; i < this.Controls.Count;i++ )
{
Control control = this.Controls[i] as Control;
if (control.Focused)
{
this.Controls.Remove(control);
}
}
}
}
}
}

原创粉丝点击