Tip Browse Folder Dialog

jjdickow

Member
Joined
Aug 19, 2010
Messages
8
Programming Experience
Beginner
Alright guys so I am going to show you how to create a browsefolderdialog. All this does it popup to the user and let them choose a folder.

For this you need:
1 Button
1 Textbox

Double click your button and add in the code:

VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim BrowseFolder As New FolderBrowserDialog
        BrowseFolder.ShowDialog()
        TextBox1.Text = BrowseFolder.SelectedPath
    End Sub



VB.NET:
Dim BrowseFolder As New FolderBrowserDialog
This defines BrowseFolder as your FolderBrowserDialog

VB.NET:
BrowseFolder.ShowDialog()
This makes your FolderBrowserDialog pop-up to the user and lets them select their path

VB.NET:
TextBox1.Text = BrowseFolder.SelectedPath
This sets your textbox1.text to what ever folder is selected.

After that you can do whatever you want with your selected folder.



This is how it will work when you are done:

[youtube]http://www.youtube.com/watch?v=ci9zC2HN8lA[/youtube]
 
Actually I wouldn't do it like that at all. FolderBrowserDialog is a preconfigured form used as a modal dialog, and is designed as a component. This means two things;
  • it is visual and basically the same as any form you would create in Designer yourself, and
  • you usually only use one instance of these at any given time called from a form.
Components is something you can find in Toolbox in Designer, and you add one to form just like buttons and such controls. This one in particular is found under 'Dialogs' in Toolbox. Doing that does not add it to the client area of the form, but beneath it in what is called the 'components area'. Now you can select it and set properties for it in Designer just like any other control. As you try out all the things in Toolbox you'll discover there are lots of standard components.

Now for using it you can refer to it by the name in designer, for example Me.BrowseFolder. One essential thing about dialogs is the ability for user to cancel or press ok, these are most common though there are other dialog results too. In code this is expressed where ShowDialog is a function and it returns the DialogResult value reflected by user action. You can only proceed to get the user value if user pressed ok:
VB.NET:
If Me.BrowseFolder.ShowDialog = Windows.Forms.DialogResult.OK Then
    Me.TextBox1.Text = Me.BrowseFolder.SelectedPath
End If
When you type the code "ShowDialog=" the valid responses is presented by VS code editor, so there very little code typing to do to produce this code.
 
yea if you wanna take the easy way and not actually code stuff you can use pre configured things.

To quote another thread I read recently:
you know this is for beginners right? lol
;) The easy way would be the best for beginners, yes?
 
Back
Top