Bank Management System using VB.NET and CSV file


How to Create a Bank Management System using VB.NET and CSV file.

สอนฟรี VB.NET ทำงานกับ CSV file หรือ Text file สร้างโปรแกรมจำลองการทำงานของระบบบัญชีธนาคาร เช่น สร้างบัญชี ลบบัญชี ค้นหา ฝาก ถอน โอน เป็นต้น.

Part 1/3

Part 2/3

Part 3/3


Free VB.NET Source Code by iBasskung.

Module: MyModule.vb


Module MyModule


    Public InfoOKOnly As MsgBoxStyle = vbInformation + vbOKOnly

    Public AppTitle As String = "Code A Minute : iBasskung Tutorial"


    Public CreatedDate As String = Date.Now.ToShortDateString

    Public CreatedTime As String = TimeOfDay.ToString("hh:mm:ss")


    Public Duplicate As Boolean = False


    Public caption As String = "Code A Minute : iBasskung Tutorial"

    Public buttons As Integer = MessageBoxButtons.YesNo

    Public icons As Integer = MessageBoxIcon.Question


End Module


Class: MyClassFiles.vb


Public Class MyClassFiles


    Public ReadOnly Property GetMasterFile As String

        Get

            Return Application.StartupPath + "\Account-Master.txt"

        End Get

    End Property


    Public ReadOnly Property GetTransactionFile As String

        Get

            Return Application.StartupPath + "\Account-Transaction.txt"

        End Get

    End Property


    Public ReadOnly Property GetTempFile As String

        Get

            Return Application.StartupPath + "\TempFile.txt"

        End Get

    End Property


End Class


Class: MyAccountClass.vb


Public Class MyAccountClass


    Private _AccountNo As String

    Private _AccountName As String

    Private _PhoneNo As String

    Private _Amount As Double

    Private _ImagePath As String

    Private _ImageName As String

    Private _Status As String


    Public Sub New()

    End Sub


    Public Sub New(ByVal ANo As String, ByVal ANa As String, ByVal PNo As String,

                   ByVal Am As Double, ByVal IPa As String, ByVal INa As String,

                   ByVal Sta As String)

        Me._AccountNo = ANo

        Me._AccountName = ANa

        Me._PhoneNo = PNo

        Me._Amount = Am

        Me._ImagePath = IPa

        Me._ImageName = INa

        Me._Status = Sta

    End Sub


    Public Property AccountNo As String

        Get

            Return _AccountNo

        End Get

        Set(value As String)

            _AccountNo = value

        End Set

    End Property


    Public Property AccountName() As String

        Get

            Return _AccountName

        End Get

        Set(value As String)

            _AccountName = value

        End Set

    End Property


    Public Property PhoneNo() As String

        Get

            Return _PhoneNo

        End Get

        Set(value As String)

            _PhoneNo = value

        End Set

    End Property


    Public Property Amount() As Double

        Get

            Return _Amount

        End Get

        Set(value As Double)

            _Amount = value

        End Set

    End Property


    Public Property ImagePath() As String

        Get

            Return _ImagePath

        End Get

        Set(value As String)

            _ImagePath = value

        End Set

    End Property


    Public Property ImageName() As String

        Get

            Return _ImageName

        End Get

        Set(value As String)

            _ImageName = value

        End Set

    End Property


    Public Property Status() As String

        Get

            Return _Status

        End Get

        Set(value As String)

            _Status = value

        End Set

    End Property


End Class


Module: ModuleTextBoxValidating.vb


