Blog untuk berbagi ilmu komputer

Monday, May 9, 2016

Mengaplikasikan RadioButton dan CheckBox pada VB .Net

Contoh aplikasi untuk menghitung biaya pencucian mobil dengan memanfaatkan object RadioButton dan CheckBox di Visual Basic .Net 2010. Desain Form seperti berikut :



Sesuaikan nama setiap object pada Form yang sudah di desain dengan nama object yang sesuai dengan yang terlihat pada gambar berikut : 
Buka Text Editor Program dan ketikkan kode program untuk mendeklarasikan variabel yang diperlukan seperti berikut :



Public Class Form1
    Dim total, bayar, kembali As Double
    Dim bycuci, tottambah, totmakan As Double
    Dim makan As CheckBox()
    Dim tambah As CheckBox()
    Dim txttambah As TextBox()
    Dim txtmakan As TextBox()
    Dim txtjumlah As TextBox()
    Dim hargamakan() As Double = {17000, 21000, 16000, 10000, 5000}
    Dim hargatambah() As Double = {10000, 15000, 25000}

Kembali ke Design View dan double click pada Form1, masukkan kode program berikut ini :



Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim i As Integer
        makan = New CheckBox() {makan1, makan2, makan3, makan4, makan5}
        txtmakan = New TextBox() {txtmakan1, txtmakan2, txtmakan3, txtmakan4, txtmakan5}
        txtjumlah = New TextBox() {txtjumlah1, txtjumlah2, txtjumlah3, txtjumlah4, txtjumlah5}
        tambah = New CheckBox() {tambah1, tambah2, tambah3}
        txttambah = New TextBox() {txttambah1, txttambah2, txttambah3}

        For i = 0 To 4
            txtmakan(i).Text = "Rp. " & Format(hargamakan(i), "#,#.##")
        Next

        For i = 0 To 2
            txttambah(i).Text = "Rp. " & Format(hargatambah(i), "#,#.##")
        Next
        bycuci = 0
    End Sub


Double Click pada Jenis1, Jenis2, Jenis3 dan Jenis4, masukkan kode program pada masing-masing object tersebut seperti berikut :



    Private Sub jenis1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles jenis1.CheckedChanged
        bycuci = 15000
        TxtByCuci.Text = "Rp. " & Format(bycuci, "#,#.##")
    End Sub

    Private Sub jenis2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles jenis2.CheckedChanged
        bycuci = 25000
        TxtByCuci.Text = "Rp. " & Format(bycuci, "#,#.##")
    End Sub

    Private Sub jenis3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles jenis3.CheckedChanged
        bycuci = 30000
        TxtByCuci.Text = "Rp. " & Format(bycuci, "#,#.##")
    End Sub

    Private Sub jenis4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles jenis4.CheckedChanged
        bycuci = 50000
        TxtByCuci.Text = "Rp. " & Format(bycuci, "#,#.##")
    End Sub

Double Click pada Btnhitung, ketikkan kode program berikut :

    Private Sub btnhitung_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnhitung.Click
        Dim i As Integer
        totmakan = 0
        tottambah = 0
        For i = 0 To 2
            If tambah(i).Checked Then
                tottambah = tottambah + hargatambah(i)
            End If
        Next
        For i = 0 To 4
            If makan(i).Checked Then
                totmakan = totmakan + (hargamakan(i) * Val(txtjumlah(i).Text))
            End If
        Next
        total = bycuci + tottambah + totmakan
        txttotal.Text = "Rp. " & Format(total, "#,#.##")
    End Sub

Double Click pada txtbayar dan ganti event menjadi KeyPress, masukkan kode program berikut :

    Private Sub txtbayar_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtbayar.KeyPress
        If e.KeyChar = Chr(13) Then
            kembali = Val(txtbayar.Text) - total
            txtkembali.Text = "Rp. " & Format(total, "#,#.##")
        End If
    End Sub

Double  Click pada btnreset dan ketikkan kode program berikut :

    Private Sub btnreset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnreset.Click
        Dim i As Integer
        For i = 0 To 4
            makan(i).Checked = False
        Next
        For i = 0 To 4
            txtjumlah(i).Text = 0
        Next
        For i = 0 To 2
            tambah(i).Checked = False
        Next
        txttotal.Clear()
        txtbayar.Clear()
        txtkembali.Clear()
    End Sub
End Class
  
Share:

0 comments:

Post a Comment

Blogger templates