usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespacexiaojian2{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); } Private voidForm1_Load (Objectsender, EventArgs e) { } Private voidButton1_Click (Objectsender, EventArgs e) { This. SplitContainer1.Panel2.Controls.Clear (); for(inti =0; I < -; i++) { //Form1 xx = new Form1 (); //xx. Show ();Button BTN =NewButton () {Name ="btn"+ I, Text ="Test"+ I, Size =NewSize ( -, -) }; Btn. Location=NewPoint ( -* (i%Ten), -* (I/Ten)); Btn. MouseDown+=Button1_mousedown; Btn. MouseMove+=Button1_mousemove; This. SPLITCONTAINER1.PANEL2.CONTROLS.ADD (BTN);//Add the generated button to the foreground page,}} Point pt; Private voidButton1_mousedown (Objectsender, MouseEventArgs e) {PT=cursor.position; } Private voidButton1_mousemove (Objectsender, MouseEventArgs e) { if(MouseButtons = =mousebuttons.left) {intpx = cursor.position.x-Pt. X intPY = CURSOR.POSITION.Y-Pt. Y Button Butt= (Button) sender;//gets which button is, and convertsButt. Location=NewPoint (Butt. location.x + px, butt. LOCATION.Y +py); PT=cursor.position; } } }}
20150506-winform Auto-Generate button & button drag