Senin, 09 September 2024

VB Classic Open Path In Explorer

Add the following Controls and then set the properties for these following objects:

Object Control Property Value
Form Name frmOpenExplorerByPath
Form Caption Open Explorer By Path
TextBox Name txtPath
TextBox Text txtPath
CommandButton Name cmdAddAppPathToTextBox
CommandButton Caption &App.Path
CommandButton Name cmdOpenInExplorer1
CommandButton Caption Open In Explorer &1
CommandButton Name cmdOpenInExplorer2
CommandButton Caption Open In Explorer &2
CommandButton Name cmdOpenInExplorer3
CommandButton Caption Open In Explorer &3

Type this Visual Basic classic source code in Visual Basic Studio Code window:
Option Explicit
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Private Sub cmdAddAppPathToTextBox_Click()
txtPath.Text = App.Path
If Right(txtPath.Text, 1) <> "\" Then txtPath.Text = txtPath.Text & "\"
txtPath.ToolTipText = txtPath.Text
End Sub

Private Sub cmdOpenInExplorer_Click()
' Show Windows file explorer based on the file path,
' If not it will open new windows file explorer based on the file path
If txtPath.Text = "" Then
MsgBox "Text Path is empty, please press the App.Path button"
Exit Sub
End If
ShellExecute hWnd, "open", txtPath.Text, 0, 0, 1
End Sub

Private Sub cmdOpenInExplorer2_Click()
' Show Windows file explorer based on the file path,
' If not it will open new windows file explorer based on the file path
' It will focus to the windows file explorer
If txtPath.Text = "" Then
MsgBox "Text Path is empty, please press the App.Path button"
Exit Sub
End If
ShellExecute hWnd, "Explore", txtPath.Text, vbNullString, vbNullString, 1
End Sub

Private Sub cmdOpenInExplorer3_Click()
' Show Windows file explorer based on the file path,
' if not it will open new windows file explorer based on the file path.
' It will focus to the windows file explorer.
' if the path is folder then it will go to the parent path and select the folder.
If txtPath.Text = "" Then
MsgBox "Text Path is empty, please press the App.Path button"
Exit Sub
End If
Shell "explorer.exe /select, " & txtPath.Text, vbNormalFocus
End Sub

Private Sub Form_Load()
txtPath.Text = ""
End Sub

Private Sub Form_Unload(Cancel As Integer)
Set frmOpenPath = Nothing
End Sub

Bibliography:

Introduction to Programming Using Visual Basic;Pearson.
Learning Visual Basic .NET: Introducing the Language, .NET Programming & Object Oriented Software Development;O'Reilly Media
Programming with Microsoft Visual Basic;Cengage Learning.
Visual Basic in Easy Steps;In Easy Steps Limited.

Related Post:

Tidak ada komentar:

Posting Komentar

Various Other Posts