Module ModuleTextBoxValidating


    Public QCFailed As Boolean = False


    Public Sub TextBoxValidating(ByVal Tex As TextBox)


        If Tex.Text = "" Or String.IsNullOrEmpty(Tex.Text) Then

            QCFailed = True

            Return

        End If


        If Not IsNumeric(Tex.Text) Then

            MsgBox("Incorrect Format...", InfoOKOnly, AppTitle)

            Tex.Focus()

            QCFailed = True

            Return

        End If


        If Tex.Name = "TextBoxAccountID" Then

            Dim accID As String = Tex.Text


            If accID.Length <> 10 Then

                MsgBox(accID.Length & " : Please input only 10 digit numbers...", InfoOKOnly, AppTitle)

                Tex.Focus()

                QCFailed = True

                Return

            End If


            Dim value As Long

            Dim IsLng As Boolean = Long.TryParse(accID, value)


            If Not (IsLng) Then

                MsgBox("Integer Only Please...", InfoOKOnly, AppTitle)

                Tex.Focus()

                QCFailed = True

                Return

            End If


        ElseIf Tex.Name = "TextBoxBalance" Or Tex.Name = "TextBoxAmount" Then

            Dim Amount As String = Tex.Text


            Dim DblAMount As Double = CType(Tex.Text, Double)


            'If DblAMount < 500 Then

            '    MsgBox("Amount must be greater than or equal to 500", InfoOKOnly, AppTitle)

            '    Tex.Focus()

            '    Return

            'End If


            Tex.Text = Format(DblAMount, "#,##0.00")


        End If


        QCFailed = False


    End Sub


End Module


Form: Form1.vb


