Effect:
In a winform project, add a ToolStrip and then add a ToolStripButton to it (tsbStart, which is the icon circled by the red box)
this.toolStripTools = new System.Windows.Forms.ToolStrip(); this.tsbStart = new System.Windows.Forms.ToolStripButton();
, when tsbStart is clicked and dragged to the workspace (the blank area in the lower part of the figure, it is a panel, and a usercontrol is dynamically added to the panel), the event WorkPlace_DragEnter(object sender, DragEventArgs e) is triggered when dragging into the workspace (usercontrol), put the mouse e.Effect = DragDropEffects.Copy;. When the drag is ended, when WorkPlace_DragDrop(object sender, DragEventArgs e) is monitored within the scope of the workspace (usercontroll), a control is dynamically added to the workspace to complete the drag effect.
Implementation code:
main form code:
public Main() { InitializeComponent(); } private void Main_Load(object sender, EventArgs e) { if (this.wpClient != null) wpClient.CloseFlow(); // When the form is reloaded, clear the controls in the form. this .plClient.Controls.Clear(); wpClient = new WorkPlace("1", UserId, UserName); wpClient.WorkFlowCaption = "Hello WorkFlow Designer..."; wpClient.CanEdit = true; wpClient.State = " Modify " ; // Add the wpClient object to the plClient control area. this .plClient.Controls.Add( this .wpClient); // The toolbar icon of the Main form takes effect and is available. this .toolStripTools.Enabled = true ; } private void tsbStart_MouseDown(object sender, MouseEventArgs e) { // If the left button is left, the flag is true (indicating that dragging starts) if ((e.Button == System.Windows.Forms.MouseButtons.Left)) { // Form a drag effect, a combined effect of move + copy Button button = new Button(); button.SetBounds(0, 0, 100, 20); button.Text = "button "; button.DoDragDrop(button, (DragDropEffects.Copy | DragDropEffects.Move)); // Form a drag effect, a combined effect of move + copy } }
workspace custom control code:
public WorkPlace() { InitializeComponent(); // If the structure drag event is allowed, it must be set to true, otherwise the drag event cannot be ended. // The custom control itself supports dragged controls this .AllowDrop = true ; this .DragEnter += new DragEventHandler(WorkPlace_DragEnter); this .DragDrop += new DragEventHandler(WorkPlace_DragDrop); } public WorkPlace(string workFlowId, string userId, string userName) : this() { this.WorkFlowId = workFlowId; this.UserId = userId; this.UserName = userName; } /// <summary> /// Close the form. When closing the form, you need to determine whether the current work content has been saved, otherwise a confirmation message will be prompted. /// </summary> public void CloseFlow() { } void WorkPlace_DragEnter(object sender, DragEventArgs e) { // When the Button is dragged onto the WinForm, the mouse effect appears if ((e.Data.GetDataPresent( typeof (Button)))) { e.Effect = DragDropEffects.Copy; } } int count = 0; void WorkPlace_DragDrop(object sender, DragEventArgs e) { if ((e.Data.GetDataPresent(typeof(Button)))) { // After dragging and dropping, a new control Button is automatically generated btn = new Button(); btn.SetBounds(e.X, e.Y, 100, 50); btn.Location = this .PointToClient( new Point(eX, eY)); // Use this method to calculate the X, Y coordinates of the client container interface. Otherwise use X directly, Y is the screen coordinate this .Controls.Add(btn); btn.Text = " Button " + count.ToString(); count = count + 1; } }
Precautions:
The property AllowDrop must be set to true inside the workplace custom control, otherwise the DragDown and DargDrop events will not be triggered.
this.AllowDrop = true;