How to choose a file with a button

Transmit

Newcomer
Joined
Jan 18, 2006
Messages
5
Well like the title says, I want to be able to choose a file by clicking on a button and that thing with browse and stuff comes up so I can choose my text file.
I want to be able to save a file too when clicking on a button. Can someone help me please? many thanks :D
 
If this is a windows application then just add a OpenFileDialog control to the form and from your code just call it's .ShowDialog method, once the user has selected a file you can retreive it's name via it's .FileName property.
 
yes this is a windows application, I just added the openfiledialog control but how can I call it? Do I need to declare a new variable and put in there or something?
maybe you can give me a quick sample code?

How can I get the place where the file is located?
like: c:/hlahlal/file.txt

bescause I want to put that place in variable and show it in a textbox


thx for your help so far, really appreciate it!
 
Last edited:
Once you have added an OpenFileDialog control to your project and named it using the Properties editor (in my example named openDialog). You simply add a click event to you button that looks something like this...

C#:
openDialog.ShowDialog();
myTextbox.Text = openDialog.FileName;
Visual Basic:
openDialog.ShowDialog()
myTextbox.Text = openDialog.FileName

This will display the dialog allowing the user to select a file, then once the dialog is closed the FileName will be added to the textbox named myTextbox. It may also be an idea to check if the user actually clicked ok rather than cancel, this can be done by....

C#:
if(openDialog.ShowDialog() == DialogResult.Ok)
    myTextbox.Text = openDialog.FileName;
Visual Basic:
        If openDialog.ShowDialog() = DialogResult.OK Then
            myTextbox.Text = openDialog.FileName
        End If
 
Thx man, now how can I open a window like showdialog does but then with "save" instead of "ok" so that it actually saved the file to that directory?

Okay here is my code I have now, it shows the directory in the textboxes and stuff and my mission :P is to get 2 text files togheter, now I get this error:
"Additional information: Object reference not set to an instance of an object."
at the part where I colored my code red:

Imports System.IO
Public Class Form1
Inherits System.Windows.Forms.Form

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer.Enabled = False
Progressbar.Value = 0
End Sub

Private Sub cmdsamenvoegen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdsamenvoegen.Click
Dim lijn As String
Dim bestand1 As String
Dim bestand2 As String
Dim schrijflocatie As String
Dim postcode As Integer
Dim gemeentenaam As String
Dim id As Integer
Dim ID1 As Integer
Dim naam As String
Dim geslacht As String
Dim straatnr As String
Dim postcode2 As Integer
Dim resultaat As Integer
Dim teller As Integer
Dim naamsamengevoegd As String
Dim straatnrsamengevoegd As String
Dim postcodesamengevoegd As Integer
Dim gemeentenaamsamengevoegd As String

bestand1 = txtbestand1.Text
bestand2 = txtbestand2.Text
schrijflocatie = txtsamengevoegd.Text

FileOpen(1, bestand1, OpenMode.Input)

Dim tweedebestand As New FileStream(bestand2, FileMode.OpenOrCreate, FileAccess.Read)
Dim reader As New StreamReader(tweedebestand)
Dim wegschrijfbestand As New FileStream(schrijflocatie, FileMode.Open, FileAccess.Write)
Dim writer As New StreamWriter(wegschrijfbestand)

Do While Not EOF(1)
Input(1, ID1)
Input(1, postcode)
Input(1, gemeentenaam)
Loop

Do While Not reader.peek - 1
lijn = reader.ReadLine()
naam = lijn.Substring(0, 21)
geslacht = lijn.Substring(21, 1)
straatnr = CType(lijn.Substring(22, 30), String)
postcode2 = CType(lijn.Substring(52, 1), Integer)
Loop

For teller = 0 To 62
id = CType(teller.ToString.Substring(0, 1), Integer)
naamsamengevoegd = naam.Substring(2, 32)
straatnrsamengevoegd = CType(straatnr.ToString.Substring(34, 23), String)
postcodesamengevoegd = CType(postcode.ToString.Substring(59, 4), Integer)
gemeentenaamsamengevoegd = gemeentenaam.Substring(65, 20)
writer.WriteLine(lijn)
Next

Progressbar.Value = 0
Timer.Enabled = True
End Sub

Private Sub Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer.Tick
Progressbar.Value = Progressbar.Value + 1
If Progressbar.Value >= 100 Then _
Timer.Enabled = False
End Sub




Private Sub cmdbestand1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdbestand1.Click
Dim openbestand1 As New OpenFileDialog
If openbestand1.ShowDialog() = DialogResult.OK Then
txtbestand1.Text = openbestand1.FileName
End If
End Sub

Private Sub cmdbestand2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdbestand2.Click
Dim openbestand2 As New OpenFileDialog
If openbestand2.ShowDialog() = DialogResult.OK Then
txtbestand2.Text = openbestand2.FileName
End If
End Sub

Private Sub cmdbestand3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdbestand3.Click
Dim lol As String
Dim opslaanbestand3 As New OpenFileDialog
If opslaanbestand3.ShowDialog() = DialogResult.OK Then
txtsamengevoegd.Text = opslaanbestand3.FileName
End If

End Sub
End Class


suggestions?
 
Last edited:
If you wish to show the standard save dialog then you simply add a SaveFileDialog to your application and call its .ShowDialog method in the same way you do for an OpenFileDialog.
 
Back
Top