• home
  • forum
  • my
  • kt
  • download
  • Save files in a textbox

    Author: 2008-09-13 09:25:51 From:

    When you've done that, it should prompt you with a dialog box asking you what type of project you want to make. Choose Standard EXE and press open.

    First Step.

    Once you have done that, you should have something that looks like this:

    Step 2.

    Now we are going to set up the design of the application. To do this we are going to need one textbox, one command button and one Common Dialog.

    So go over to your toolbox (the thing that says "General" and has lots of little icons), and select the command button (the one circled in red in the picture below) and create one of them on the form. Then select the textbox (the one circled in green) and create one of them on the form.

    Step 3

    Step4.2

    Now we need to insert the common dialog into the toolbox. We do this because it is not there when we start up Visual Basic 6.0. So go ahead and press CONTROL+T. This will bring up the components window. Scrooooolll down until you find something that says, "Microsoft Common Dialog Control 6.0" or similar. Select it and press OK.

    Step 5

    You will notice a new icon in your toolbox. Click it and drag it onto your form.

    Step 6

    Now we need to name these objects. To do this, click on the textbox and go to the properties dialog (it's on the right hand side).

    Step 7

    Go to the name arena. Change the value from Text1 to txt_Input. Do the same for the command button. But instead, call it cmd_save. And the same for the Common Control, except call it CD1.

    You can now, also, change the caption of the Command button to Save or whatever you like. Do this the same way we did for the name. Except look for "Caption" instead of Name. You can change the value of the text box as well. Click on it and scroll down to Text on the properties dialog. Change it to whatever you want. You can always change it when the application is running.

    .Now for the fun part, coding!

    Double click on the command button. It should come up with code like this:

    Private Sub cmd_save_Click()

    End Sub

    In the middle of this, insert this code

    Dim hFile As Long
    Dim
    sFilename As String
    CD1.ShowSave
    sFilename = CD1.filename

    hFile = FreeFile
    Open sFilename For Output As #hFile
    Print #hFile, txt_input.Text
    Close #hFile

    You should now have this code

    Private Sub cmd_save_Click()
    Dim hFile As Long
    Dim
    sFilename As String
    CD1.ShowSave
    sFilename = CD1.filename

    hFile = FreeFile
    Open sFilename For Output As #hFile
    Print #hFile, txt_input.Text
    Close #hFile

    End Sub

    What this is telling the application to do is, when you press the command button it is dimming sFilename as a string. This means we can use the word filename throughout our project and it will recognize it for what the value is. We made the value CD1.Filename. We did this by typing sFilename = CD1.filename

    Then we had CD1.ShowSave. This is telling the common dialog to show the save dialog.

    Then we have

    hFile = FreeFile
    Open sFilename For Output As #hFile
    Print #hFile, txt_input.Text
    Close #hFile

    This is telling the app to open "sFilename" (remember that it is a string. So it's the name that you selected when the Common idalog showed up) for output. And then it's saying to print the text in "txt_input.text" to the file. You could also use "Write" instead of Print. But this will ad Quotes to the file.

    Now press F5. This will run your application. Alternitivly you can press this button (the one highlighted in blue):

    You should be able too, now, when your application is running, be able to press one button and it opens a save dialog. You should then choose a file oe make one. Then click save. It should then save the text that you typed to that file.

    When you are done with your application, exit out of it and save.

     

    Download Project

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      .NET (8)
      Buttons (3)
      Database Related (7)
      Date and Time (1)
      Development (3)
      Error Handling (2)
      File Manipulation (5)
      Introduction to Visual Basic (17)
      Miscellaneous (3)
      Multimedia (10)
      Networking (10)
      Security (1)
      VB Script (6)

    New

    Hot