[RESOLVED] Double Click on Textbox to open Windows explore path set on the TextBox
Hi Good people of VBForum, This is what I'm trying to achive, I have a text box where the Text in the TextBox properties is set to e.g. c:\myFolders, I want to be able to navigate to the c:\myFolders or any path that I have entered on the TextBox Text property when I double click on the TextBox.
Please help if possible.
Thank you,
Maxd
Re: Double Click on Textbox to open Windows explore path set on the TextBox
Do you really need it to open Explorer itself for some reason or are you just looking to be able to browse for a file? You're probably looking for an OpenFialDialog.
Re: Double Click on Textbox to open Windows explore path set on the TextBox
Quote:
Originally Posted by
FunkyDexter
Do you really need it to open Explorer itself for some reason or are you just looking to be able to browse for a file? You're probably looking for an OpenFialDialog.
hi FunkyDexter, thank you for you reply, yes I want the path entered on textbox to be opened in explorer whenever I double click on the textbox
Re: Double Click on Textbox to open Windows explore path set on the TextBox
If you really want to use explorer you just need to use Process.Start(myTextbox.Text).
I'll be surprised if that's really what your after, though. The various file dialog classes are more likely to give you what you need.
Re: Double Click on Textbox to open Windows explore path set on the TextBox
Code:
Dim procInfo As New ProcessStartInfo()
procInfo.UseShellExecute = False
procInfo.FileName = "explorer.exe"
procInfo.Arguments = """" & FolderPath & """" ' must be in double qoutes
Process.Start(procInfo)
Though in most of my apps I do it like this so that if explorer already has the folder open it is restored instead of opening another instance of explorer.
Though I'm sure that isn't fool proof if you have a lot of folders with same name open. ;)
Code:
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
OpenFolder("C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE")
End Sub
End Class
Code:
Imports System.Runtime.InteropServices
Module Module1
' Launch explorer & open folder
Public Sub OpenFolder(FolderPath As String)
' get folder name from full path
Dim titleText = IO.Path.GetFileName(FolderPath.Trim)
' get handle to windows explorer window with that title text.
Dim folder_hWnd = FindWindow("CabinetWClass", titleText) ' "CabinetWClass" used in Windows 7 for explorer windows.
' window folder found?
If folder_hWnd = IntPtr.Zero Then ' no, launch explorer to open folder!
Dim procInfo As New ProcessStartInfo()
procInfo.UseShellExecute = False
procInfo.FileName = "explorer.exe"
procInfo.Arguments = """" & FolderPath & """" ' must be in double quotes or folder names with comma char will mess up the arguments!
' open explorer w/folder path
Process.Start(procInfo)
Else
' folder with that name found running,
' so if minimized restore it,
If IsIconic(folder_hWnd) Then
ShowWindow(folder_hWnd, 9) ' SW_RESTORE = 9
Else ' otherwise, bring to front.
SetForegroundWindow(folder_hWnd)
End If
End If
End Sub
#Region "Private API"
Private Declare Auto Function IsIconic Lib "user32.dll" (ByVal hwnd As IntPtr) As Boolean
<DllImport("user32.dll", CharSet:=CharSet.Auto, EntryPoint:="FindWindow")> _
Private Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
End Function
<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
Private Function SetForegroundWindow(ByVal hWnd As IntPtr) As Boolean
End Function
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Function ShowWindow(ByVal hwnd As IntPtr, ByVal nCmdShow As Int32) As Boolean
End Function
#End Region
End Module
Re: Double Click on Textbox to open Windows explore path set on the TextBox
Hi Edgemeal, thank you very much for your reply, I'm testing the code and I'll let you know how I get on. Thank you
Re: Double Click on Textbox to open Windows explore path set on the TextBox
Hi Edgemeal,
I've tried the code below and it works very well but not the way I want it to, the code is kicking of when I run the app and which ever text i have on the textbox properties it opens that path automatically when the app start, what I want to do is when the user double clicks on the textbox it should take the user to the path that's currently on the textbox, I'm looking at different ways to modify your code to see if I can do it, if you have any suggestions, Please let me know. Thank you very much.
Code:
Imports System.Runtime.InteropServices
Module Module1
' Launch explorer & open folder
Public Sub OpenFolder(FolderPath As String)
' get folder name from full path
Dim titleText = IO.Path.GetFileName(FolderPath.Trim)
' get handle to windows explorer window with that title text.
Dim folder_hWnd = FindWindow("CabinetWClass", titleText) ' "CabinetWClass" used in Windows 7 for explorer windows.
' window folder found?
If folder_hWnd = IntPtr.Zero Then ' no, launch explorer to open folder!
Dim procInfo As New ProcessStartInfo()
procInfo.UseShellExecute = False
procInfo.FileName = "explorer.exe"
procInfo.Arguments = """" & FolderPath & """" ' must be in double quotes or folder names with comma char will mess up the arguments!
' open explorer w/folder path
Process.Start(procInfo)
Else
' folder with that name found running,
' so if minimized restore it,
If IsIconic(folder_hWnd) Then
ShowWindow(folder_hWnd, 9) ' SW_RESTORE = 9
Else ' otherwise, bring to front.
SetForegroundWindow(folder_hWnd)
End If
End If
End Sub
#Region "Private API"
Private Declare Auto Function IsIconic Lib "user32.dll" (ByVal hwnd As IntPtr) As Boolean
<DllImport("user32.dll", CharSet:=CharSet.Auto, EntryPoint:="FindWindow")> _
Private Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
End Function
<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
Private Function SetForegroundWindow(ByVal hWnd As IntPtr) As Boolean
End Function
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Function ShowWindow(ByVal hwnd As IntPtr, ByVal nCmdShow As Int32) As Boolean
End Function
#End Region
End Module
Re: Double Click on Textbox to open Windows explore path set on the TextBox
Show the code for your Form (at least where it calls this method). I was able to get it to only open up Explorer on a double-click using this code (and not immediately after I run the program):
Code:
Private Sub txtText1_DoubleClick(sender As Object, e As System.EventArgs) Handles txtText1.DoubleClick
OpenFolder(Me.txtText1.Text)
End Sub
Re: Double Click on Textbox to open Windows explore path set on the TextBox
Quote:
Originally Posted by
Pyth007
Show the code for your Form (at least where it calls this method). I was able to get it to only open up Explorer on a double-click using this code (and not immediately after I run the program):
Code:
Private Sub txtText1_DoubleClick(sender As Object, e As System.EventArgs) Handles txtText1.DoubleClick
OpenFolder(Me.txtText1.Text)
End Sub
Thank you very much Pyth007, being a noub is not easy, your suggestion worked Fine. thanks :)
Re: Double Click on Textbox to open Windows explore path set on the TextBox
Thank you Edgemeal, your solution works very well, best regards