OpenFileDialog

timtom1

Member
Joined
Jun 12, 2006
Messages
24
Programming Experience
Beginner
Hi when you open a form dialog box and select a file path how do you get it to appear in the textbox and the file locaiton stored?
 
In that case there is something wrong else where. You are you saying that you open a FileDialog select a file hit enter, or ok and then the path isn't displayed in the textbox. Mmmm.. sounds fishy to me. Can you post the code you are using.
 
VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] ButtonBrowse_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] ButtonBrowse.Click
[/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]' now launch the dialog box
[/COLOR][/SIZE][SIZE=2]OpenFileDialog.ShowDialog()
[/SIZE]

VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] TextBox_TextChanged([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] TextBoxPath.TextChanged
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].TextBox.Text = OpenFileDialog.FileName
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff][COLOR=#000000]
[/COLOR]
[/COLOR][/SIZE]
 
Is that it!?!:) Ok, so have you set up the rest of the properties of the OpenFileDialog? The list is below...

AddExtension: Gets/Sets if the dialog box adds extension to file names if the user doesn't supply the extension.
CheckFileEixsts: Checks whether the specified file exists before returning from the dialog.
CheckPathExists: Checks whether the specified path exists before returning from the dialog.
DefaultExt: Allows you to set the default file extension.
FileName: Gets/Sets file name selected in the file dialog box.
FileNames: Gets the file names of all selected files.
Filter: Gets/Sets the current file name filter string, which sets the choices that appear in the "Files of Type" box.
FilterIndex: Gets/Sets the index of the filter selected in the file dialog box.
InitialDirectory: This property allows to set the initial directory which should open when you use the OpenFileDialog.
MultiSelect: This property when set to True allows to select multiple file extensions.
ReadOnlyChecked: Gets/Sets whether the read-only checkbox is checked.
RestoreDirectory: If True, this property restores the original directory before closing.
ShowHelp: Gets/Sets whether the help button should be displayed.
ShowReadOnly: Gets/Sets whether the dialog displays a read-only check box.
Title: This property allows to set a title for the file dialog box.
ValidateNames: This property is used to specify whether the dialog box accepts only valid file names.

But the real reason this isn't working is because the textchanged event wont fire because nothing has changed.

VB.NET:
PrivateSub ButtonBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonBrowse.Click
' now launch the dialog box
OpenFileDialog.ShowDialog()
Me.TextBox.Text = OpenFileDialog.FileName

 
Hello. Get an OpenFileDialog from the Toolbox.
To a button click put this:

If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
TextBox1.Text = OpenFileDialog1.FileName
End If
 
hi mate cheers for that, I have managed to get the file name to display from OpenFileDialog and limited the search to jpg but I also have a picture box on there now how do I get the selected picture to display in the box?
 
Hello TimTom1. I'll paste this code then explain:
VB.NET:
'ofdPic.Filter = "Windows Bitmaps|*.bmp|jpeg Files|*.jpg"
If ofdPic.ShowDialog = Windows.Forms.DialogResult.OK Then
picShowL.Image = Image.FromFile(ofdPic.FileName)
End If
MsgBox(ofdPic.FileName)

OK? ofdPic is the OpenFileDialog got from the Toolbox. You can change its Name as I have done by clicking on its icon in the lower pane and altering it in the Properties Window.[I like making Names short!]
picPic is the Name of the PictureBox.
The line: ofdPic.Filter..... can be used.......it's worth looking into the use of filters........
I added on a MsgBox so you can get the path as well.....if you want it. But use the TextBox as before.
Happy Programming!!
 
Back
Top