我正在使用C#在Windows窗體項目中使用groupbox
作爲容器並向其中添加項(Labels
,pictureBoxes
...)。我想知道是否可以在此控件上實現拖放事件以使用鼠標移動項目,或者如果必須在添加到我的groupBox
的控件中實現此事件。在組盒子中拖拽
Q
在組盒子中拖拽
0
A
回答
1
關於我的話題了一些研究之後,我發現與我的一個朋友幫助解決。爲了完成這個任務,我們需要使用一些變量,屬性和三個鼠標事件來分配給這些對象:MouseMove,MouseUp和MouseDown。我最近也發現了一個例子:http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/6eb864ff-0ea8-4641-bc2a-83db94371429
0
這段代碼是用來複制一個控件(在這裏我們已經完成了Button的操作,你可以通過改變Button類到DragDrop事件中的任何其他類)到一個groupbox。
首先將Groupbox的「AllowDrop」屬性設置爲true。
groupBox5.AllowDrop=true;
從屬性窗口創建GROUPBOX一個 「dragEnter事件」 事件
private void groupBox5_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
接下來,從屬性窗口創建GROUPBOX一個 「的DragDrop」 事件
private void groupBox5_DragDrop(object sender, DragEventArgs e)
{
Control c = e.Data.GetData(e.Data.GetFormats()[0]) as Control;
// Declare rnd globally for creating random id for dynamic button(eg : Random rnd = new Random();)
Button btn = new Button();
btn.Name = "Button" + rnd.Next();
btn.Size = c.Size;
btn.Click += new System.EventHandler(DynamicButton_Click);
if (c != null)
{
btn.Text = c.Text;
btn.Location = this.groupBox5.PointToClient(new Point(e.X, e.Y));
this.groupBox5.Controls.Add(btn);
}
}
相關問題
- 1. 錘子拖拽中心
- 2. jQuery可拖拽/可拖拽
- 3. 可拖拽的位置在可拖拽
- 4. 可拖拽的元素在拖拽的頁面中拖動
- 5. jquery ui可拖拽/可拖拽,掉落時可交換拖拽
- 6. jQuery可拖拽/可拖拽目標組件
- 7. 我該如何限制一組可拖拽到一個droppable,另一組可拖拽到第二個可拖拽
- 8. iOS上的JQ UI可拖拽:啓動拖拽處理程序中的拖拽
- 9. Drag Drag拖拽時添加拖拽
- 10. jQuery UI的拖拽,拖拽父DIV
- 11. java2D棋子在棋盤上拖拽
- 12. 在android中拖拽n拖放textview
- 13. 在組件之間拖拽n掉落
- 14. jQuery可拖拽+可拖拽 - 物理移動拖拽項目到被拖拽的容器
- 15. 拖拽textview
- 16. jquery拖拽框
- 17. 拖拽圖像
- 18. 拖拽事件
- 19. 用Ember.js拖拽
- 20. Jquery拖拽CKEditor
- 21. 拖拽UIButtons
- 22. 元件拖拽
- 23. 可可:如何在拖拽時變形拖拽圖像
- 24. Jquery拖放 - 無法拖拽
- 25. 當被拖拽時,JPanel子類「跳轉」
- 26. 拖拽照片和沙盒應用程序
- 27. Ext.container.Viewport中可拖拽Extjs Ext.grid.Panel
- 28. 拖拽NetBeans Java組件爲新的LaF
- 29. XCode4 - 不允許拖拽和複製組
- 30. 只讀(但可拖拽)組合框
檢查本教程的http:/ /vicky4147.wordpress.com/2007/02/04/a-simple-drag-drop-in-winforms/ – Zaki 2013-02-26 12:13:20
嗨Sam1,謝謝你的代碼示例。我的情況有點不同。我的控件已經在組框中。 – brunoviske 2013-02-26 15:48:45
@brunoviske你可以請添加一些代碼你想做什麼? – Shrivallabh 2013-02-27 10:09:06