save file dialog

  • Thread starter Thread starter GJP
  • Start date Start date
G

GJP

Hello.

Ive been asked to make my own notepade for college assignment.

All ig going well, but i cant get the save to work.

I can get Save a (shows dialog box), i can get it to just save too, but when
i try to put an IF in it to check if the file has already been saved before,
it will not save.

Private Sub mnuSave_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuSave.Click
'fname is a var, set it to file name
fname = SFD.FileName()
If fname > "0" Then
'save the file will current name
rtb1.SaveFile(SFD.FileName, RichTextBoxStreamType.RichText)
Else
'need to do a save as if got this far
'title box
SFD.Title = "Please chose a folder and a name for your file"
'rich text format
SFD.Filter = "Rich Text Files (*.rtf)|*.rtf"
SFD.FilterIndex = 1
result = SFD.ShowDialog
If result = DialogResult.OK Then
rtb1.SaveFile(SFD.FileName, RichTextBoxStreamType.RichText)
fname = SFD.FileName
End If
End If

End Sub

Any ideas? Thanks!
 
Hi GJP,

Normaly should this be enough using the save Dialog box
(but you can do more with it)
\\\
Dim sfd As New SaveFileDialog
sfd.Title = "Please chose a folder and a name for your file"
sfd.Filter = "Rich Text Files (*.rtf)|*.rtf"
sfd.FilterIndex = 1
If sfd.ShowDialog = DialogResult.OK Then
rtb1.SaveFile(sfd.FileName, RichTextBoxStreamType.RichText)
End If
///

If someone chooses a file that already exist the savefiledialog ask if it
has to be overwritten. And if he sais "save" (depends on your
langagesettings), that is with this routine done (and when it not exist it
is just written).

Cor
 
Cor said:
If someone chooses a file that already exist the savefiledialog ask if it
has to be overwritten. And if he sais "save" (depends on your
langagesettings), that is with this routine done (and when it not exist it
is just written).

Cor

Not sure if i explained my self properly before, I have a menu item ('Save')
that when you press it it will save the file. The code i posted (for
'Save') is suposed to check if you have saved the file before(by doing a
'Save As'), if you have, then it will save the file under that name('Save'),
but if you have not, it will ask you to name the file -ie, dialog box opens
(Imagine going to 'File' then 'Save' in note pad - it will ask you to give
it a name if it has not been saved before, but if you have already done a
'Save As', it will just save it).

If i do a 'save as' first, edit the text in rtb1, then click 'save' it will
work, but if i close the program, reopen it, edit the text in rtb1 and then
'save' it does not save it, i have messed around, its either the sub
'mnuSave_click' or the sub 'mnuOpen_click' that is giving me the problem,
but i cant work it out

Here the code:

''''I have the variable 'fname' that is used to check if the file already
has been saved (it will have a file name if it has)

''''Code for 'Save As':

Private Sub mnuSaveAs_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuSaveAs.Click
SFD.Title = "Please chose a folder and a name for your file"
SFD.Filter = "Rich Text Files (*.rtf)|*.rtf"
SFD.FilterIndex = 1
result = SFD.ShowDialog
If result = DialogResult.OK Then
rtb1.SaveFile(SFD.FileName, RichTextBoxStreamType.RichText)
fname = SFD.FileName
End If
End Sub

''''Code for 'Save'

Private Sub mnuSave_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuSave.Click
'fname is a var, set it to file name
fname = SFD.FileName()
If fname > "0" Then
'save the file will current name
rtb1.SaveFile(SFD.FileName, RichTextBoxStreamType.RichText)
Else
'need to do a save as
SFD.Title = "Please chose a folder and a name for your file"
SFD.Filter = "Rich Text Files (*.rtf)|*.rtf"
SFD.FilterIndex = 1
result = SFD.ShowDialog
If result = DialogResult.OK Then
rtb1.SaveFile(SFD.FileName, RichTextBoxStreamType.RichText)
fname = SFD.FileName
End If
End If
End sub

''''Code for open

Private Sub mnuOpen_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuOpen.Click

OFD.Title = "Please chose a folder and the name of your file"
OFD.Filter = "Rich Text Files (*.rtf)|*.rtf"
OFD.FilterIndex = 1
result = OFD.ShowDialog
If result = DialogResult.OK Then
rtb1.LoadFile(OFD.FileName)
fname = OFD.FileName
End If
Me.Text = OFD.FileName
End Sub
 
Hi GJP,

You mean something as this?
\\\
Private Sub mnuSave_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuSave.Click
If fname = sfd.FileName Then
'save the file will current name
rtb1.SaveFile(sfd.FileName, RichTextBoxStreamType.RichText)
Else
mnuSaveas_Click(Nothing, Nothing)
End If
End Sub
///

I optimezed it a little bit but the only thing that gives you the trouble is
I think this,
If fname > "0" Then

Why would the fname be something as "0"
 
Cor said:
Hi GJP,

You mean something as this?

Yeah, but if you click on 'Save' it opens the OFD, for some reason, fname is
not being set as the file name. Where should i put fname = file name bit of
code. I have it as:

Private Sub mnuOpen_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuOpen.Click
OFD.Title = "Please chose a folder and the name of your file"
OFD.Filter = "Rich Text Files (*.rtf)|*.rtf"
OFD.FilterIndex = 1
result = OFD.ShowDialog
If result = DialogResult.OK Then
rtb1.LoadFile(OFD.FileName)
fname = OFD.FileName
End If
Me.Text = OFD.FileName
End Sub

I put a break point on the line 'fname = ofd.filename' and fname showed up
as empty
Thanks
Gav.
 
Just seen that it does set fname as file name in the open sub, but in the
Save sub, file name has been changed to doc1, but i dont know how....
 
Hi GJP,

I hope it works now totaly, we changed the saveas, save and now the open, It
will be look nicer also.

Cor
 
Cor, you are a star!

Its working :)

Now that ive got that bit working i can get the 'New' and 'Exit' working to.
I tried doing it yestarday, but because the save sub was not working, it was
affecting the 'New' and 'Exit' subs.

Anyhow, thanks for your help, im new to VB, so would have taken me much
longer to work out the problems if you didnt help.

Ta

Gav.
 
Back
Top