-
Nov 14th, 2014, 06:33 AM
#1
Thread Starter
New Member
[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
-
Nov 14th, 2014, 06:43 AM
#2
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.
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
-
Nov 14th, 2014, 09:05 AM
#3
Thread Starter
New Member
Re: Double Click on Textbox to open Windows explore path set on the TextBox
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
-
Nov 14th, 2014, 09:36 AM
#4
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.
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
-
Nov 14th, 2014, 09:54 AM
#5
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
Last edited by Edgemeal; Nov 16th, 2014 at 08:10 AM.
Reason: Add reason why folder name used as argument must be in double quotes!
-
Nov 19th, 2014, 03:41 PM
#6
Thread Starter
New Member
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
-
Nov 19th, 2014, 04:12 PM
#7
Thread Starter
New Member
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
-
Nov 19th, 2014, 04:40 PM
#8
Hyperactive Member
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
-
Nov 20th, 2014, 06:28 AM
#9
Thread Starter
New Member
Re: Double Click on Textbox to open Windows explore path set on the TextBox
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
-
Nov 20th, 2014, 06:29 AM
#10
Thread Starter
New Member
Re: Double Click on Textbox to open Windows explore path set on the TextBox
Thank you Edgemeal, your solution works very well, best regards
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|