我想製作一個Visual Basic窗體,該窗體具有可由用戶拖動到窗體上其他位置的指定元素集。我怎樣才能做到這一點?我已經看過Google,並在StackOverflow上進行了一些搜索,但是我沒有發現任何東西。使Visual Basic窗體上的元素可拖拽
感謝您的幫助!
我想製作一個Visual Basic窗體,該窗體具有可由用戶拖動到窗體上其他位置的指定元素集。我怎樣才能做到這一點?我已經看過Google,並在StackOverflow上進行了一些搜索,但是我沒有發現任何東西。使Visual Basic窗體上的元素可拖拽
感謝您的幫助!
我長時間寫了一篇關於這個話題的文章,可能對你有幫助。
Drag & Drop Files into Your Form or Control
在那個例子我展示瞭如何將文件拖到表單或控制。您的要求稍有不同,但有關拖動的基本想法仍然相同。
看看這個例子,它演示瞭如何將一個控件拖到窗體上的另一個控件。 在這個例子中,把你的窗體上的兩個列表框 - ListBox1
和ListBox2
,並添加以下代碼:
Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
ListBox1.AllowDrop = True
ListBox2.AllowDrop = True
ListBox1.Items.Add("Test Item 1")
ListBox1.Items.Add("Test Item 2")
ListBox1.Items.Add("Test Item 3")
ListBox1.Items.Add("Test Item 4")
ListBox2.Items.Add("Test Item 5")
ListBox2.Items.Add("Test Item 6")
ListBox2.Items.Add("Test Item 7")
ListBox2.Items.Add("Test Item 8")
End Sub
Private Sub ListBoxes_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown, ListBox2.MouseDown
Dim lb As ListBox = CType(sender, ListBox)
lb.DoDragDrop(lb, DragDropEffects.Move)
End Sub
Private Sub ListBoxes_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox1.DragEnter, ListBox2.DragEnter
e.Effect = DragDropEffects.Move
End Sub
Private Sub ListBoxes_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox1.DragDrop, ListBox2.DragDrop
If TypeOf sender Is ListBox Then
Dim lb As ListBox = CType(sender, ListBox)
Dim srcLb As ListBox = e.Data.GetData(GetType(ListBox))
If lb IsNot srcLb Then
lb.Items.Add(srcLb.SelectedItem.ToString)
srcLb.Items.Remove(srcLb.SelectedItem)
End If
End If
End Sub
現在將項目從一個列表框到另一個,反之亦然,看到了效果。
編輯:
移動控制表單
(在評論OP的澄清後加入)
移動窗體上的控件上的東西比拖着東西我上面顯示出完全不同的。您可以簡單地捕獲MouseDown,MouseMove和MouseUp事件來執行此操作。
這裏是一個例子。在窗體上添加很多控件,並添加以下代碼:
Private Moving As Boolean
Private Sub Controls_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
Dim ctl As Control = CType(sender, Control)
Cursor.Position = PointToScreen(ctl.Location + New Point(ctl.Width \ 2, ctl.Height \ 2))
Moving = True
End Sub
Private Sub Controls_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
If Moving Then
Dim ctl As Control = CType(sender, Control)
ctl.Location = PointToClient(Cursor.Position - New Point(ctl.Width \ 2, ctl.Height \ 2))
End If
End Sub
Private Sub Controls_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs)
Moving = False
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
For Each ctl As Control In Me.Controls
ctl.Cursor = Cursors.SizeAll
AddHandler ctl.MouseDown, AddressOf Controls_MouseDown
AddHandler ctl.MouseUp, AddressOf Controls_MouseUp
AddHandler ctl.MouseMove, AddressOf Controls_MouseMove
Next
End Sub
運行該應用程序並將控件拖到窗體上的這裏和那裏。
謝謝!我可以在按鈕等上使用它嗎?當我提到移動物體時,這就是我想到的。 – PWF
我上面顯示的是使用列表框。這只是一個例子。您也可以使用任何其他控件。只要通過'ListBoxes_DragDrop'方法,並觀察你有你拖動的對象的引用。同樣,如果您拖動一個「Button」控件,您將獲得該按鈕的引用,並且您可以執行任何您想要的操作。你究竟想要做什麼?您是否試圖將數據從一個控件移動到另一個控件,或者只是想要將其物理位置移到窗體上? –
我正試圖在窗體上移動窗體元素的物理位置。 – PWF