Results 1 to 10 of 10

Thread: [RESOLVED] Double Click on Textbox to open Windows explore path set on the TextBox

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2014
    Location
    Dublin, Ireland
    Posts
    11

    Resolved [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

  2. #2
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,900

    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

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2014
    Location
    Dublin, Ireland
    Posts
    11

    Re: Double Click on Textbox to open Windows explore path set on the TextBox

    Quote Originally Posted by FunkyDexter View Post
    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

  4. #4
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,900

    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

  5. #5
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    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!

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2014
    Location
    Dublin, Ireland
    Posts
    11

    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

  7. #7

    Thread Starter
    New Member
    Join Date
    Oct 2014
    Location
    Dublin, Ireland
    Posts
    11

    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

  8. #8
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    311

    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

  9. #9

    Thread Starter
    New Member
    Join Date
    Oct 2014
    Location
    Dublin, Ireland
    Posts
    11

    Re: Double Click on Textbox to open Windows explore path set on the TextBox

    Quote Originally Posted by Pyth007 View Post
    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

  10. #10

    Thread Starter
    New Member
    Join Date
    Oct 2014
    Location
    Dublin, Ireland
    Posts
    11

    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
  •  



Click Here to Expand Forum to Full Width