VB.NET拖放文本文件到TextBox获得其路径。 C# 鼠标拖动一个文件路径 到TextBox中显示

VB.NET\u62d6\u653e\u6587\u672c\u6587\u4ef6\u5230TextBox\u83b7\u5f97\u5176\u8def\u5f84\u3002

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 \u662f\u5f00\u542f\u62d6\u653e\u652f\u6301\uff0c\u53ef\u4ee5\u5728\u7a97\u4f53\u8bbe\u8ba1\u5668\u91cc\u9762\u5f00\u542f\u5b83\uff0c\u4e5f\u53ef\u4ee5\u4ee3\u7801\u5f00\u542f\u3002

\u8bbe\u7f6e\u5141\u8bb8\u62d6\u653e\uff0c\u5e76\u5904\u7406\u62d6\u5165\u548c\u62d6\u653e\u4e8b\u4ef6
textBox.AllowDrop = true;
textBox.DragEnter += new DragEventHandler(textBox_DragEnter);
textBox.DragDrop += new DragEventHandler(textBox_DragDrop);

\u83b7\u53d6\u6587\u4ef6\u8def\u5f84
void textBox_DragDrop(object sender, DragEventArgs e)
{
string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
textBox.Text = path;
}
\u62d6\u5165\u7684\u662f\u6587\u4ef6\u5219\u54cd\u5e94
void textBox_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Link;
else
e.Effect = DragDropEffects.None;
}

新建一个项目,将原来form1的所有代码删了,将下面代码粘贴上去,就可以了。

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows 窗体设计器生成的代码 "

Public Sub New()
MyBase.New()

'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()

'在 InitializeComponent() 调用之后添加任何初始化

End Sub

'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer

