Kamis, 31 Maret 2016

Save data VB.net to .txt

Hi Guys.... Now i will share my experience to make program on VB.net for save some data reciept with .txt format file.

I use VisualBasic 2010, first of all create new project. and save with a name what do you want. if me the name of my project is Form1.

and i am design my form to be like this .....

In this form have : TextBox, Label, Button, DateTimePicker and FileToolStripMenuItem.

Next........
- Double click on Save Button. copy and paste this code :

        Dim objStreamWriter As IO.StreamWriter
        Dim year = Format(CDate(DateAndTime.Now), "yMMddhhss")
        Dim NamaFile As String = "Reciept-" & TextBox4.Text & "-Date-" & year
        objStreamWriter = New System.IO.StreamWriter(My.Application.Info.DirectoryPath &   "\DATA\" & NamaFile & ".txt")
        objStreamWriter.WriteLine(TextBox5.Text)
        objStreamWriter.WriteLine(TextBox1.Text)
        objStreamWriter.WriteLine(TextBox2.Text)
        objStreamWriter.WriteLine(TextBox3.Text)
        objStreamWriter.WriteLine(DateTimePicker1.Value)
        objStreamWriter.WriteLine(TextBox4.Text)
        MsgBox("Data stored!")
        objStreamWriter.Close()

- Double click on Exit Button and copy & paste this code over there:

       Me.Close()

- The last, double click on FileToolStripMenuItem (Change the name to be "File") - and write "Import reciept" 
    - Double click on "Import reciept" 
       copy and paste this code:

       Dim vimport As New OpenFileDialog
        vimport.Filter = "Text File(*.txt)| *.txt"
        vimport.InitialDirectory = My.Application.Info.DirectoryPath & "\DATA\"
        If vimport.ShowDialog <> Windows.Forms.DialogResult.OK Then Exit Sub
        Dim myfile As String = vimport.FileName
        Dim readfile As New System.IO.StreamReader(myfile)
        TextBox5.Text = readfile.ReadLine
        TextBox1.Text = readfile.ReadLine
        TextBox2.Text = readfile.ReadLine
        TextBox3.Text = readfile.ReadLine
        DateTimePicker1.Value = readfile.ReadLine
        TextBox4.Text = readfile.ReadLine
        readfile.Close()


Okay for final press F5 your program is finish.. thank you for reading. if you have some problem or any question write in comment..

thank you Guys.... See you