Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 7th, 2005, 8:14 PM   #1
Gradius
Newbie
 
Join Date: Nov 2004
Posts: 10
Rep Power: 0 Gradius is on a distinguished road
Open File Dialog box

How do i make one of these. Does it come with VB because if it does i dont have it.

Gradius is offline   Reply With Quote
Old Aug 11th, 2005, 1:43 PM   #2
BrianN
Newbie
 
Join Date: Aug 2005
Posts: 6
Rep Power: 0 BrianN is on a distinguished road
You would use the common dialog control.

To use this control you would go to the projects menu. and then click on components. (see figure 1)


Figure 1

You will then presented with the components screen. (see figure 2)


Figure 2

On the components screen scroll down to the Microsoft Common Dialog Control 6.0. (The version number may not be 6.0 but that is ok) Check the checkbox next to the contorl and click ok. You will then see another control added to the toolbar. Add this control to the form that you want to call the control from.

I also added a command button to the form to show you how you would go about using the code. So here is the code that I had for the command button.

Private Sub Command1_Click()
CommonDialog1.FileName = "" 'Reset the filename to nothing so that if the user doesn't select a file we can tell
CommonDialog1.Filter = "All files (*.*)|*.*|Text files (*.txt)|*.txt|" 'These are the filetypes that show up in the dropdown list. First it is the text describing the filetype then | and the file type. You may have as many of these as you want
CommonDialog1.ShowOpen ' this is the command to show the dialog box
If CommonDialog1.FileName = "" Then ' if the filename is empty
    Exit Sub
Else ' if there was a file selected
    ' put the file handleing code here
    MsgBox "You selected: " & CommonDialog1.FileName
End If
End Sub

If you need any more help let me know.
BrianN is offline   Reply With Quote
Old Aug 11th, 2005, 2:27 PM   #3
Rory
Expert Programmer
 
Rory's Avatar
 
Join Date: Jan 2005
Location: London
Posts: 542
Rep Power: 4 Rory is on a distinguished road
Send a message via MSN to Rory
Well Microsoft would have you use the CommonDialog ActiveX Control, for which there is plenty of documentation. However, this is old, useless, requires a Professional or better edition and does not come by default with Windows. Unless you're willing to write a setup program for absolutely everything, I would use the "Native VB" method and call the Common Dialog APIs directly.

The code below demonstrates basic showing of a multi-select open dialog. Part of it was from the Windows SDK in C++.

Declarations, etc - put in a module:
Option Explicit

' Common Dialog Flags stolen from Windows.h
Public Enum CDlG32Flags
    OFN_ALLOWMULTISELECT = &H200
    OFN_CREATEPROMPT = &H2000
    OFN_ENABLEHOOK = &H20
    OFN_ENABLETEMPLATE = &H40
    OFN_ENABLETEMPLATEHANDLE = &H80
    OFN_EXPLORER = &H80000
    OFN_EXTENSIONDIFFERENT = &H400
    OFN_FILEMUSTEXIST = &H1000
    OFN_HIDEREADONLY = &H4
    OFN_LONGNAMES = &H200000
    OFN_NOCHANGEDIR = &H8
    OFN_NODEREFERENCELINKS = &H100000
    OFN_NOLONGNAMES = &H40000
    OFN_NONETWORKBUTTON = &H20000
    OFN_NOREADONLYRETURN = &H8000& 'Pointer
    OFN_NOTESTFILECREATE = &H10000
    OFN_NOVALIDATE = &H100
    OFN_OVERWRITEPROMPT = &H2
    OFN_PATHMUSTEXIST = &H800
    OFN_READONLY = &H1
    OFN_SHAREAWARE = &H4000
    OFN_SHAREFALLTHROUGH = 2
    OFN_SHAREWARN = 0
    OFN_SHARENOWARN = 1
    OFN_SHOWHELP = &H10
    OFS_MAXPATHNAME = 260
    OFS_FILE_OPEN_FLAGS = &H382000
    OFS_FILE_SAVE_FLAGS = &H280006
