Vb6:选择一个文件以便以后通过浏览按钮使用

时间:2008-11-14 04:53:10

标签: vb6

这是我的代码(注意这是由朋友提供的):

Private Sub Browse_Click()
   Dim textfile As String
   textfile = Space(255)
   GetFileNameFromBrowseW Me.hWnd, StrPtr(sSave), 255, StrPtr("c:\"), 
      StrPtr("txt"), StrPtr("Apps (*.txt)" + Chr$(0) + "*.txt" + Chr$(0) +
      "All files (*.*)" + Chr$(0) + "*.*" + Chr$(0)), StrPtr("Select File")
      Text1 = Left$(textfile, lstrlen(textfile))
End Sub

基本上后来我编辑了所选的文本文件,所以稍后我只是在我的函数中使用textfile来调用它。但是我得到了一条未找到的路径,所以我觉得我做错了什么。 提前谢谢。

编辑:我想要做的就是选择一个文本文件,然后再调用它并使用它。

5 个答案:

答案 0 :(得分:9)

正如shahkalpesh所提到的,您只需使用标准的COM库即可访问此功能。

在VB6中,添加组件:

  • 项目>部件
  • 在“控件”选项卡上,选择“Microsoft通用对话框控件6.0(SP6)”

现在在表单上,​​从工具箱中添加新的Common Dialog控件

在代码中,您需要:

CommonDialog.Filter = "Apps (*.txt)|*.txt|All files (*.*)|*.*"
CommonDialog.DefaultExt = "txt"
CommonDialog.DialogTitle = "Select File"
CommonDialog.ShowOpen

'The FileName property gives you the variable you need to use
MsgBox CommonDialog.FileName

答案 1 :(得分:1)

VB6中的“通用对话框控件”不提供此功能吗?

我的VB6有点生疏,但已经提供了选择文件的基本对话框 工具 - >控制 - > Microsoft Common Dialog Controls v ....

此外,您对GetFileNameFromBrowseW的调用不包括引用变量 - textfile

答案 2 :(得分:0)

可能save包含路径名,但文本文件包含255个空格。

答案 3 :(得分:0)

sSave替换为textfile。当您稍后需要引用所选文件时,请使用Text1(可能是VB文本框控件,因此Text1单独隐式调用Text1.Text.Text是VB.Textbox的默认成员。 / p>

答案 4 :(得分:0)

来自here

我找到了这个代码并运行它。

Private Const VER_PLATFORM_WIN32_NT = 2
Private Type OSVERSIONINFO
    dwOSVersionInfoSize As Long
    dwMajorVersion As Long
    dwMinorVersion As Long
    dwBuildNumber As Long
    dwPlatformId As Long
    szCSDVersion As String * 128
End Type
Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (ByRef lpVersionInformation As OSVERSIONINFO) As Long
Private Declare Function GetFileNameFromBrowseW Lib "shell32" Alias "#63" (ByVal hwndOwner As Long, ByVal lpstrFile As Long, ByVal nMaxFile As Long, ByVal lpstrInitialDir As Long, ByVal lpstrDefExt As Long, ByVal lpstrFilter As Long, ByVal lpstrTitle As Long) As Long
Private Declare Function GetFileNameFromBrowseA Lib "shell32" Alias "#63" (ByVal hwndOwner As Long, ByVal lpstrFile As String, ByVal nMaxFile As Long, ByVal lpstrInitialDir As String, ByVal lpstrDefExt As String, ByVal lpstrFilter As String, ByVal lpstrTitle As String) As Long
Private Sub Form_Load()
    'KPD-Team 2001
    'URL: http://www.allapi.net/
    'E-Mail: KPDTeam@Allapi.net
    Dim sSave As String
    sSave = Space(255)
    'If we're on WinNT, call the unicode version of the function
    If IsWinNT Then
        GetFileNameFromBrowseW Me.hWnd, StrPtr(sSave), 255, StrPtr("c:\"), StrPtr("txt"), StrPtr("Text files (*.txt)" + Chr$(0) + "*.txt" + Chr$(0) + "All files (*.*)" + Chr$(0) + "*.*" + Chr$(0)), StrPtr("The Title")
    'If we're not on WinNT, call the ANSI version of the function
    Else
        GetFileNameFromBrowseA Me.hWnd, sSave, 255, "c:\", "txt", "Text files (*.txt)" + Chr$(0) + "*.txt" + Chr$(0) + "All files (*.*)" + Chr$(0) + "*.*" + Chr$(0), "The Title"
    End If
    'Show the result
    MsgBox sSave
End Sub
Public Function IsWinNT() As Boolean
    Dim myOS As OSVERSIONINFO
    myOS.dwOSVersionInfoSize = Len(myOS)
    GetVersionEx myOS
    IsWinNT = (myOS.dwPlatformId = VER_PLATFORM_WIN32_NT)
End Function

从我可以告诉你GetFileName函数看起来正确所以我猜这个问题是用这个

Text1 = Left$(textfile, lstrlen(textfile))

用它来检查

MsgBox "(" & Text1 & ")-(" & textfile & ")"

确保您从Left $和lstrlen

获得预期结果