Page 1 of 1

Programming: Using File Dialogs and Showing Text in Files

Posted: Sun May 15, 2005 10:50 am
by Uber-Banana
*Update on bottom*

First, vocabulary:

OpenFileDialog: The dialog that comes up when a user presses the open button in your application.

SaveFileDialog: The dialog that comes up when a user presses the open button in your application.

Here is an example of using an OpenFileDialog:

Code: Select all

OpenFileDialog.FileName = "Test"
OpenFileDialog.InitialDirectory = "C:\Program Files\Test Program"
OpenFileDialog.Filter = " Test File (*.test)|.test"
OpenFileDialog.DefaultExt = "test"
OpenFileDialog.ShowDialog()
Example of SaveFileDialog:

Code: Select all

SaveFileDialog.Filename = "Test"
SaveFileDialog.InitialDirectory = "C:\Program Files\Test Program"
SaveFileDialog.Filter = " Test File (*.test)|.test"
SaveFileDialog.DefaultExt = "test"
SaveFileDialog.ShowDialog()
What does it all mean? Well, let me explain.

all of the commands above showdialog are telling the dialog "what to do",
without these parameters the dialog wouldnt do anything.

for example, if you didnt have .DefaultExt then it would just save the files as "files" with no extension, unless the user specifies one at end of name.

Say you put ShowDialog above everything, it wont work......

why you say, because the way the exe reads your code is from top to bottom, so if your parameters are below, the exe hasnt even recognized them yet.

Example of opening and saving .map files:

Code: Select all

OpenFileDialog.FileName = "*.map"
OpenFileDialog.DefaultExt = "map"
OpenFileDialog.InitialDirectory = "C:\Program Files\Microsoft Games\Halo\Maps"
OpenFileDialog.Filter = "Halo Cache Files (*.map)|.map"
OpenFileDialog.ShowDialog()
Saving:

Code: Select all

SaveFileDialog.FileName = "*.map"
SaveFileDialog.DefaultExt = "map"
SaveFileDialog.InitialDirectory = "C:\Program Files\Microsoft Games\Halo\Maps"
SaveFileDialog.Filter = "Halo Cache Files (*.map)|.map"
SaveFileDialog.ShowDialog()
I get the following error:

Directory Not Found

Solution:

Make sure the folder exists, if it doesnt then create it and try again.

Error:

Blue underline under Open/SaveFileDialog

make sure that your dialogs are created and named OpenFileDialog or SaveFileDialog.



I hope this has helped some of you with your VB programming.

There are many more parameters but these are the most important to get the dialog to work.

.Title is a parameter that will change the title of the dialog box that comes up.

Example of use:

Code: Select all

OpenFileDialog.Title = "Open Halo Cache File"
or

Code: Select all

SaveFileDialog.Title = "Save Halo Cache File"

Code: Select all

Public Sub OpenFileDialog_FileOk

Dim strFile As String
Dim strFileContents As String

strFile = OpenFileDialog.FileName

strFileContents = My.Computer.FileSystem.ReadAllText(strFile)
MsgBox("Done loading file" + strFile)

txtmain.Text = strFileContents

End Sub

El Fin

Posted: Mon May 16, 2005 3:11 pm
by Zak Nuva
Here's a question: what if I want to open the file and display its contents in a label or something(such as Halo Map Tools or HHT)? I tried having the label text equal OpenFileDialog.Show, but it only displayed a 1, not the contents of the file. What I want to know is how to display the file's hex contents in a label or textbox so the user can see/edit it. If you tell me, I'll put your name in the credits when I make the next big Halo editor. :wink:

...like that will ever happen. :P At least I'll be at rest...I've been wondering this for a while now lol . I could at least make a simple hex editor with it though :D .

Posted: Mon May 16, 2005 3:15 pm
by Uber-Banana
you need to make it



label.Text = OpenFileDialog.FileName



see if that works, it wont be hex tho, at least i dont think

Posted: Mon May 16, 2005 3:31 pm
by Zak Nuva
Ah...doesn't work. I can get the hash of the file, or display its path with the code you gave me. I've also tried converting the file to string, but it only shows System.IOstream or something like that. This is frustrating me. :roll: XD

I'm working on it though...your tutorial did help :D

Posted: Tue May 17, 2005 12:13 pm
by Uber-Banana
i myself am trying to get that to work, hehe


thats the only thing i cant do with filedialogs.

Posted: Tue Aug 16, 2005 10:16 am
by Onetoomanysodas
Well if you use a Rich Text Box control you can use the '.LoadFile' property to load text or what not.

Posted: Wed Oct 19, 2005 3:09 pm
by PlasmaGhost
ok heres how you do it, u need the system IO.

Code: Select all

   Dim data As String = ""
   Dim Open As New OpenFileDialog()
	Dim myStreamReader As System.IO.StreamReader

	Open.Filter = "Halo Map (*.map)|*.map"
	Open.CheckFileExists = True
	Open.Title = "Open Halo Map"
	Open.ShowDialog(Me)
        Try
            Open.OpenFile()
            myStreamReader = System.IO.File.OpenText(Open.FileName)
            data = myStreamReader.ReadToEnd()
	Catch ex As Exception
            MsgBox("An error has occured.")
        End Try
now, that loads the contents into the string named data. sorry but i dont know how to convert the text into bytes. That there will load up text files. so u can make up ur own file extension and there ya go. Ok so, next lesson, to save it. do this:

Code: Select all

        Dim data As String = ""
        Dim Save As New SaveFileDialog()
        Dim myStreamWriter As System.IO.StreamWriter

        Save.Filter = "Halo Map (*.map)|*.map"
        Save.CheckPathExists = True
        Save.Title = "Save Map"
        Save.ShowDialog(Me)

        Try
            myStreamWriter = System.IO.File.AppendText(Save.FileName)
            myStreamWriter.Write(data)
            myStreamWriter.Flush()
        Catch ex As Exception
            MsgBox("An error has occured.")
        End Try
ok so that saves whatever is in the string named data. if i messed up or u need more help, just PM me or AIM me on my acount PlasmaGrunt. check out my new program HMMN. it just started beta test tho. hope my code helps :wink:

EDIT: This is VB 2005 Express, might work for VB6