Problem with compiling SaveAs code
I am trying to write the code for the Save As function in my program.
I can get the SaveDialogBox up, and if the filename exists, that part works OK, and if you hit Cancel, that works OK.
But the code won't compile because it sees FileMode and FileAccess as undeclared.
Also the FileNotFoundException doesn't seem to be part of this set, and is not recognized. What am I missing?
Anyone know?
If not, does anyone have any canned code to execute the SaveAs DialogBox?
Private Sub MenuItem7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem7.Click
Dim fileChooser As SaveFileDialog = New SaveFileDialog
Dim result As DialogResult = filechooser.ShowDialog
Dim fileName As String
Dim output As FileStream
fileChooser.CheckFileExists = False
If result = DialogResult.Cancel Then
Return
End If
fileName = fileChooser.FileName
If (fileName = "" OrElse fileName = Nothing) Then
MessageBox.Show("Invalid File Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
Try
output = New FileStream(fileName, FileMode.OpenorCreate, FileAccess.Write)
Catch fileException As FileNotFoundException
MessageBox.Show("File Not Found", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
End Sub
Re: still get compile errors
Quote:
Originally posted by jtenright
When I added the Imports System.IO, I noticed that one of the exceptions went away - concerning the FileNotFoundException, which previously was not recognized. However,the exception I can't get past is this which occurs on this line of code :
output = New FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write)
The exception that is raised when trying to compile is "Too many arguments for Public Sub New()"
The following is what is in Public Sub New()
Imports System.IO
Imports System.Windows.Forms
Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
'MyBase.New()
Me.SaveFileDialog1.Filter = "Text Files|*.txt|RTF files|*.rtf|All files|*.*"
Me.SaveFileDialog1.InitialDirectory = "C:/Documents and Settings/Owner/My Documents"
Me.OpenFileDialog1.Filter = "Text Files|*.txt|RTF files|*.rtf|All files|*.*"
Me.OpenFileDialog1.InitialDirectory = "C:/Documents and Settings/Owner/My Documents"
'Add any initialization after the InitializeComponent() call
End Sub
What is wrong?
I dotn know the reason for error but what you have in sub new is pretty bad:D fist of all uncomment the mybase.new, second add teh InitializeComponent function after it... otherwise your controls wont show up
hmm that line of code should be working fine and I dont think sub new should be raising any errors. more code maybe?:D