Hack Forums
[.NET] How to make a Browse button - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] How to make a Browse button (/showthread.php?tid=4616660)



How to make a Browse button - CodeBinary - Jan 2, 2015

Open up Visual Basic

Insert a textbox, and button

Double click your button:

Make a variable called ofd

Code
Dim ofd As New OpenFileDialog

What this is: Declaring a variable called ofd and makes new OpenFileDialog

Now call your variable and give it a title and filter it.
Code
ofd.Title = "Select Your file!" : ofd.Filter = "(*.exe)|*.exe"

Now make an id statement that if the file was ok change the textbox's text to the file name.

Code
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
      TextBox1.Text = ofd.FileName
      End If

Final Code:

Code
Private Sub ElegantThemeButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ElegantThemeButton2.Click
      Dim ofd As New OpenFileDialog
      ofd.Title = "Select Your file!" : ofd.Filter = "(*.exe)|*.exe"
      If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
      TextBox1.Text = ofd.FileName
      End If
    End Sub



* - Odus - Jan 2, 2015

(Jan 2, 2015 06:15 PM)Multi Bills Wrote:  
Code
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
      TextBox1.Text = ofd.FileName
      Else : Exit Sub

      End If

Why are you handling the else and doing nothing with it when it will exit the sub regardless? Also, please don't use the colon when you're making tutorials... They're meant to be clear. What awful writing. Why does this irritate me so much?


* - +Haze - Jan 2, 2015

It's not just you that it irritates. If you use it, atleast use it correctly, not like this.

And why are you exiting sub op? Do this its so much cleaner.

Code
Using ofd As New OpenFileDilaog With {.Filter = "Executable|*.exe"}
If ofd.ShowDialog = DialogResult.OK Then
' To stuff here
End If
End Using



* - CodeBinary - Jan 2, 2015

(Jan 2, 2015 06:29 PM)+Haze Wrote:  It's not just you that it irritates. If you use it, atleast use it correctly, not like this.

And why are you exiting sub op? Do this its so much cleaner.

Code
Using ofd As New OpenFileDilaog With {.Filter = "Executable|*.exe"}
If ofd.ShowDialog = DialogResult.OK Then
' To stuff here
End If
End Using

Fixed it.

(Jan 2, 2015 06:19 PM)Odus Wrote:  Why are you handling the else and doing nothing with it when it will exit the sub regardless? Also, please don't use the colon when you're making tutorials... They're meant to be clear. What awful writing. Why does this irritate me so much?

I don't appreciate you being so rude I am just trying to help.


* - ElementalTree - Jan 4, 2015

(Jan 2, 2015 06:34 PM)Multi Bills Wrote:  Fixed it.

I don't appreciate you being so rude I am just trying to help.

But if you're trying to help beginners, atleast advance from it yourself first.