'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(56, 128)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(304, 21)
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = "TextBox1"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(416, 389)
Me.Controls.Add(Me.TextBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.AllowDrop = True
listview1.AllowDrop = True
listview1.MultiSelect = True
listview1.FullRowSelect = True
listview1.View = View.Details

' 声明并构建 ColumnHeader 对象。
Dim header1, header2, header3, header4 As ColumnHeader
header1 = New ColumnHeader
header2 = New ColumnHeader
header3 = New ColumnHeader
header4 = New ColumnHeader

' 设定资料行的行首文字、水平对齐、以及宽度。
header1.Text = "文件名称"
header1.TextAlign = HorizontalAlignment.Left
header1.Width = 200

' 设定资料行的行首文字、水平对齐、以及宽度。
header2.TextAlign = HorizontalAlignment.Left
header2.Text = "所在文件夹"
header2.Width = 200

' 设定资料行的行首文字、水平对齐、以及宽度。
header3.TextAlign = HorizontalAlignment.Left
header3.Text = "大小"
header3.Width = 150

' 设定资料行的行首文字、水平对齐、以及宽度。
header4.TextAlign = HorizontalAlignment.Left
header4.Text = "修改日期"
header4.Width = 150

' 将资料行行首加入 ListView 控制项中。
listview1.Columns.Add(header1)
listview1.Columns.Add(header2)
listview1.Columns.Add(header3)
listview1.Columns.Add(header4)
End Sub
Dim listview1 As New ListView
Private Sub TextBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
Dim MyFiles(), MyFilesName(100), MyFilesDir As String
Dim i, j As Integer
' 将文件指派给一个数组。
MyFiles = e.Data.GetData(DataFormats.FileDrop)
MyFilesDir = Strings.Left(MyFiles(0), Strings.InStrRev(MyFiles(0), "\"))
ReDim MyFilesName(MyFiles.Length) '至关重要的一步

For i = 0 To MyFiles.Length - 1
MyFilesName(i) = Strings.Right(MyFiles(i), Strings.Len(MyFiles(i)) - Strings.InStrRev(MyFiles(i), "\"))
Next

Dim dirInfo As New System.IO.DirectoryInfo(MyFilesDir)

Dim fileSysInfos(), filesysinfo As System.IO.FileSystemInfo
Dim file As System.IO.FileInfo

Try
fileSysInfos = dirInfo.GetFileSystemInfos
ReDim fileSysInfos(MyFiles.Length) '至关重要的一步
Dim fileVers(fileSysInfos.Length), fileVer As FileVersionInfo
For i = 0 To MyFiles.Length - 1
fileSysInfos(i) = dirInfo.GetFileSystemInfos(MyFilesName(i))(0)
If Not CBool(fileSysInfos(i).Attributes And IO.FileAttributes.Directory) Then
fileVers(i) = FileVersionInfo.GetVersionInfo(MyFiles(i))
End If
Next

For Each filesysinfo In fileSysInfos
If Not filesysinfo Is Nothing Then '这一句可以避免“未将对象引用设置到对象的实例”(也就是空引用)的错误
j = listview1.Items.Count
listview1.Items.Add(filesysinfo.Name)
listview1.Items(j).SubItems.Add(MyFilesDir)

If CBool(filesysinfo.Attributes And IO.FileAttributes.Directory) Then
listview1.Items(j).SubItems.Add("")
listview1.Items(j).SubItems.Add("文件夹")
Else
file = dirInfo.GetFiles(filesysinfo.Name.ToString)(0)
fileVer = FileVersionInfo.GetVersionInfo(MyFilesDir & "\" & filesysinfo.Name)

listview1.Items(j).SubItems.Add(file.Length)
If fileVer.FileDescription = "" Then
listview1.Items(j).SubItems.Add(file.Extension)

'MsgBox(file.Extension)
If file.Extension = ".txt" Then
TextBox1.Text = MyFilesDir & filesysinfo.Name
Else
TextBox1.Text = "你拖动的不是文本文档"
End If

Else
listview1.Items(j).SubItems.Add(fileVer.FileDescription)
End If

End If
listview1.Items(j).SubItems.Add(filesysinfo.CreationTime)
End If
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

End If
End Sub
End Class

上MSDN查Clipboard.GetDataObject()相关的资料!

很值得关注

  • VB.NET鎷栨斁鏂囨湰鏂囦欢鍒癟extBox鑾峰緱鍏惰矾寰勩
    绛旓細鏂板缓涓涓」鐩紝灏嗗師鏉orm1鐨勬墍鏈変唬鐮佸垹浜嗭紝灏嗕笅闈唬鐮佺矘璐翠笂鍘,灏卞彲浠ヤ簡銆侾ublic Class Form1 Inherits System.Windows.Forms.Form Region " Windows 绐椾綋璁捐鍣ㄧ敓鎴愮殑浠g爜 "Public Sub New()MyBase.New()'璇ヨ皟鐢ㄦ槸 Windows 绐椾綋璁捐鍣ㄦ墍蹇呴渶鐨勩侷nitializeComponent()'鍦 InitializeComponent() 璋冪敤涔嬪悗...
  • VB.NET鎷栨斁鏂囨湰鏂囦欢鍒癟extBox鑾峰緱鍏惰矾寰勩
    绛旓細TextBox1.AllowDrop = True 鏄紑鍚鎷栨斁鏀寔锛屽彲浠ュ湪绐椾綋璁捐鍣ㄩ噷闈㈠紑鍚畠锛屼篃鍙互浠g爜寮鍚
  • VB 瀹炵幇 鎷栨斁 鍦╰ext 閲屾樉绀 鍙槸涓嶆垚鍔 涔熸病鏈夐敊 甯垜鐪嬬湅浠g爜_鐧惧害鐭 ...
    绛旓細濡傛灉鏄樉绀鏂囨湰鏂囦欢鐨勮瘽鍙互杩欐牱锛宼ext 鐨刴ultiline 璁剧疆 涓簍rue 鏄彲浠ュ琛屾樉绀哄惁鍒欏彧鑳戒竴琛 Private Sub Form_Load()Text1.Text = ""Text1.OLEDropMode = 1 End Sub Private Sub Text1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single,...
  • VB閲屽浣曡涓涓鏂囨湰鏂囦欢鐨勫唴瀹瑰湪TEXTBOX鎺т欢閲屾樉绀
    绛旓細LZ瑕佺殑鏄鎷栨斁骞舵墦寮鍚璇曡瘯杩欎釜鍚text1鐨凮LEDropMode瑕佽涓1~Private Sub Text1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)Dim FF As Integer FF = FreeFile Dim a As String Dim b As String Me.Caption = Data...
  • VB 鎷栨嫿鎵撳紑TXT
    绛旓細MsgBox "鎷栨斁鐨勬枃浠朵笉鏄 TXT 鏂囦欢", vbInformation Exit Sub End If '浠ヤ笅鏄墦寮鏂囨湰鏂囦欢,浜岃繘鍒舵柟寮忔墦寮 Dim tFree As Integer Dim tTxt As String tFree = FreeFile Open tFile For Binary As tFree tTxt = Space(LOF(tFree))Get #tFree, 1, tTxt Close tFree Me.Text1.Text = t...
  • vb闂:璇烽棶鎬庝箞鐢ㄤ簩杩涘埗鏂瑰紡(binary)鎵撳紑鏂囨湰鏂囦欢(txt)骞惰鍙栭噷闈㈢殑...
    绛旓細CDg1.FileName = "*.txt"CDg1.ShowOpen FileNam = CDg1.FileName Open FileNam For Binary As #1 '浠ヤ簩杩涘埗鏂囦欢鏂瑰紡鎵撳紑鏂囦欢 ReDim tStr(n) As Byte Get #1, , tStr '璇诲彇鏂囨湰 Close (1)Text1.Text = StrConv((tStr()), vbUnicode) ' 鍐欏叆text1 End Sub ...
  • VB 澶嶅埗鏂囦欢 鍒鎸囧畾鍏变韩鏂囦欢澶逛笅
    绛旓細鍋囪浣犺澶嶅埗鍒扮殑鏂囦欢澶鏄痗:\file FileCopy commondialog1.filename "c:\file\" 灏卞彲浠ヤ簡銆
  • VB绠鍗曞皬绋嬪簭,鍙婂叾绠鍗,楹荤儲鍦ㄧ嚎鐨勫府涓,涓昏褰㈡垚杩欐牱鍔熻兘 鑳芥湁涓涓鍏...
    绛旓細RM鏂囦欢淇鍣(RM-Fixer) 瑕佺偣:rmfix鍙兘浼氬rm鏂囦欢閫犳垚姘镐箙鎬ф崯鍧,鎵浠ュ湪浣跨敤rmfix涔嬪墠鎶婅淇鐨剅m鏂囦欢鍋氬浠姐傜涓绉嶆儏鍐:涓涓畬鏁寸殑rm鏂囦欢,浣嗗彧鑳芥挱鏀句竴閮ㄥ垎:1:鎶妑m鏂囦欢鎷栨嫿鍒皉mfix绋嬪簭鍥炬爣涓(娉:涔熷彲浠ュ懡浠よ鏂瑰紡,渚嬪:鈥渞mfix c:moviea.rm鈥) 2:杩欐椂鍊檙mfix浼氫互dos妯″紡杩愯骞舵樉绀哄嚭涓涓彍鍗,鎸夆淎鈥濋敭...
  • VB閲屽叧浜嶳ich textbox鎺т欢鐨勬柟娉
    绛旓細璁剧疆 FileName 灞炴э紝灏 .rtf 鎴 .txt 鏂囦欢鐨勫唴瀹硅鍏ヨ鎺т欢銆傜敤 SelPrint 鏂规硶鎵撳嵃 RichTextBox 鎺т欢涓殑鍏ㄩ儴鎴栭儴鍒鏂囨湰銆傚皢 RichTextBox 鎺т欢缁戝畾鍒 Data 鎺т欢锛屽彲浠ュ鐞 Microsoft Access 鏁版嵁搴撶殑 Memo 瀛楁锛屾垨鑰呭叾瀹冩暟鎹簱涓ぇ鍨嬬殑鏂囨湰瀛楁锛堟瘮濡 SQL 鏈嶅姟鍣ㄤ腑鐨 TEXT 鏁版嵁绫诲瀷锛夈傚湪杩愯鏃讹紝...
  • vb6.0 濡備綍璺ㄥ鍣ㄦ垨鑰呰法绐椾綋鎷栧姩鍥剧墖?
    绛旓細As Integer, X As Single, Y As Single)If Data.GetFormat(vbCFFiles) Then Dim vFN For Each vFN In Data.Files DropFile Text1, vFN Next vFN End If End Sub 杩愯搴旂敤绋嬪簭锛屾墦寮 Windows鈥滆祫婧愮鐞嗗櫒鈥濓紝绐佸嚭鏄剧ず鑻ュ共鏂囨湰鏂囦欢骞跺皢瀹冧滑鎷栧姩鍒版枃鏈鎺т欢涓傚湪鏂囨湰妗嗕腑灏嗘墦寮姣忎釜鏂囨湰鏂囦欢銆
  • 扩展阅读:免费logo设计生成器 ... vts文件怎么转成mp3 ... 免费海报一键生成器 ... vb官网下载 ... 免费logo设计自动生成 ... 一键logo设计 ... 免费logo一键生成器 ... 安卓手机如何打开vtt文件 ... 名字logo免费设计在线生成 ...

    本站交流只代表网友个人观点,与本站立场无关
    欢迎反馈与建议,请联系电邮
    2024© 车视网