這是一個(gè)簡單的示例,你只需要新建一個(gè)項(xiàng)目,并在項(xiàng)目上放置一個(gè)文本框:TEXTBOX1,然后,將以下代碼復(fù)制到代碼區(qū):
洞口網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)建站!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營維護(hù)。創(chuàng)新互聯(lián)建站2013年至今到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)建站。
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.AllowDrop = True
End Sub
''建拖曳事件,實(shí)現(xiàn)文件拖放
Private Sub Form1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter
Try
If e.Data.GetDataPresent(DataFormats.FileDrop) = True Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub Form1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter
Try
Dim filepaht As String() = e.Data.GetData(DataFormats.FileDrop)
For Each File As String In filepaht
textBox1.Text = File
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
如此,在文本框里將會出現(xiàn)被拖曳文件的路徑信息。
很久沒有上這里了,今天看到了這個(gè)問題,嘗試做了一個(gè);
本例以源文本框TextBox1全部文字作為拖放文字為例,實(shí)現(xiàn)拖放
1、向一個(gè)窗體中添加兩個(gè)文本框,分別名為TextBox1,TextBox2。注意:把TextBox2控件的AllowDrop屬性設(shè)置成True,這點(diǎn)不要遺漏。
2、完整的代碼如下:
Public Class Form1
Private MouseIsDown As Boolean = False
Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
'設(shè)置一個(gè)標(biāo)志以顯示鼠標(biāo)已按下。
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
'開始拖動(將TextBox1的文本內(nèi)容作為拖放內(nèi)容)。
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
'檢查正在被拖放的數(shù)據(jù)的格式。
If (e.Data.GetDataPresent(DataFormats.Text)) Then
'顯示復(fù)制光標(biāo)(表示是拖放行為)。
e.Effect = DragDropEffects.Copy
Else
'顯示不放置光標(biāo)(表示不是拖放行為)。
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
'粘貼文本(將拖放內(nèi)容作為TextBox2的文本內(nèi)容)。
TextBox2.Text = e.Data.GetData(DataFormats.Text)
End Sub
End Class
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
TextBox1.AllowDrop = True
End Sub
Private Sub TextBoxDragEnter(sender As Object, e As DragEventArgs) Handles TextBox1.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
Dim files As String()
Try
files = CType(e.Data.GetData(DataFormats.FileDrop), String())
Me.TextBox1.Text = files(files.Length - 1)
Catch ex As Exception
MessageBox.Show(ex.Message)
Return
End Try
End If
End Sub
TextBox1.AllowDrop = True 是開啟拖放支持,可以在窗體設(shè)計(jì)器里面開啟它,也可以代碼開啟。