-
Feb 6th, 2012, 11:15 PM
#1
Thread Starter
Lively Member
Help: Save Solution
Im trying to save a file in XML and offer the user a save dialog to appear so they can name the file. The file will save using a custom file extention. The problem is I keep getting off the wall suggestions and Im looking for a simple solution. Here is what I have, please only show examples using my syntax based code. Im still a bit new to Vb so posting off the wall examples will not help in any way shape or form thank you.
Code:
Private FileDialog As SaveFileDialog
Public Sub New()
InitializeComponent()
FileDialog = New SaveFileDialog()
FileDialog.Filter = "UOK Application | *.App"
FileDialog.DefaultExt = "app"
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim AppSerializer As Xml.Serialization.XmlSerializer = New Xml.Serialization.XmlSerializer(GetType(Storage))
Dim fileStream As System.IO.Stream = FileDialog.OpenFile()
Dim SW As New System.IO.StreamWriter(FileDialog)
SaveFileDialog1.ShowDialog()
AppSerializer.Serialize(SW, Storage)
SW.Close()
Me.Update()
End Sub
-
Feb 6th, 2012, 11:45 PM
#2
Re: Help: Save Solution
Is there a problem with the code itself ? Is it not saving or something ?
-
Feb 7th, 2012, 12:02 AM
#3
Thread Starter
Lively Member
Re: Help: Save Solution
Dim SW As New System.IO.StreamWriter(FileDialog)
Syntax error on the SW
Error 1 Overload resolution failed because no accessible 'New' can be called with these arguments:
'Public Sub New(path As String)': Value of type 'System.Windows.Forms.SaveFileDialog' cannot be converted to 'String'.
'Public Sub New(stream As System.IO.Stream)': Value of type 'System.Windows.Forms.SaveFileDialog' cannot be converted to 'System.IO.Stream'. C:\Users\Admin\documents\visual studio 2010\Projects\UOK Software Intern Application\UOK Software Intern Application\MainForm.vb 35 13 UOK Software Intern Application
If you remove the "New" is generates more errors so Im lost and Im sure there is a easy way to accomplish what I need as described above. Im just too newb to figure it out.
-
Feb 7th, 2012, 01:49 AM
#4
Re: Help: Save Solution
Whoa whoa, I just really looked at your code and you got a lot of things wrong. You really are new at this. You were mismatching data types left and right. However it was a great effort for someone so new. Here is what your code should look like:-
vbnet Code:
Public Class Form1
Private g_data As New Storage With {.SampleData = "Hello"}
Private FileDialog As SaveFileDialog
Public Sub New()
InitializeComponent()
FileDialog = New SaveFileDialog()
FileDialog.Filter = "UOK Application | *.app"
FileDialog.DefaultExt = "app"
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim dr As DialogResult = FileDialog.ShowDialog()
Dim fileName As String = FileDialog.FileName
Dim AppSerializer As Xml.Serialization.XmlSerializer = New Xml.Serialization.XmlSerializer(GetType(Storage))
Dim fileStream As New System.IO.FileStream(fileName, IO.FileMode.Create)
Dim SW As New System.IO.StreamWriter(fileStream)
AppSerializer.Serialize(SW, g_data)
SW.Close()
End Sub
End Class
Public Class Storage
Public SampleData As String
End Class
Now I dont know what your storage class looks like so I had to make my own to help me to work this out for you so use this code as a template so you can get your own working right.
-
Feb 7th, 2012, 10:17 AM
#5
Thread Starter
Lively Member
Re: Help: Save Solution
Thanks man, thats way better then what the MSDN kept trying to suggest after I told them a million times I wanted something I could understand. Storage is a class I created so I could save the data to a XML document. Because my form uses multipal text boxes and various pages, I needed to make a class to store it all. Ill post a video demo of the software once I get this plugged in. Again though thanks, I hope this works lol
Edit:
Tested it and it works. Awsome stuff guys.
Last edited by mholmes_3038; Feb 7th, 2012 at 10:27 AM.
-
Feb 7th, 2012, 02:54 PM
#6
Thread Starter
Lively Member
Re: Help: Save Solution
Arg now the load does nto want to work. I had it working bit now its throwing a exception. Here is the load code.
Code:
Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
'Load Action
Dim AppSerializer As Xml.Serialization.XmlSerializer = New Xml.Serialization.XmlSerializer(GetType(Storage))
Dim AppFileStream As FileStream = New FileStream(".App", FileMode.Open)
OpenFileDialog1.Title = "Windows Explorer"
OpenFileDialog1.ShowDialog()
Storage = CType(AppSerializer.Deserialize(AppFileStream), Storage)
FormUpdate()
AppFileStream.Close()
End Sub
System.IO.FileNotFoundException was unhandled
FileName=C:\Users\Admin\documents\visual studio 2010\Projects\UOK Software Digital Application\UOK Software Digital Application\bin\Debug\.App
Message=Could not find file 'C:\Users\Admin\documents\visual studio 2010\Projects\UOK Software Digital Application\UOK Software Digital Application\bin\Debug\.App'.
Source=mscorlib
StackTrace:
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode)
at UOK_Software_Digital_Application.Page_1.OpenToolStripMenuItem_Click(Object sender, EventArgs e) in C:\Users\Admin\documents\visual studio 2010\Projects\UOK Software Digital Application\UOK Software Digital Application\Page_1.vb:line 13
at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ToolStrip.WndProc(Message& m)
at System.Windows.Forms.ToolStripDropDown.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoCompo nentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at UOK_Software_Digital_Application.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
-
Feb 8th, 2012, 08:47 AM
#7
Re: Help: Save Solution
vbnet Code:
Dim AppFileStream As FileStream = New FileStream(".App", FileMode.Open)
You didnt provide a proper file name here.
-
Feb 8th, 2012, 10:20 PM
#8
Thread Starter
Lively Member
Re: Help: Save Solution
Ahhh
New FileStream(filename, FileMode.Open)
work?
-
Feb 12th, 2012, 01:31 PM
#9
-
Feb 19th, 2012, 07:20 PM
#10
Thread Starter
Lively Member
Re: Help: Save Solution
getting a error on the load action
Could not find file 'C:\Users\Admin\documents\visual studio 2010\Projects\UOK Software Digital Application\UOK Software Digital Application\bin\Debug\.app'.
Code:
Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
'Load Action
Dim AppSerializer As Xml.Serialization.XmlSerializer = New Xml.Serialization.XmlSerializer(GetType(Storage))
Dim AppFileStream As FileStream = New FileStream(".app", FileMode.Open)
OpenFileDialog1.Filter = "UOK Application | *.App"
OpenFileDialog1.DefaultExt = "app"
OpenFileDialog1.Title = "Windows Explorer"
OpenFileDialog1.ShowDialog()
Storage = CType(AppSerializer.Deserialize(AppFileStream), Storage)
FormUpdate()
AppFileStream.Close()
End Sub
Edit:
well i got it to work but not a fan of the result. I have to have a file called .app inside there or it will crash. suggestions?
Last edited by mholmes_3038; Feb 19th, 2012 at 07:26 PM.
-
Feb 20th, 2012, 07:24 AM
#11
Re: Help: Save Solution
vbnet Code:
Dim AppFileStream As FileStream = New FileStream(".app", FileMode.Open)
You made the same mistake again. ".app" is not a proper file name.
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
|