Avatar billede geiri Nybegynder
09. december 2002 - 18:34 Der er 6 kommentarer

how to save

hi, im beginner in vb6 (reading sams teach yourself vb in 21 days)and i really want to know how i can save text.
my code
Private Sub Command1_Click()
  CommonDialog1.Filter = "All files(*.*) | *.*|text files(*.txt)|*.txt"
  CommonDialog1.FilterIndex = 1
      CommonDialog1.ShowSave
      openfile (CommonDialog1.FileName)
End Sub

I hope you can help me out.
thx
Avatar billede lovix Nybegynder
09. december 2002 - 20:23 #1
To save text is actually quite simpel!

Open "C:\The_File.txt" for output as #1
print #1, The_Text
close #1

          'To open text:
Dim LoadText as String      'It must be String!!

Open "C:\The_File.txt" for input as #1
line input #1, LoadText
Close #1

    Hope it helps!

-LoViX-
Avatar billede lovix Nybegynder
09. december 2002 - 20:25 #2
To show the loaded text just write:

MsgBox "This is the loaded text:" & VbCrlf & LoadText
-
Avatar billede martin_moth Mester
10. december 2002 - 17:48 #3
If you want to use the commondialog, you should know, that it does nothing but setting some properties, eg:

  CommonDialog1.ShowSave

shows the "save file dialog box" - but no file is saved when you press "OK" to dismiss the dialog box!!! You have to do that yourself:

  CommonDialog1.ShowSave
  Open Commondialog.filename for output as #1
  print #1, "Some text which is saved in the file"
  close #1

As you can see, after showing the dialogbox by uding .showSave, the property .Filename has a value, which is the name and path of the file you chose in the dialog box.  You can use this property as shown in the example.

So just remember, if you are using the commondialog to save or open a file, print something or something else, NOTHING HAPPENS when you press "OK". All the commondialog does is returning come values (eg. Commondialog.Filename), which you can use in the code that should allways follow after the use of a commondialog

Got it? Else, ask again ;o)

PS: You can change the appearance of your commondialog by using certain "flags", try looking in the "Flags Property (All Common Dialogs)" section of your help file

/Martin
Avatar billede kantoraza Nybegynder
10. december 2002 - 19:00 #4
I am, with this code, just finishing the work you, and the others, have done. Plus adding some new procedures, and so on. I hope you will enjoy the code! :-)

Private Sub Command1_Click()
' Shows the Common Dialog control
CommonDialog1.InitDir = "c:\"
    CommonDialog1.Filter = "All files(*.*) | *.*|text files(*.txt)|*.txt"
      CommonDialog1.FilterIndex = 2
          CommonDialog1.ShowSave
     
' If the user press 'cancel'
If Len(CommonDialog1.FileName) < 1 Then
    Exit Sub
End If

' Save the current text (in this case Text1 is the current text)
Dim Filename As String
FileName = CommonDialog1.FileName

' This line is NOT a Visual Basic command, but
' A self-made procedure!
SaveFile CommonDialog1.FileName, Text1.Text

Me.Caption = "File Saved"
End Sub


Public Sub SaveFile(Where As String, What As String)
    Open Where For Output As #5
        Print #5, What
    Close #5
End Sub

Written by NorthCode .:. NorthCode@e-mail.dk | www.NorthCode.tk
Avatar billede martin_moth Mester
11. december 2002 - 09:59 #5
Nortcode > It is allways more safe to use Freefile instead of a fixed number:

Public Sub SaveFile(Where As String, What As String)
  dim filenumber as integer   
  filenumber = Freefile 
  Open Where For Output As #filenumber
    Print #filenumber, What
  Close #filenumber
End Sub

:o)
Avatar billede kantoraza Nybegynder
13. december 2002 - 16:52 #6
Thanks!
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester