经过长时间学习VB.NET TextBox控件,于是和大家分享一下,看完本文你肯定有不少收获,希望本文能教会你更多东西。拖放操作的一个很简单然而很有用的情形是从一个VB.NET TextBox控件复制文本到另一个VB.NET TextBox控件。当然你可以只用键盘就能实现(CTRL + C and CTRL + V),然而拖放更简单因为它仅需要鼠标的移动就可以完成。
向一个窗体中添加两个文本框,并把第二个VB.NET 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.
End Sub
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
在上面的例子中,MouseDown事件是用来判断鼠标是否按下的,MouseMove事件中用到了DoDragDrop方法。尽管你可以在 MouseDown事件中来初始化Drag,然而这么做会带来出人意料之外的结果。在用户点击控件时,将显示no-drag 指针。DoDragDrop方法有两个参数
◆data,这个例子中代表的是第一个TextBox的Text属性。
◆allowedEffects,这个例子中是只允许复制。
【编辑推荐】