トップ回答者
Button にTextファイルのアイコンをドラッグ アンド ドロップしてButtonにファイルを割り当てる方法

質問
-
Microsoft Visual Basic 2008 Express Edition SP1 で Form1 上の Button1 に Text ファイルのアイコンをドラッグ アンド ドロップして、 Button1.Text に Text ファイルのファイル名を割り当て、更にその Button をクリックすると、割り当てた Text ファイルを開くようなプログラムを模索しています。
前半の、「Form1 上の Button1 に Text ファイルのアイコンをドラッグ アンド ドロップして、 Button1.Text に Text ファイルのファイル名を割り当て」までが、まずできませんでした。
Button1.AllowDrop = True
にしてから
Private Sub Button1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Button1.DragEnter
If (e.Data.GetDataPresent(DataFormats.Text)) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End IfEnd Sub
として
Private Sub Button1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Button1
Button1.Text = e.Data.GetData(DataFormats.Text).ToString
End Sub
としてみましたが、ドロップできませんでした。
ご教授いただきますよう、よろしくお願いいたします。
回答
-
Hongliang さまへ
早速のご回答をいただきまして、ありがとうございました。
教えていただきましたように修正し、下記のようにしまして、Button1 には、ファイル名(拡張子無し)で表示することが
できました。
Private Sub Button1_DragEnter(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles Button1.DragEnter
If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End IfEnd Sub
Private Sub Button1_DragDrop(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles Button1
Dim I As Integer = Len(".txt")
Dim FN0 As String
Dim FN1 As StringFN0 = e.Data.GetData(DataFormats.FileDrop)(0)
FN1 = ""
Do Until FN1 = "¥"
I = I + 1
FN1 = Mid(FN0, Len(FN0) - I, 1)
Loop
Button1.Text = Mid(FN0, Len(FN0) - I+1, I - Len(".txt"))End Sub
ありがとうございました。
つぎに、 FN0 = e.Data.GetData(DataFormats.FileDrop)(0) が対象のファイルになりますので、
ボタンをクリックすると、このファイルを開くプログラムを下記のようにしまして、実現できました。
Private Sub Button1_DragDrop(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles Button1
Dim I As Integer = Len(".txt")
Dim FN0 As String
Dim FN1 As StringFN0 = e.Data.GetData(DataFormats.FileDrop)(0)
FN1 = ""
Do Until FN1 = "\"
I = I + 1
FN1 = Mid(FN0, Len(FN0) - I, 1)
Loop
Button1.Text = Mid(FN0, Len(FN0) - I+1, I - Len(".txt"))'NumToPath() : 対象テキストファイルを保存するフォルダのパスを取得Subプロシージャ(別途作成済み)
NumDir = NumToPath()
IO.File.Copy(FN0, NumDir & "\" & Button1.Text & ".txt")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _
Button1.Click
'NumToPath() : 対象テキストファイルを保存するフォルダのパスを取得Subプロシージャ(別途作成済み)
NumDir = NumToPath()
Process.Start(NumDir & "\" & Button1.Text & ".txt")End Sub
なお "\" は、"¥"(実際は半角の¥)です。
ありがとうございました。
- 回答としてマーク けにと 2010年4月28日 15:57
すべての返信
-
Hongliang さまへ
早速のご回答をいただきまして、ありがとうございました。
教えていただきましたように修正し、下記のようにしまして、Button1 には、ファイル名(拡張子無し)で表示することが
できました。
Private Sub Button1_DragEnter(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles Button1.DragEnter
If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End IfEnd Sub
Private Sub Button1_DragDrop(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles Button1
Dim I As Integer = Len(".txt")
Dim FN0 As String
Dim FN1 As StringFN0 = e.Data.GetData(DataFormats.FileDrop)(0)
FN1 = ""
Do Until FN1 = "¥"
I = I + 1
FN1 = Mid(FN0, Len(FN0) - I, 1)
Loop
Button1.Text = Mid(FN0, Len(FN0) - I+1, I - Len(".txt"))End Sub
ありがとうございました。
つぎに、 FN0 = e.Data.GetData(DataFormats.FileDrop)(0) が対象のファイルになりますので、
ボタンをクリックすると、このファイルを開くプログラムを下記のようにしまして、実現できました。
Private Sub Button1_DragDrop(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles Button1
Dim I As Integer = Len(".txt")
Dim FN0 As String
Dim FN1 As StringFN0 = e.Data.GetData(DataFormats.FileDrop)(0)
FN1 = ""
Do Until FN1 = "\"
I = I + 1
FN1 = Mid(FN0, Len(FN0) - I, 1)
Loop
Button1.Text = Mid(FN0, Len(FN0) - I+1, I - Len(".txt"))'NumToPath() : 対象テキストファイルを保存するフォルダのパスを取得Subプロシージャ(別途作成済み)
NumDir = NumToPath()
IO.File.Copy(FN0, NumDir & "\" & Button1.Text & ".txt")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _
Button1.Click
'NumToPath() : 対象テキストファイルを保存するフォルダのパスを取得Subプロシージャ(別途作成済み)
NumDir = NumToPath()
Process.Start(NumDir & "\" & Button1.Text & ".txt")End Sub
なお "\" は、"¥"(実際は半角の¥)です。
ありがとうございました。
- 回答としてマーク けにと 2010年4月28日 15:57