即日起在codingBlog上分享您的技术经验即可获得积分,积分可兑换现金哦。

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

编程语言 satlor_twain 38℃ 0评论

增加和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);


}


}


}


}


}


}

转载请注明:CodingBlog » C#BUTTON控件的增加与delete删除

喜欢 (0)or分享 (0)
发表我的评论
取消评论

*

表情