"); //-->
我们经常会遇到这样的操作,那就是复制一个文本的内容到另外一个,此
小程序利用设置TextBox控件的AllowDrop属性和为TextBox控件的MouseDow
_n,DragDrop事件添加处理程序,从而实现将一个TextBox控件中的文字串
拖放到另一个TextBox控件中。
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'设置TextBox控件支持拖放操作
Me.TextBox1.AllowDrop = True
Me.TextBox2.AllowDrop = True
End Sub
'开始拖放textBox1控件中的文字
Private Sub TextBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
If (e.Button = Windows.Forms.MouseButtons.Left) Then
Me.TextBox1.SelectAll()
Me.TextBox1.DoDragDrop(Me.TextBox1.SelectedText, _
DragDropEffects.Move Or DragDropEffects.Copy)
End If
End Sub
'当用户在拖放操作过程中首次将鼠标光标拖到控件上时发生
Private Sub TextBox2_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragEnter
If (e.Data.GetDataPresent(DataFormats.Text)) Then
e.Effect = DragDropEffects.Copy
End If
End Sub
'在完成将textBox1中的文字拖放到textBox2时发生
Private Sub TextBox2_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragDrop
Me.TextBox2.Text = e.Data.GetData(DataFormats.Text).ToString()
'判断是否按下了Ctrl键
If ((e.KeyState & 8) <> 8) Then
Me.TextBox1.Text = ""
End If
End Sub
*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。