|
-
Mar 26th, 2007, 03:10 PM
#1
Thread Starter
New Member
Saving/opening issues...
Hello there everyone!
I'm michael and i'm 16.
I started using VB a couple of days ago and have found it quite an interesting thing to use. i have created my own program (very simple one i might add ) which works as i would like it to. i do however have a few slight problems.
I have added a menu strip bar to my program, added File >new>save etc. and have begun to code it so that the buttons actually work. i have got as far as the Save(/open)FileDialog opening but i don't quite know how to make the rest work. this is the code i have for the save/openFileDialog:
Save:
Code:
SaveFileDialog1.InitialDirectory = "C:\"
SaveFileDialog1.Title = "Save a Text File"
SaveFileDialog1.Filter = "Text Files(*.txt)|*.txt"
SaveFileDialog1.FileName = ""
SaveFileDialog1.OverwritePrompt = True
Open:
Code:
OpenFileDialog1.InitialDirectory = "C:\"
OpenFileDialog1.Title = "Open a Text File"
OpenFileDialog1.Filter = "Text Files(*.txt)|*.txt"
OpenFileDialog1.FileName = ""
OpenFileDialog1.ShowDialog()
I found these codes on a website for saving and opening, but i don't quite know how to incorporate them into what i already have:
To save:
Code:
Dim FILE_NAME As String = "C:\test.txt"
Dim i As Integer
Dim aryText(4) As String
aryText(0) = (start.Text)
aryText(1) = (num1.Text)
aryText(2) = (num1.Text)
aryText(3) = (ans.Text)
aryText(4) = (time.Text)
Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
For i = 0 To 4
objWriter.WriteLine(aryText(i))
Next
objWriter.Close()
As i understand, this would save the file as "text.txt"? however i would have liked the user to be able to name the file themselves, hense the first code i posted. i also understand that this code writes the text on different lines? this is important because my form has various text boxes so the different lines need to go in the correct boxes.
To OpeN:
Code:
Dim FILE_NAME As String = "C:\test.txt"
Dim TextLine As String
If System.IO.File.Exists(FILE_NAME) = True Then
Dim objReader As New System.IO.StreamReader(FILE_NAME)
Do While objReader.Peek() <> -1
TextLine = TextLine & objReader.ReadLine() & vbNewLine
Loop
Textbox1.Text = TextLine
Else
MsgBox("File Does Not Exist")
End If
again, does this specficially open "test.txt" and how do i get around this?
Apologies for the extremely long post and many thanks to anybody that can help me!
Regards
Michael
-
Mar 26th, 2007, 03:16 PM
#2
Re: Saving/opening issues...
Yes it specifically opens Text.txt. Use the OpenFileDialog1.FileName to get the selected file from the users choice.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Mar 26th, 2007, 03:26 PM
#3
Fanatic Member
Re: Saving/opening issues...
Damn, that has got to be the best first post I have ever seen... seriously. He explained what he knew, asked straightforward questions, and posted some of his code. And he got a reply in six minutes. Someone should sticky this to the top of the forum. I think it should be required reading.
VB.Net 2008
.Net Framework 2.0
"Must you breathe? 'Cause I need heaven..."
-
Mar 27th, 2007, 09:28 AM
#4
Thread Starter
New Member
Re: Saving/opening issues...
lol, thx experience i have read some previous posts and seen how annoyed you guys feel whe people don't post code etc didn't want to seem like one of those people 
with the code, i have replaced the "C:\test.txt" with OpenFileDialog1.FileName like robdogg said earlier but i am now getting an error with these lines:
Code:
Dim objReader As New System.IO.StreamReader(FILE_NAME)
and:
Code:
Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
it says
A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
does anybody know what is wrong with them?
thanks again
-
Mar 27th, 2007, 03:36 PM
#5
Re: Saving/opening issues...
What you need to do is to have a button (or some other control) and when a user click it, you show him/her a open file dialog (or save file dialog) then check the dialog result. If the result is OK then you read the file name and do your stuff. This is sample code to open a text file and read the text into textbox1. Code for using savefiledialog is very much similar.
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ofd As New OpenFileDialog
With ofd
.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
.Filter = "Text Files (*.txt)|*.txt"
.CheckFileExists = True
.Multiselect = False
End With
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
TextBox1.Text = IO.File.ReadAllText(ofd.FileName)
End If
End Sub
-
Mar 27th, 2007, 04:14 PM
#6
Thread Starter
New Member
Re: Saving/opening issues...
Cool, this is sort of working, except for a couple of things.
the open dialog opens fine, but when you go to open the file, it opens but then the open dialog opens again. so if you double click a file, it puts the text into textbox1 but then opens the opendialog again.
also, is there a way to change the code so that it opens so that each line of the text file goes in a different textbox, because i have different textboxes that need the information on the different lines to go in them.
But many thanks for this Stanav
-
Mar 27th, 2007, 04:15 PM
#7
Re: Saving/opening issues...
Did you use the code exactly as posted? Post your code. It shouldnt be opening twice.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Mar 27th, 2007, 04:23 PM
#8
Thread Starter
New Member
Re: Saving/opening issues...
Ahh sorted to double opening thing. forgot to delete my initial code
so is there a way to open the file with different lines going into different boxes?
-
Mar 27th, 2007, 04:53 PM
#9
Re: Saving/opening issues...
You would have to read in the file one line at a time using a streamreader, just one way, and redirecting the contents to whatever textboxes/controls you want.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|