save file dialog

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • GJP

    save file dialog

    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(B yVal sender As System.Object, ByVal e As
    System.EventArg s) 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(S FD.FileName, RichTextBoxStre amType.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(S FD.FileName, RichTextBoxStre amType.RichText )
    fname = SFD.FileName
    End If
    End If

    End Sub

    Any ideas? Thanks!
    --

    Do something amazing give Blood

    Register to be a blood donor, give blood and save lives. Find out more about blood donation.



  • Cor

    #2
    Re: save file dialog

    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(s fd.FileName, RichTextBoxStre amType.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


    Comment

    • GJP

      #3
      Re: save file dialog


      "Cor" <non@non.com> wrote in message
      news:%231XWqq3q DHA.920@TK2MSFT NGP10.phx.gbl.. .[color=blue]
      > 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
      >
      >[/color]

      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.EventArg s) 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(S FD.FileName, RichTextBoxStre amType.RichText )
      fname = SFD.FileName
      End If
      End Sub

      ''''Code for 'Save'

      Private Sub mnuSave_Click(B yVal sender As System.Object, ByVal e As
      System.EventArg s) 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(S FD.FileName, RichTextBoxStre amType.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(S FD.FileName, RichTextBoxStre amType.RichText )
      fname = SFD.FileName
      End If
      End If
      End sub

      ''''Code for open

      Private Sub mnuOpen_Click(B yVal sender As System.Object, ByVal e As
      System.EventArg s) 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(O FD.FileName)
      fname = OFD.FileName
      End If
      Me.Text = OFD.FileName
      End Sub


      Comment

      • Cor

        #4
        Re: save file dialog

        Hi GJP,

        You mean something as this?
        \\\
        Private Sub mnuSave_Click(B yVal sender As System.Object, ByVal e As
        System.EventArg s) Handles mnuSave.Click
        If fname = sfd.FileName Then
        'save the file will current name
        rtb1.SaveFile(s fd.FileName, RichTextBoxStre amType.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,
        [color=blue]
        > If fname > "0" Then[/color]

        Why would the fname be something as "0"
        [color=blue][color=green]
        > >[/color]
        >
        > Not sure if i explained my self properly before, I have a menu item[/color]
        ('Save')[color=blue]
        > 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[/color]
        name('Save'),[color=blue]
        > but if you have not, it will ask you to name the file -ie, dialog box[/color]
        opens[color=blue]
        > (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[/color]
        will[color=blue]
        > work, but if i close the program, reopen it, edit the text in rtb1 and[/color]
        then[color=blue]
        > '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.EventArg s) 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(S FD.FileName, RichTextBoxStre amType.RichText )
        > fname = SFD.FileName
        > End If
        > End Sub
        >
        > ''''Code for 'Save'
        >
        > Private Sub mnuSave_Click(B yVal sender As System.Object, ByVal e As
        > System.EventArg s) 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(S FD.FileName, RichTextBoxStre amType.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(S FD.FileName,[/color]
        RichTextBoxStre amType.RichText )[color=blue]
        > fname = SFD.FileName
        > End If
        > End If
        > End sub
        >
        > ''''Code for open
        >
        > Private Sub mnuOpen_Click(B yVal sender As System.Object, ByVal e As
        > System.EventArg s) 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(O FD.FileName)
        > fname = OFD.FileName
        > End If
        > Me.Text = OFD.FileName
        > End Sub
        >
        >[/color]


        Comment

        • Cor

          #5
          Re: save file dialog

          Hi GJP,

          You mean something as this?
          \\\
          Private Sub mnuSave_Click(B yVal sender As System.Object, ByVal e As
          System.EventArg s) Handles mnuSave.Click
          If fname = sfd.FileName Then
          'save the file will current name
          rtb1.SaveFile(s fd.FileName, RichTextBoxStre amType.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,
          [color=blue]
          > If fname > "0" Then[/color]

          Why would the fname be something as "0"
          [color=blue][color=green]
          > >[/color]
          >
          > Not sure if i explained my self properly before, I have a menu item[/color]
          ('Save')[color=blue]
          > 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[/color]
          name('Save'),[color=blue]
          > but if you have not, it will ask you to name the file -ie, dialog box[/color]
          opens[color=blue]
          > (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[/color]
          will[color=blue]
          > work, but if i close the program, reopen it, edit the text in rtb1 and[/color]
          then[color=blue]
          > '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.EventArg s) 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(S FD.FileName, RichTextBoxStre amType.RichText )
          > fname = SFD.FileName
          > End If
          > End Sub
          >
          > ''''Code for 'Save'
          >
          > Private Sub mnuSave_Click(B yVal sender As System.Object, ByVal e As
          > System.EventArg s) 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(S FD.FileName, RichTextBoxStre amType.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(S FD.FileName,[/color]
          RichTextBoxStre amType.RichText )[color=blue]
          > fname = SFD.FileName
          > End If
          > End If
          > End sub
          >
          > ''''Code for open
          >
          > Private Sub mnuOpen_Click(B yVal sender As System.Object, ByVal e As
          > System.EventArg s) 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(O FD.FileName)
          > fname = OFD.FileName
          > End If
          > Me.Text = OFD.FileName
          > End Sub
          >
          >[/color]


          Comment

          • GJP

            #6
            Re: save file dialog


            "Cor" <non@non.com> wrote in message
            news:ugCeL36qDH A.1880@TK2MSFTN GP09.phx.gbl...[color=blue]
            > Hi GJP,
            >
            > You mean something as this?[/color]

            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(B yVal sender As System.Object, ByVal e As
            System.EventArg s) 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(O FD.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.


            Comment

            • GJP

              #7
              Re: save file dialog


              "Cor" <non@non.com> wrote in message
              news:ugCeL36qDH A.1880@TK2MSFTN GP09.phx.gbl...[color=blue]
              > Hi GJP,
              >
              > You mean something as this?[/color]

              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(B yVal sender As System.Object, ByVal e As
              System.EventArg s) 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(O FD.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.


              Comment

              • GJP

                #8
                Re: save file dialog

                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....

                "GJP" <gavin@gMYSECON DNAME4397.fsnet .co.uk> wrote in message
                news:bp60bo$cj3 $1@newsg2.svr.p ol.co.uk...
                [color=blue]
                > Yeah, but if you click on 'Save' it opens the OFD, for some reason, fname[/color]
                is[color=blue]
                > not being set as the file name. Where should i put fname = file name bit[/color]
                of[color=blue]
                > code. I have it as:
                >
                > Private Sub mnuOpen_Click(B yVal sender As System.Object, ByVal e As
                > System.EventArg s) 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(O FD.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[/color]
                up[color=blue]
                > as empty
                > Thanks
                > Gav.
                >
                >[/color]


                Comment

                • GJP

                  #9
                  Re: save file dialog

                  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....

                  "GJP" <gavin@gMYSECON DNAME4397.fsnet .co.uk> wrote in message
                  news:bp60bo$cj3 $1@newsg2.svr.p ol.co.uk...
                  [color=blue]
                  > Yeah, but if you click on 'Save' it opens the OFD, for some reason, fname[/color]
                  is[color=blue]
                  > not being set as the file name. Where should i put fname = file name bit[/color]
                  of[color=blue]
                  > code. I have it as:
                  >
                  > Private Sub mnuOpen_Click(B yVal sender As System.Object, ByVal e As
                  > System.EventArg s) 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(O FD.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[/color]
                  up[color=blue]
                  > as empty
                  > Thanks
                  > Gav.
                  >
                  >[/color]


                  Comment

                  • Cor

                    #10
                    Re: save file dialog

                    Hi GJP,

                    I hope it works now totaly, we changed the saveas, save and now the open, It
                    will be look nicer also.
                    [color=blue][color=green]
                    > >
                    > > Private Sub mnuOpen_Click(B yVal sender As System.Object, ByVal e As
                    > > System.EventArg s) 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(O FD.FileName)
                    > > fname = OFD.FileName[/color][/color]
                    sfd.FileName = ofd.FileName[color=blue][color=green]
                    > > End If
                    > > Me.Text = OFD.FileName
                    > > End Sub
                    > >[/color][/color]

                    Cor


                    Comment

                    • GJP

                      #11
                      Re: save file dialog

                      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.


                      "Cor" <non@non.com> wrote in message
                      news:Omc1zgBrDH A.1408@TK2MSFTN GP11.phx.gbl...[color=blue]
                      > Hi GJP,
                      >
                      > I hope it works now totaly, we changed the saveas, save and now the open,[/color]
                      It[color=blue]
                      > will be look nicer also.
                      >[color=green][color=darkred]
                      > > >
                      > > > Private Sub mnuOpen_Click(B yVal sender As System.Object, ByVal e As
                      > > > System.EventArg s) 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(O FD.FileName)
                      > > > fname = OFD.FileName[/color][/color]
                      > sfd.FileName = ofd.FileName[color=green][color=darkred]
                      > > > End If
                      > > > Me.Text = OFD.FileName
                      > > > End Sub
                      > > >[/color][/color]
                      >
                      > Cor
                      >
                      >[/color]


                      Comment

                      Working...