Public Class FormNewAccount


    Private ImgPath As String = ""

    Private ImgName As String = ""


    Private Sub ButtonCancel_Click(sender As Object, e As EventArgs) Handles ButtonCancel.Click

        Me.Close()

    End Sub


    Private Sub EnableControls()

        ButtonSave.Enabled = True

        TextBoxAccountName.Enabled = True

        TextBoxPhoneNo.Enabled = True

        TextBoxAmount.Enabled = True

    End Sub


    Private Sub DisableControls()

        ButtonSave.Enabled = False

        TextBoxAccountName.Enabled = False

        TextBoxPhoneNo.Enabled = False

        TextBoxAmount.Enabled = False

    End Sub


    Private Sub ClearTextBoxes()

        TextBoxAccountNo.Text = ""

        TextBoxAccountName.Text = ""

        TextBoxPhoneNo.Text = ""

        TextBoxAmount.Text = ""

        TextBoxImageFile.Text = ""

        PictureBox1.Image = Nothing

    End Sub


    Private Sub FormNewAccount_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim cf As MyClassFiles

        cf = New MyClassFiles()


        If Not (My.Computer.FileSystem.FileExists(cf.GetMasterFile)) Or

           Not (My.Computer.FileSystem.FileExists(cf.GetTransactionFile)) Then

            FileOpen(1, cf.GetMasterFile, OpenMode.Output)

            FileOpen(2, cf.GetTransactionFile, OpenMode.Output)


            '-- Close before reopening in another mode.

            FileClose(1)

            FileClose(2)


            MsgBox("The files have been created successfully!", InfoOKOnly, AppTitle)

        End If

    End Sub


    Private Sub TextBoxAccountNo_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles TextBoxAccountNo.Validating


        If TextBoxAccountNo.Text = "" Or String.IsNullOrEmpty(TextBoxAccountNo.Text) Then Return


        Dim accNo As String = TextBoxAccountNo.Text


        If (accNo.Length <> 10) Then

            DisableControls()

            MsgBox(accNo.Length & " : Please input only 10 digit numbers...", InfoOKOnly, AppTitle)

            TextBoxAccountNo.Focus()

            Return

        End If


        If Not IsNumeric(accNo) Then

            DisableControls()

            MsgBox("Incorrect format...", InfoOKOnly, AppTitle)

            TextBoxAccountNo.Focus()

            Return

        End If


        Dim value As Long

        Dim IsLng As Boolean = Long.TryParse(accNo, value)


        If Not (IsLng) Then

            DisableControls()

            MsgBox("Integer only please!", InfoOKOnly, AppTitle)

            TextBoxAccountNo.Focus()

            Return

        End If


        Dim Acc As String = ""

        Dim cf As MyClassFiles

        cf = New MyClassFiles()


        '-- Open file for input.

        FileOpen(1, cf.GetMasterFile, OpenMode.Input)


        '-- Check for end of file (EOF)

        Do While Not EOF(1)

            Input(1, Acc)

            '-- Identifying Duplicate Account No.

            If (Acc = TextBoxAccountNo.Text) Then

                Duplicate = True

                Exit Do

            End If

        Loop


        FileClose(1)


        If (Duplicate) Then

            DisableControls()

            MsgBox("Duplicate Account Number!", InfoOKOnly, AppTitle)

            Duplicate = False

            Return

        End If


        EnableControls()


    End Sub


    Private Sub TextBoxAmount_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles TextBoxAmount.Validating


        If TextBoxAmount.Text = "" Or String.IsNullOrEmpty(TextBoxAmount.Text) Then Return


        Dim Amount As String = TextBoxAmount.Text


        If Not IsNumeric(Amount) Then

            MsgBox("Incorrect Format!...", InfoOKOnly, AppTitle)

            TextBoxAmount.Focus()

            Return '- Exit Sub

        End If


        Dim DblAmount As Double = CType(Amount, Double)

        If (DblAmount < 500) Then

            MsgBox("Amount must be greater than or equal to 500", InfoOKOnly, AppTitle)

            TextBoxAmount.Focus()

            Return

        End If


        TextBoxAmount.Text = Format(DblAmount, "#,##0.00")


    End Sub


    Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click


        If (MsgBox("Do you want to continue?", vbQuestion + vbYesNo, AppTitle) = MsgBoxResult.Yes) Then

            If TextBoxAccountNo.Text = "" Or String.IsNullOrEmpty(TextBoxAccountNo.Text) Or

               TextBoxAccountName.Text = "" Or String.IsNullOrEmpty(TextBoxAccountName.Text) Or

               TextBoxPhoneNo.Text = "" Or String.IsNullOrEmpty(TextBoxPhoneNo.Text) Or

               TextBoxAmount.Text = "" Or String.IsNullOrEmpty(TextBoxAmount.Text) Then

                MsgBox("Please input all the required fields!", InfoOKOnly, AppTitle)

                Return

            End If


            Dim cf As MyClassFiles = New MyClassFiles()


            Dim accNo As String = TextBoxAccountNo.Text

            Dim accName As String = TextBoxAccountName.Text

            Dim PhoneNo As String = TextBoxPhoneNo.Text

            Dim Cash As Double = CDbl(TextBoxAmount.Text)

            Dim Stat As String = "Active"


            Dim ImgP As String

            Dim ImgN As String


            If ImgPath = "" Then

                ImgP = "-"

            Else

                ImgP = ImgPath

            End If


            If ImgName = "" Then

                ImgN = "-"

            Else

                ImgN = ImgName

            End If



            Dim o As MyAccountClass = New MyAccountClass(accNo, accName, PhoneNo, Cash, ImgP, ImgN, Stat)


            FileOpen(1, cf.GetMasterFile, OpenMode.Append)


            '-- C stand for Create New Account

            WriteLine(1, o.AccountNo, o.AccountName, o.PhoneNo, o.Amount, o.ImagePath, o.ImageName, o.Status)


            FileOpen(2, cf.GetTransactionFile, OpenMode.Append)


            WriteLine(2, o.AccountNo, CreatedDate, CreatedTime,

                      "C", o.Amount)


            '-- Close before reopening in another mode.

            FileClose(1)

            FileClose(2)


            MsgBox("The files have been saved!", InfoOKOnly, AppTitle)


            ClearTextBoxes()


        End If

    End Sub


    Private Sub ButtonChoosePicture_Click(sender As Object, e As EventArgs) Handles ButtonChoosePicture.Click


        If TextBoxAccountNo.Text = "" Or String.IsNullOrEmpty(TextBoxAccountNo.Text) Then Return


        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then

            PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)

            ImgPath = IO.Path.GetDirectoryName(OpenFileDialog1.FileName)

            ImgName = IO.Path.GetFileName(OpenFileDialog1.FileName)

            TextBoxImageFile.Text = ImgName.ToString

        End If


    End Sub


    Private Sub ButtonClearPicture_Click(sender As Object, e As EventArgs) Handles ButtonClearPicture.Click

        PictureBox1.Image = Nothing

        TextBoxImageFile.Text = ""

        ImgPath = ""

        ImgName = ""

    End Sub


    Private Sub ButtonSearch_Click(sender As Object, e As EventArgs) Handles ButtonSearch.Click

        Dim f As Form = FormSearch

        f.ShowDialog()

    End Sub


