文件夹浏览器对话框 [英] FolderBrowserDialog

查看:69
本文介绍了文件夹浏览器对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好...
我在VB.NET 2008中使用FolderBrowserDialog遇到问题

每当我单击按钮以显示出现FolderFrowserDialog时,它将出现3次...当我单击按钮时...
请帮忙...

这是我的代码:

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]

推荐答案

txtFileName.Text = txtFileName.Text.ToString

这告诉我您一直在疯狂地猜测,却不知道自己在做什么.在字符串上调用ToString可以实现什么功能?这条线对您有什么作用?

您真的不知道自己在做什么.您的if语句全部显示对话框.因此,如果他们都得到了评估,那么它就会显示三遍,就像您要求的那样.一次显示一次,如果您希望一次显示一次.
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.


当然,它将被调用3次

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

3次.您认为会发生什么.

您要做的是存储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


这篇关于文件夹浏览器对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