Click here to Skip to main content
14,736,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello guys...
I've got a problem using the FolderBrowserDialog in VB.NET 2008

Everytime i click the button to show the FolderBrowserDialog to appear, it appears 3 times...when i click the button...
Please help...

Here's my Code :
txtFileName.Text = cboDataBaseSelection.Text & " " & dtpdate.Text
txtFileName.Text = txtFileName.Text.ToString

Dim FBDBackupMSB As New FolderBrowserDialog 'Open dialog for user to select folder to save backup file
FBDBackupMSB.Description = "Select location for the backup file :"
FBDBackupMSB.ShowDialog()

If FBDBackupMSB.ShowDialog = Windows.Forms.DialogResult.Cancel Then
    MsgBox("Database Backup Operation was canceled.", MsgBoxStyle.Information, _
           My.Application.Info.Title)
    btnBackUp.Enabled = True
    btnExit.Enabled = True
ElseIf FBDBackupMSB.ShowDialog = Windows.Forms.DialogResult.OK = True Then
    'Set BackUpFileName
    BackUpFileName = FBDBackupMSB.SelectedPath & "/" & txtFileName.Text & ".msb" 
    'BackUpFileName = fbdBackup.SelectedPath & "/" & txtFileName.Text & ".sql" 
    'BackUpFileName = fbdBackup.SelectedPath & "/" & txtFileName.Text & ".xml" 

    'Instantiate new BackUp Class
    CurrentBackUp = New ClsBackUp(CurrentServer, CurrentUser.Name, CurrentUser.Password, _
                                  cboDataBaseSelection.SelectedItem.ToString, BackUpFileName)

    'Use BackGroundWorker to do backup
    bgwBackUp.RunWorkerAsync() 
End If

----------------------------------------------------------------


[Modified: moved the question before the code and cleaned up the code to make it more readable...will really help you get answers and only takes a minute or so]
Posted
Updated 8-Jul-10 7:09am
v3
Comments
Wayne Gaylard 8-Jul-10 12:01pm
   
You should know by nowto use PRE tags - surely?

txtFileName.Text = txtFileName.Text.ToString

This tells me you've been guessing wildly and have no idea what you're doing. Calling ToString on a string, achieves what exactly ? And what does this line do for you ?

You really don't have any idea what you're doing. Your if statements all show the dialog. So, if they all get evaluated, then it gets shown three times, just like you asked it to. Show it once, if you want it shown once.
   
Comments
William Winner 8-Jul-10 12:13pm
   
Reason for my vote of 5
Truly a wtf bit of code!
Of course it will be called 3 times

FBDBackupMSB.ShowDialog()

If FBDBackupMSB.ShowDialog 'more code
    'more code
ElseIf FBDBackupMSB.ShowDialog 'more code
    'more code


You called
ShowDialog
three times. What did you think would happen.

What you want to do is store the result of the ShowDialog and then test the result. As in:

Dim result As DialogResult = FBDBackupMSB.ShowDialog()

If result = DialogResult.Cancel Then
    'do something
ElseIf result = DialogResult.OK
    'do something else

End If
   
try this class..

''' <summary>
''' Specifies the class used for displaying dialog boxes on the screen.
''' </summary>
''' <remarks></remarks>
Public Class DBrowse

#Region "[ DBrowse ] Declarations"
    ''' <summary>
    ''' Used to store the current file name filter string, which determines the choices that appear in the "Save as file type" or "Files of type" box in the dialog box.
    ''' </summary>
    ''' <remarks></remarks>
    Private m_Filter As String = "Text File (*.txt)|*.txt"
    ''' <summary>
    '''  Used to store the initial directory displayed by the file dialog box.
    ''' </summary>
    ''' <remarks></remarks>
    Private m_InitialDirectory As String = My.Computer.FileSystem.SpecialDirectories.Desktop
    ''' <summary>
    ''' Used to store the root folder where the browsing starts from.
    ''' </summary>
    ''' <remarks></remarks>
    Private m_RootFolder As Environment.SpecialFolder = Environment.SpecialFolder.Desktop
#End Region
#Region "[ DBrowse ] Constructors"
    ''' <summary>
    ''' Initializes a new instance of the <b>DBrowse</b> class.
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub New()
        'NOTHING TO DO:
    End Sub
#End Region
#Region "[ DBrowse ] Properties"
    ''' <summary>
    ''' Gets or sets the current file name filter string, which determines the choices that appear in the "Save as file type" or "Files of type" box in the dialog box.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks>The following is an example of a filter string: "Text files (*.txt)|*.txt|All files (*.*)|*.*" <br></br><br></br>
    ''' You can add several filter patterns to a filter by separating the file types with semicolons, for example, "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*"</remarks>
    Public Property Filter() As String
        Get
            Return m_Filter
        End Get
        Set(ByVal value As String)
            m_Filter = value
        End Set
    End Property
    ''' <summary>
    ''' Gets or sets the initial directory displayed by the file dialog box.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks>The InitialDirectory property is typically set using one of the following sources: <br></br><br></br>
    ''' A path that was previously used in the program, perhaps retained from the last directory or file operation.<br></br>
    ''' A path read from a persistent source, such as an application setting, a Registry or a string resource in the application.<br></br>
    ''' Standard Windows system and user paths, such as Program Files, MyDocuments, MyMusic, and so on.<br></br>
    ''' A path related to the current application, such as its startup directory.
    ''' <br></br>Default is <i>My.Computer.FileSystem.SpecialDirectories.Desktop</i></remarks>
    Public Property InitialDirectory() As String
        Get
            Return m_InitialDirectory
        End Get
        Set(ByVal value As String)
            m_InitialDirectory = value
        End Set
    End Property
    ''' <summary>
    ''' Gets or sets the root folder where the browsing starts from.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks>Only the specified folder and any subfolders that are beneath it will appear in the dialog box and be selectable.
    ''' <br></br>Default is <i>Environment.SpecialFolder.Desktop</i></remarks>
    Public Property RootFolder() As Environment.SpecialFolder
        Get
            Return m_RootFolder
        End Get
        Set(ByVal value As Environment.SpecialFolder)
            m_RootFolder = value
        End Set
    End Property
#End Region
#Region "[ DBrowse ] Methods"
    ''' <summary>
    ''' Call(s) to Prompts the user to select a folder using <b>FolderBrowserDialog</b>.
    ''' <example><i>instance</i>.BrowserFolder(Me.TextBox1)</example>
    ''' </summary>
    ''' <param name="_ObjTextBox">TextBox object to assign the path selected by the user. </param>
    ''' <param name="_Message">Descriptive text displayed above the tree view control in the dialog box. </param>
    ''' <returns>Sucess or not as Boolean</returns>
    ''' <remarks></remarks>
    Public Function BrowserFolder(ByVal _ObjTextBox As TextBox, Optional ByVal _Message As String = "Select the Folder and click OK..!!") As Boolean
        Dim _ObjFldrBrwsr As New FolderBrowserDialog
        _ObjFldrBrwsr.RootFolder = m_RootFolder
        _ObjFldrBrwsr.ShowNewFolderButton = False
        _ObjFldrBrwsr.Description = _Message
        _ObjFldrBrwsr.SelectedPath = Trim(_ObjTextBox.Text)
        If (_ObjFldrBrwsr.ShowDialog(_ObjTextBox) = Windows.Forms.DialogResult.OK) Then
            _ObjTextBox.Text = _ObjFldrBrwsr.SelectedPath
            _ObjFldrBrwsr.Dispose()
            Return True
        Else
            _ObjFldrBrwsr.Dispose()
            Return False
        End If
    End Function
    ''' <summary>
    ''' Call(s) to Runs a common dialog box <b>OpenFileDialog</b>
    ''' <example><i>instance</i>.BrowseFile(Me.TextBox1)</example>
    ''' </summary>
    ''' <param name="_ObjTextBox">TextBox object to assign the FileName selected by the user. </param>
    ''' <param name="_Title">The file dialog box title.</param>
    ''' <returns>Sucess or not as Boolean</returns>
    ''' <remarks></remarks>
    Public Function BrowseFile(ByVal _ObjTextBox As TextBox, Optional ByVal _Title As String = "Select Text File...") As Boolean
        Dim _ObjFileBrowser As New System.Windows.Forms.OpenFileDialog
        _ObjFileBrowser.CheckFileExists = True
        _ObjFileBrowser.CheckPathExists = True
        _ObjFileBrowser.InitialDirectory = m_InitialDirectory
        _ObjFileBrowser.Title = _Title
        _ObjFileBrowser.FilterIndex = 0
        _ObjFileBrowser.Multiselect = False
        _ObjFileBrowser.Filter = m_Filter

        If (_ObjFileBrowser.ShowDialog(_ObjTextBox) = Windows.Forms.DialogResult.OK) Then
            _ObjTextBox.Text = _ObjFileBrowser.FileName
            Return True
        Else
            Return False
        End If
    End Function
    ''' <summary>
    ''' Call(s) to Runs a common dialog box <b>SaveFileDialog</b>
    ''' <example><i>instance</i>.BrowseFileSaveAs(Me.TextBox1)</example>
    ''' </summary>
    ''' <param name="_ObjTextBox">TextBox object to assign the FileName selected by the user. </param>
    ''' <param name="_Title">The file dialog box title.</param>
    ''' <returns>Sucess or not as Boolean</returns>
    ''' <remarks></remarks>
    Public Function BrowseFileSaveAs(ByVal _ObjTextBox As TextBox, Optional ByVal _Title As String = "Save File As...") As Boolean
        Dim _ObjFileSaveAs As New System.Windows.Forms.SaveFileDialog
        _ObjFileSaveAs.CheckFileExists = False
        _ObjFileSaveAs.CheckPathExists = True
        _ObjFileSaveAs.InitialDirectory = m_InitialDirectory
        _ObjFileSaveAs.Title = _Title
        _ObjFileSaveAs.FilterIndex = 0
        _ObjFileSaveAs.Filter = m_Filter

        If (_ObjFileSaveAs.ShowDialog(_ObjTextBox) = Windows.Forms.DialogResult.OK) Then
            _ObjTextBox.Text = _ObjFileSaveAs.FileName
            Return True
        Else
            Return False
        End If
    End Function
#End Region

End Class
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900