End Class


Form: FormSearch.vb


Public Class FormSearch


    Private Found As Boolean = False

    Private HasImage As Boolean = False


    Private ImgPath As String = ""

    Private ImgName As String = ""


    Private PictureBox_Path As String

    Private PictureBox_Name As String


    Private Sub FormSearch_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim cf As MyClassFiles = New MyClassFiles

        FileOpen(1, cf.GetTempFile, OpenMode.Output)

        FileClose(1)

    End Sub


    Private Sub TextBoxAccountNo_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles TextBoxAccountNo.Validating

        TextBoxValidating(TextBoxAccountNo)


        If QCFailed Then

            QCFailed = False

            Return

        End If


        Found = False


        Dim cf As MyClassFiles = New MyClassFiles

        Dim ac As MyAccountClass = New MyAccountClass


        '-- Open file for input.

        FileOpen(1, cf.GetMasterFile, OpenMode.Input)


        Do While Not EOF(1)

            Input(1, ac.AccountNo)

            Input(1, ac.AccountName)

            Input(1, ac.PhoneNo)

            Input(1, ac.Amount)

            Input(1, ac.ImagePath)

            Input(1, ac.ImageName)

            Input(1, ac.Status)


            '-- Get original image info.

            PictureBox_Path = ac.ImagePath

            PictureBox_Path = ac.ImageName


            '-- Clear PictureBox tag.

            PictureBox1.Tag = ""


            If ac.ImageName <> "-" Then

                HasImage = True

            Else

                HasImage = False

            End If


            If ac.AccountNo = TextBoxAccountNo.Text Then

                Found = True

                Exit Do

            End If

        Loop


        FileClose(1)


        If Not (Found) Then

            MsgBox("Account Number not found.", InfoOKOnly, AppTitle)

            TextBoxAccountName.Text = ""

            TextBoxPhoneNo.Text = ""

            TextBoxBalance.Text = ""

            TextBoxImageFile.Text = ""

            PictureBox1.Image = Nothing

            TextBoxStatus.Text = ""

            Return

        End If


        '- If found


        Dim bl As Double = CDbl(ac.Amount)

        TextBoxAccountName.Text = ac.AccountName

        TextBoxPhoneNo.Text = ac.PhoneNo

        TextBoxBalance.Text = Format(bl, "#,##0.00")

        TextBoxImageFile.Text = ac.ImageName

        TextBoxStatus.Text = ac.Status


        If HasImage Then

            PictureBox1.Image = Image.FromFile(ac.ImagePath + "\" + ac.ImageName)

        Else

            '-- copy your picture to project folder!

            '

            PictureBox1.Image = Image.FromFile(Application.StartupPath + "\No-Picture.png")

        End If


    End Sub


    Private Sub ButtonCancel_Click(sender As Object, e As EventArgs) Handles ButtonCancel.Click

        Me.Close()

    End Sub


    Private Sub FormSearch_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing

        Dim message As String = "Do you want to close the form?"


        Dim result As DialogResult

        result = MessageBox.Show(Me, message, caption, buttons, icons)


        If result = DialogResult.Yes Then

            Me.Dispose()

            e.Cancel = False

        Else

            e.Cancel = True

            Return

        End If

    End Sub


End Class


Comments