标签:
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 lab10
{
public partial class Form1 : Form
{
/*保存控件数*/
private static int countOfButton = 1;
private static int countOfCheckBox = 1;
private static int countOfTextBox = 1;
public Form1()
{
InitializeComponent();
}
/*窗体加载事件*/
private void Form1_Load(object sender, EventArgs e)
{
button2.Text = "清空";
button3.Text = "复制";
panel1.BorderStyle = BorderStyle.Fixed3D;
this.Width = 800;
this.Height = 500;
panel1.Width = 500;
}
//清空按钮事件
private void button2_Click(object sender, EventArgs e)
{
panel1.Controls.Clear();
}
//按钮复制事件
private void button1_Click(object sender, EventArgs e)
{
Button button = new Button();
button.Text = "button"+countOfButton.ToString();
panel1.Controls.Add(button);
button.Top = (countOfButton -1)* 30;
countOfButton++;
button.MouseMove += new MouseEventHandler(changeButtonLocation);
button.Cursor = Cursors.Cross;
}
//复选框复制
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
CheckBox checkBox = new CheckBox();
checkBox.Text = "checkBox" +countOfCheckBox.ToString();
panel1.Controls.Add(checkBox);
checkBox.Top = (countOfCheckBox - 1) * 30;
checkBox.Left = 100;
countOfCheckBox++;
checkBox.MouseMove+=new MouseEventHandler(changeCheckBoxLocation);
checkBox.Cursor = Cursors.Cross;
}
//文本框复制
private void textBox1_Click(object sender, EventArgs e)
{
TextBox textBox = new TextBox();
textBox.Text = "textBox" + countOfTextBox.ToString();
panel1.Controls.Add(textBox);
textBox.Top = (countOfTextBox - 1) * 30;
textBox.Left = 200;
countOfTextBox++;
textBox.MouseMove+=new MouseEventHandler(changeTexBoxLocation); //事件注册
textBox.Cursor = Cursors.Cross;
}
//panel复制事件
private void button3_Click(object sender, EventArgs e)
{
Form f1 =new Form();
f1.Width = 800;
f1.Height = 500;
f1.Show();
f1.Controls.Add(panel1);
}
//改变按钮位置
private void changeButtonLocation(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left) {
Button button = sender as Button;
button.Location = new Point(MousePosition.X-this.Location.X-panel1.Location.X-20, MousePosition.Y-this.Location.Y-panel1.Location.Y-35);
}
}
//改变复选框位置
private void changeCheckBoxLocation(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
CheckBox checkBox = sender as CheckBox;
checkBox.Location = new Point(MousePosition.X - this.Location.X - panel1.Location.X - 20, MousePosition.Y - this.Location.Y - panel1.Location.Y - 35);
}
}
//改变文本框位置
private void changeTexBoxLocation(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
TextBox textBox = sender as TextBox;
textBox.Location = new Point(MousePosition.X - this.Location.X - panel1.Location.X - 20, MousePosition.Y - this.Location.Y - panel1.Location.Y - 35);
}
}
}
}
标签:
原文地址:http://www.cnblogs.com/2014fhj/p/4564559.html