End Enum

Public Type OPENFILENAME
    nStructSize As Long
    hWndOwner As Long
    hInstance As Long
    sFilter As String
    sCustomFilter As String
    nMaxCustFilter As Long
    nFilterIndex As Long
    sFile As String
    nMaxFile As Long
    sFileTitle As String
    nMaxTitle As Long
    sInitialDir As String
    sDialogTitle As String
    flags As CDlG32Flags
    nFileOffset As Integer
    nFileExtension As Integer
    sDefFileExt As String
    nCustData As Long
    fnHook As Long
    sTemplateName As String
End Type

Public Declare Function GetOpenFileName Lib "comdlg32" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
Public Declare Function GetSaveFileName Lib "comdlg32" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long
Public Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
Public Function AppendSlash(ByVal Path As String) As String
If Right(Path, 1) = "\" Then
    AppendSlash = Path
Else
    AppendSlash = Path & "\"
End If
End Function
'Functions for handling Null terminated strings, MSDN
Public Function StripDelimitedItem(startStrg As String, delimiter As String) As String
    Dim pos As Long
    Dim item As String
    pos = InStr(1, startStrg, delimiter)
    If pos Then
       StripDelimitedItem = Mid$(startStrg, 1, pos)
       startStrg = Mid$(startStrg, pos + 1, Len(startStrg))
    End If
End Function
Public Function TrimNull(item As String) As String
    Dim pos As Integer
    pos = InStr(item, Chr$(0))
    If pos Then
       TrimNull = Left$(item, pos - 1)
    Else
       TrimNull = item
    End If
End Function

Put this on a form with a button named "command1"
Private Sub Command1_Click()
    Dim buff As String
    Dim OFN As OPENFILENAME
    Dim SelectedItems As Collection
    Set SelectedItems = New Collection
    
    With OFN
     'N.B. As this struct uses nulls to terminate (apparently something is wrong with pipes used in the ActiveX Control)
     '     MSDN says strings have to be double null terminated to prevent a segmentation fault
       .nStructSize = Len(OFN)
       .hWndOwner = Me.hWnd
       .sFilter = "Text Files" & vbNullChar & "*.txt" & vbNullChar & _
               "All Files (*.*)" & vbNullChar & "*.*" & vbNullChar & vbNullChar
       .nFilterIndex = 1
       .sFile = "Something.txt" & Space$(1024) & vbNullChar & vbNullChar
       .nMaxFile = Len(.sFile)
       .sDefFileExt = "txt" & vbNullChar & vbNullChar
       .sFileTitle = vbNullChar & Space$(512) & vbNullChar & vbNullChar
       .nMaxTitle = Len(OFN.sFileTitle)
       .sInitialDir = "C:\" & vbNullChar & vbNullChar
       .sDialogTitle = "Open Summat"
       .flags = OFS_FILE_OPEN_FLAGS Or OFN_ALLOWMULTISELECT
    End With
    If Not GetOpenFileName(OFN) = 0 Then
        buff = Trim$(Left$(OFN.sFile, Len(OFN.sFile) - 2))
        'Remove the various control characters
        Do While Len(buff) > 3
            SelectedItems.Add TrimNull(StripDelimitedItem(buff, vbNullChar))
        Loop
        If SelectedItems.Count > 1 Then 'Multiselect
            Dim ItemEnum As Integer
            For ItemEnum = 2 To SelectedItems.Count
                MsgBox "You selected: " & AppendSlash(SelectedItems.item(1)) & SelectedItems.item(ItemEnum)
            Next
        Else
            MsgBox "You selected: " & SelectedItems.item(1)
        End If
        MsgBox "Asked ReadOnly: " & CBool(Abs((OFN.flags And OFN_READONLY)))
    Else
        MsgBox "Error or user cancelled"
    End If
    Set SelectedItems = Nothing
End Sub

This looks like overkill, but it's trivial to wrap it within a ShowOpenDialog function or whatever. Hope this helps!
Rory is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:12 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC