Full width home advertisement

Post Page Advertisement [Top]

http://adf.ly/11765845/lab-14-expensesnavigating-using-array

  • code in class Expense
Public Class Expense
    Public Property ID As String
    Public Property Description As String
    Public Property Amount As Double
    Public Sub New()

    End Sub
    Public Sub New(id As String, descr As String, amount As Double)
        Me.ID = id
        Me.Description = descr
        Me.Amount = amount
    End Sub
End Class
  • code in form1
Public Class Form1
    Private Const MAX As Integer = 100
    Private exps As Expense()
    Private nums As Integer = 0
    Private curIndex As Integer = -1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.exps = Array.CreateInstance(GetType(Expense), MAX)
        Me.ViewCurExepse()
    End Sub
    Private Sub ClearBoxes()
        txtID.Clear()
        txtDescr.Clear()
        txtAmount.Clear()
        txtPos.Clear()
    End Sub
    Private Sub ViewCurExepse()
        If curIndex < 0 Then
            ClearBoxes()
        Else
            Dim ex As Expense = exps(curIndex)
            If ex Is Nothing Then
                ClearBoxes()
            Else
                txtID.Text = ex.ID
                txtDescr.Text = ex.Description
                txtAmount.Text = ex.Amount
                txtPos.Text = String.Format("{0}/{1}", curIndex + 1, nums)
            End If
        End If
        btnCreate.Enabled = False
        btnUpdate.Enabled = False
    End Sub

    Private Sub DoTextChanged(sender As Object, e As EventArgs) Handles txtAmount.TextChanged, txtDescr.TextChanged, txtID.TextChanged
        btnCreate.Enabled = True
        btnUpdate.Enabled = True
    End Sub

    Private Sub DoClickCreate(sender As Object, e As EventArgs) Handles btnCreate.Click
        Dim tid As String = txtID.Text.Trim()
        Dim tdescr As String = txtDescr.Text.Trim()
        Dim tamount As Double = 0
        Double.TryParse(txtAmount.Text, tamount)
        Dim targetIndex As Integer = -1
        For targetIndex = 0 To nums
            If exps(targetIndex) Is Nothing Then Exit For
        Next
        exps(targetIndex) = New Expense(tid, tdescr, tamount)
        nums += 1
        If curIndex < 0 Then curIndex = targetIndex
        ViewCurExepse()
    End Sub

    Private Sub DoClickNavigate(sender As Object, e As EventArgs) Handles btnFirst.Click, btnPrev.Click, btnNext.Click, btnLast.Click
        If nums < 2 Then Return
        If sender Is btnFirst Then curIndex = 0
        If sender Is btnPrev And curIndex > 0 Then
            Do
                curIndex -= 1
            Loop While (curIndex > 0 And exps(curIndex) Is Nothing)
        End If
        If sender Is btnNext And curIndex < nums - 1 Then
            Do
                curIndex += 1
            Loop While (curIndex < nums - 1 And exps(curIndex) Is Nothing)
        End If
        If sender Is btnLast Then curIndex = nums - 1
        ViewCurExepse()
    End Sub

    Private Sub DoClickUpdate(sender As Object, e As EventArgs) Handles btnUpdate.Click
        Dim ex As Expense = exps(curIndex)
        If ex Is Nothing Then Return
        Dim tamount As Double = 0
        Double.TryParse(txtAmount.Text, tamount)
        ex.ID = txtID.Text.Trim()
        ex.Description = txtDescr.Text.Trim()
        ex.Amount = tamount
        ViewCurExepse()
    End Sub
End Class

No comments:

Post a Comment

Bottom Ad [Post Page]

| KhmerSharing