我有,应该是一个非常简单的问题,但现在我用了5个小时没有结果。 我有一个usercontrol,UserControl1,我想在我的表单Form1上拖放。
就是这样。它应该很简单,但我用谷歌搜索了几个小时没有结果。有没有人有一个示例代码来解决这个问题?
答案 0 :(得分:0)
我不知道用户控件是什么(我还在学习),但我发现了一些可能有用的东西。 在此代码中,将两个TextBox控件添加到窗体并将第二个TextBox控件的AllowDrop属性设置为True。 然后使用此代码启用拖放
Private MouseIsDown As Boolean = False
Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
' Set a flag to show that the mouse is down.
MouseIsDown = True
End Sub
Private Sub TextBox1_MouseMove(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseMove
If MouseIsDown Then
' Initiate dragging.
TextBox1.DoDragDrop(TextBox1.Text, DragDropEffects.Copy)
End If
MouseIsDown = False
End Sub
Private Sub TextBox2_DragEnter(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles TextBox2.DragEnter
' Check the format of the data being dropped.
If (e.Data.GetDataPresent(DataFormats.Text)) Then
' Display the copy cursor.
e.Effect = DragDropEffects.Copy
Else
' Display the no-drop cursor.
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub TextBox2_DragDrop(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles TextBox2.DragDrop
' Paste the text.
TextBox2.Text = e.Data.GetData(DataFormats.Text)
End Sub
我希望您可以将其用于用户控件。祝你好运和评论!
答案 1 :(得分:0)
以下是我用过的代码,以使其工作。 现在我有一个表单Form1和一个usercontrol,Usercontrol1。为了拖动用户控件,我在usercontrol的顶部插入了一个面板,并且只有当用户按下面板(panel1)时,控件才能移动 - 就像普通的windows窗体一样。
Public Class UserControl1
Shared mypositionX As Integer
Shared mypositionY As Integer
Shared mBlnFormDragging As Boolean
Shared drawBeginX As Integer
Shared drawBeginY As Integer
Shared drawing As Boolean
Private Sub Panel1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseMove
If mBlnFormDragging = True Then
Dim position As Point = Form1.PointToClient(MousePosition)
Me.Location = New Point(position)
End If
End Sub
Private Sub Panel1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseUp
' Dim dd1 As DragDropEffects = DoDragDrop(ParentForm, DragDropEffects.Move)
mBlnFormDragging = False
Dim position As Point = Form1.PointToClient(MousePosition)
Location = New Point(position)
End Sub
Public Sub Panel1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown
'Dim dd1 As DragDropEffects = DoDragDrop(ParentForm, DragDropEffects.Move)
mBlnFormDragging = True
End Sub