Imports System.Data
Imports System.Data.OleDb
Imports System.DBNull
Public Class Form1
Inherits System.Windows.Forms.Form
'Pendeklarasian objek
Dim objConnection As OleDbConnection = New Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source= D:\Dea Prantika\Database\Database dan Data Binding\buku.mdb")
Dim objDataAdapter As OleDbDataAdapter
Dim objDataSet As DataSet
Dim objDataView As DataView
Dim WithEvents objCurrencyManager As CurrencyManager
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'KatalogDataSet.buku' table. You can move, or remove it, as needed.
'Me.BukuTableAdapter.Fill(Me.KatalogDataSet.buku)FillDataSetAndView()
FillDataSetAndView()
End Sub
Private Sub FillDataSetAndView()
objDataView = Nothing 'membersihkan objek sebelum digunakan
objDataSet = New DataSet() 'inisialisasi instance baru dari bjek data set
objConnection.Open() 'membuka koneksi dataset
objDataAdapter = New OleDbDataAdapter("select * from buku ORDER BY kd_buku", objConnection)
objDataAdapter.Fill(objDataSet, ("buku"))
objDataView = New DataView(objDataSet.Tables("buku"))
objCurrencyManager = CType(Me.BindingContext(objDataView), CurrencyManager)
objConnection.Close()
DataGridView1.DataSource = objDataView
DataGridView1.Refresh()
tampil()
objDataSet = Nothing
End Sub
Private Sub ShowPosition()
txt_posisi.Text = objCurrencyManager.Position + 1 & "dari" & objCurrencyManager.Count()
End Sub
Private Sub btn_First_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_first.Click
objCurrencyManager.Position = 0
ShowPosition()
End Sub
Private Sub btn_previous_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_previous.Click
objCurrencyManager.Position -= 1
ShowPosition()
End Sub
Private Sub btn_next_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_next.Click
objCurrencyManager.Position += 1
ShowPosition()
End Sub
Private Sub btn_last_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_last.Click
objCurrencyManager.Position = objCurrencyManager.Count()
ShowPosition()
End Sub
Private Sub btn_save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_add.Click
Dim intPosition As Integer, intMaxID As Integer
Dim strId As String
Dim objCommand As OleDbCommand = New OleDbCommand()
intPosition = objCurrencyManager.Position
objDataSet = New DataSet()
objConnection.Open()
objDataAdapter = New OleDbDataAdapter("SELECT MAX(kd_buku) as MaxID from buku;", objConnection)
objDataAdapter.Fill(objDataSet, "buku")
'jika maxid kosong
If objDataSet.Tables("buku").Rows(0).Item("MaxID") Is System.DBNull.Value Then
intMaxID = 1000
Else
'strId = CStr(objDataSet.Tables("buku")).Rows(0).Item("MaxID")
intMaxID += 1
End If
strId = "10" & intMaxID
objCommand.Connection = objConnection
objCommand.CommandText = "INSERT INTO buku (kd_buku, nm_buku, nm_pengarang, harga) values(?, ?, ?, ?,)"
objCommand.CommandType = CommandType.Text
'parameter untuk kolom kd_buku
objCommand.Parameters.Add(New OleDbParameter())
objCommand.Parameters.Item(0).Direction = ParameterDirection.Input
objCommand.Parameters.Item(0).DbType = DbType.String
objCommand.Parameters.Item(0).Size = 4
objCommand.Parameters.Item(0).Value = Txt_kd_buku.Text
'parameter untuk kolom nm_buku
objCommand.Parameters.Add(New OleDbParameter())
objCommand.Parameters.Item(1).Direction = ParameterDirection.Input
objCommand.Parameters.Item(1).DbType = DbType.String
objCommand.Parameters.Item(1).Size = 30
objCommand.Parameters.Item(1).Value = Txt_nm_buku.Text
'parameter untuk pengarang
objCommand.Parameters.Add(New OleDbParameter())
objCommand.Parameters.Item(2).Direction = ParameterDirection.Input
objCommand.Parameters.Item(2).DbType = DbType.String
objCommand.Parameters.Item(2).Size = 20
objCommand.Parameters.Item(2).Value = Txt_pengarang.Text
'parameter untuk harga
objCommand.Parameters.Add(New OleDbParameter())
objCommand.Parameters.Item(3).Direction = ParameterDirection.Input
objCommand.Parameters.Item(3).DbType = DbType.Int64
objCommand.Parameters.Item(3).Size = 8
objCommand.Parameters.Item(3).Value = Txt_hrg.Text
'eksekusi objek oledbcommand untuk memasukan data baru
Try
objCommand.ExecuteNonQuery()
Catch err As OleDbException
MessageBox.Show(err.Message)
End Try
objConnection.Close()
'mengisi dataset dan binding kolom
FillDataSetAndView()
tampil()
'BindFields()
objCurrencyManager.Position = intPosition
ShowPosition()
'menampilkan pesan data telah ditambahkan
ToolStripStatusLabel1.Text = "Data telah ditambahkan"
'bersihkan
objDataSet = Nothing
objDataAdapter = Nothing
objCommand = Nothing
End Sub
Private Sub btn_add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_add.Click
Txt_kd_buku.Text = ""
Txt_nm_buku.Text = ""
Txt_pengarang.Text = ""
Txt_hrg.Text = ""
Txt_cari.Text = ""
End Sub
Private Sub btn_update_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_update.Click
Dim intPosition As Integer
Dim objCommand As OleDbCommand = New OleDbCommand()
'menyimpan posisi data saat ini
intPosition = objCurrencyManager.Position
'membuka koneksi database
objConnection.Open()
objCommand.Connection = objConnection
objCommand.CommandText = "UPDATE buku Set nm_buku = ?, nm_pengarang = ?, harga = ?, WHERE kd_buku = ?"
objCommand.CommandType = CommandType.Text
'parameter untuk kolom nm_buku
objCommand.Parameters.Add(New OleDbParameter())
objCommand.Parameters.Item(0).Direction = ParameterDirection.Input
objCommand.Parameters.Item(0).DbType = DbType.String
objCommand.Parameters.Item(0).Size = 30
objCommand.Parameters.Item(0).Value = Txt_nm_buku.Text
'parameter untuk kolom pngarang
objCommand.Parameters.Add(New OleDbParameter())
objCommand.Parameters.Item(1).Direction = ParameterDirection.Input
objCommand.Parameters.Item(1).DbType = DbType.String
objCommand.Parameters.Item(1).Size = 20
objCommand.Parameters.Item(1).Value = Txt_pengarang.Text
'parameter untuk kolom harga
objCommand.Parameters.Add(New OleDbParameter())
objCommand.Parameters.Item(2).Direction = ParameterDirection.Input
objCommand.Parameters.Item(2).DbType = DbType.Currency
objCommand.Parameters.Item(2).Size = 8
objCommand.Parameters.Item(2).Value = Txt_hrg.Text
'parameter untuk kolom kd_buku
objCommand.Parameters.Add(New OleDbParameter())
objCommand.Parameters.Item(3).Direction = ParameterDirection.Input
objCommand.Parameters.Item(3).DbType = DbType.String
objCommand.Parameters.Item(3).Size = 4
objCommand.Parameters.Item(3).Value = Txt_kd_buku.Text
objCommand.ExecuteNonQuery()
objConnection.Close()
FillDataSetAndView()
tampil()
'BindFields()
objCurrencyManager.Position = intPosition
ShowPosition()
'menampilkan pesan data telah diganti
ToolStripStatusLabel1.Text = "Data telah dganti"
'bersihkan
objCommand = Nothing
End Sub
Private Sub btn_sort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_sort.Click
Select Case cmb_field.SelectedIndex
Case 0 'kd_buku
objDataView.Sort = "kd_buku"
Case 1 'nm_buku
objDataView.Sort = "nm_buku"
Case 2 'nm_pengarang
objDataView.Sort = "nm_pengarang"
Case 3 'harga
objDataView.Sort = "harga"
End Select
btn_First_Click(Nothing, Nothing)
End Sub
Private Sub btn_cari_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Cari.Click
Dim intPosition As Integer
Select Case cmb_field.SelectedIndex
Case 0 'kd_buku
objDataView.Sort = "kd_buku"
Case 1 'nm_buku
objDataView.Sort = "nm_buku"
Case 2 'nm_pengarang
objDataView.Sort = "nm_pengarang"
Case 3 'harga
objDataView.Sort = "harga"
End Select
If cmb_field.SelectedIndex < 3 Then
intPosition = objDataView.Find(Txt_cari.Text)
Else
intPosition = objDataView.Find(CDec(Txt_cari.Text))
End If
If intPosition = -1 Then
ToolStripStatusLabel1.Text = "data tidak ditemukan"
Else
ToolStripStatusLabel1.Text = "data ditemukan"
objCurrencyManager.Position = intPosition
End If
ShowPosition()
End Sub
Private Sub btn_delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Delete.Click
Dim intPosition As Integer
Dim objcommand As OleDbCommand = New OleDbCommand()
intPosition = Me.BindingContext(objDataView).Position - 1
'jika posisi <0 set ke 0
If intPosition < 0 Then
intPosition = 0
End If
objConnection.Open()
objcommand.Connection = objConnection
objcommand.CommandText = "DELETE FROM buku WHERE kd_buku = ?;"
objcommand.CommandType = CommandType.Text
'menambahkan parameter
'parameter untuk kolom kd_buku
objcommand.Parameters.Add(New OleDbParameter())
objcommand.Parameters.Item(0).Direction = ParameterDirection.Input
objcommand.Parameters.Item(0).DbType = DbType.String
objcommand.Parameters.Item(0).Size = 4
objcommand.Parameters.Item(0).Value = Txt_kd_buku.Text
'eksekusi untuk update data
objcommand.ExecuteNonQuery()
objConnection.Close()
FillDataSetAndView()
tampil()
Me.BindingContext(objDataView).Position = intPosition
ShowPosition()
objcommand = Nothing
End Sub
Private Sub tampil()
Me.Txt_kd_buku.Text = objDataView.Item(objCurrencyManager.Position).Item("kd_buku")
Me.Txt_pengarang.Text = objDataView.Item(objCurrencyManager.Position).Item("nm_pengarang")
Me.Txt_nm_buku.Text = objDataView.Item(objCurrencyManager.Position).Item("nm_buku")
Me.Txt_hrg.Text = objDataView.Item(objCurrencyManager.Position).Item("harga")
End Sub
Private Sub objCurrencyManager_PositionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles objCurrencyManager.PositionChanged
ShowPosition()
tampil()
End Sub
Private Sub btn_exit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_exit.Click
End
End Sub
End Class
Selamat datang di blog saya.Semoga bermanfaat bagi yang membacanya.Dilarang copy paste.Jika ada yang copy paste akan di tindak lanjuti oleh akun google domain Indonesia.Terima kasih telah membaca blog saya.
Kamis, 04 Juli 2013
Rabu, 03 Juli 2013
Pembuatan Program Kode Database
Hallo selamat siang semua.. ketemu saya lagi nih disini hehehe kali ini saya akan membahas mengenai database dan data bindings dalam visual basic (dot) net.Ini adalah materi VB(dot)Net latihan saya di kampus.Sebenarnya cukup sulit untuk menyatukannya antara form data set, data adapter, data source, dan data connection.Saya rasa semua sudah bisa dasarnya dalam pembuatan form-form, makan saya tidak akan menjelaskan lagi.Okeyy langsung saja mari kita simak codingan berikut ini dengan teliti dan cermat..
Public Class Form1
Dim WithEvents objcurr As CurrencyManager
Dim objDateView As DataView
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'BukuDataSet.Buku' table. You can move, or remove it, as needed.
Me.BukuTableAdapter.Fill(Me.BukuDataSet.Buku)
'Me.OleDbDataAdapter1.Fill(Me.BukuDataSet.Buku)
objDateView = New DataView(BukuDataSet.Tables("buku"))
objcurr = CType(Me.BindingContext(objDateView), CurrencyManager)
DataGridView1.DataSource = objDateView
DataGridView1.Refresh()
End Sub
Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnsave.Click
BukuDataSet.Buku.AddBukuRow(txt_kd_buku.Text, txtnmbk.Text, txtpngrng.Text, txthrg.Text)
OleDbDataAdapter1.Update(BukuDataSet.Buku)
objDateView = New DataView(BukuDataSet.Tables("buku"))
objcurr = CType(Me.BindingContext(objDateView), CurrencyManager)
DataGridView1.DataSource = objDateView
DataGridView1.Refresh()
Btnupdate.Enabled = True
Btnadd.Enabled = True
Btnsave.Enabled = False
End Sub
Private Sub btnexit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_exit.Click
End
End Sub
Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnadd.Click
bersihkan()
txt_kd_buku.Text = ""
txtnmbk.Text = ""
txtpngrng.Text = ""
txthrg.Text = ""
Btnsave.Enabled = True
Btnadd.Enabled = False
Btnupdate.Enabled = False
End Sub
Private Sub btnupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnupdate.Click
Dim pos As Integer
bersihkan()
BukuDataSet.Buku(Me.BindingContext(BukuDataSet, "buku").Position).EndEdit()
'Me.BukuDataSet.Buku.FindBykd_buku(Me.txt_kd_buku.Text).nm_buku = Me.txtnmbk.Text
'Me.BukuDataSet.Buku.FindBykd_buku(Me.txt_kd_buku.Text).nm_pengarang = Me.txtpngrng.Text
'Me.BukuDataSet.Buku.FindBykd_buku(Me.txt_kd_buku.Text).harga = Me.txthrg.Text
OleDbDataAdapter1.Update(BukuDataSet.Buku)
objcurr.Position = pos
OleDbConnection1 = Nothing
Btnadd.Enabled = True
End Sub
Private Sub btndelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
'Me.BukuDataSet.Buku.FindBykd_buku(Me.txt_kd_buku.Text).Delete()
OleDbDataAdapter1.Update(BukuDataSet.Buku)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
objcurr.Position += 1
End Sub
Private Sub tampil()
Me.txt_kd_buku.Text = New DataView(BukuDataSet.Tables("buku")).Item(objcurr.Position).Item("kd_buku")
Me.Txtpngrng.Text = New DataView(BukuDataSet.Tables("buku")).Item(objcurr.Position).Item("nm_pengarang")
Me.Txtnmbk.Text = New DataView(BukuDataSet.Tables("buku")).Item(objcurr.Position).Item("nm_buku")
Me.Txthrg.Text = New DataView(BukuDataSet.Tables("buku")).Item(objcurr.Position).Item("harga")
End Sub
Private Sub objCurrencyManager_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles objcurr.PositionChanged
tampil()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
objcurr.Position -= 0
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
objcurr.Position -= 1
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
objcurr.Position = objcurr.Count()
End Sub
Private Sub bersihkan()
txt_kd_buku.DataBindings.Clear()
txtnmbk.DataBindings.Clear()
txtpngrng.DataBindings.Clear()
txthrg.DataBindings.Clear()
End Sub
End Class
Bisa dilihat diatas, itu contoh membuat database pada toko buku.Saya merancangnya dengan sedikit panduan dari modul dosen kampus saya.Untuk lebih jelasnya bisa kalian coba dirumah atau di kampus dengan VB sendiri.Sekiranya cukup jelas sampai disini, kurang lebihnya mohon maaf.Selamat membaca, semoga dapat menambah ilmu anda.Salam mahasiswa!! :)
Follow me: @deaprantika ;)
Selasa, 02 Juli 2013
Membuat Kalkulator Riil VB(dot)Net
Disini saya ingin berbagi ilmu mengenai kalkulator riil atau kalkulator yang sangat efektif dalam Virtual Basic atau disebut VB.Net.Tetapi disini saya belum menambahkan cos sin atau tan.Hanya cuma plus minum bagi dan kali saja.Mungkin di lain waktu akan saya share lagi kalkulator yang lebih lengkap dan banyak sehingga namanya bukan kalkulator riil lagi tetapi kalkulator sangat riil.Kalian pasti sudah tau cara membuat form-form nya.Saya hanya sekedar memberi ilmu dalam nya saja atau codingannya.
Okeyyy langsung saja codingan yang pertama untuk tombol 1 sampai 0.
Public Class Form1 (Untuk membuat Form Button 1 sampai 0)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TextBox1.Text = 0 Then
TextBox1.Text = 1
Else
TextBox1.Text = TextBox1.Text & 1
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If TextBox1.Text = 0 Then
TextBox1.Text = 2
Else
TextBox1.Text = TextBox1.Text & 2
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If TextBox1.Text = 0 Then
TextBox1.Text = 3
Else
TextBox1.Text = TextBox1.Text & 3
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
If TextBox1.Text = 0 Then
TextBox1.Text = 4
Else
TextBox1.Text = TextBox1.Text & 4
End If
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
If TextBox1.Text = 0 Then
TextBox1.Text = 5
Else
TextBox1.Text = TextBox1.Text & 5
End If
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
If TextBox1.Text = 0 Then
TextBox1.Text = 6
Else
TextBox1.Text = TextBox1.Text & 6
End If
End Sub
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
If TextBox1.Text = 0 Then
TextBox1.Text = 7
Else
TextBox1.Text = TextBox1.Text & 7
End If
End Sub
Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
If TextBox1.Text = 0 Then
TextBox1.Text = 8
Else
TextBox1.Text = TextBox1.Text & 8
End If
End Sub
Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
If TextBox1.Text = 0 Then
TextBox1.Text = 9
Else
TextBox1.Text = TextBox1.Text & 9
End If
End Sub
Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
If TextBox1.Text = 0 Then
TextBox1.Text = 0
Else
TextBox1.Text = TextBox1.Text & 0
End If
End Sub
End Class
Ini untuk menentukan operasi tambah, kurang, kali, dan bagi.Perhatikan simbolnya saja "..."
Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click
Angka2 = TextBox1.Text
If simbol = "+" Then
hasil = Angka1 + Angka2
ElseIf simbol = "-" Then
hasil = Angka1 - Angka2
ElseIf simbol = "*" Then
hasil = Angka1 * Angka2
ElseIf simbol = "/" Then
hasil = Angka1 / Angka2
End If
TextBox1.Text = hasil
End Sub
Lalu selanjutnya ini untuk codingan button tombol sama dengan ( = )
TextBox1.Text = 0
Cukup sekian sampai disini, kurang lebihnya mohon maaf dan semoga berguna untuk belajar yah.Salam mahasiswa!! :)
Dont forget guys.. ;)
Follow me on twitter: @deaprantika @nogatasbrig
Okeyyy langsung saja codingan yang pertama untuk tombol 1 sampai 0.
Public Class Form1 (Untuk membuat Form Button 1 sampai 0)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TextBox1.Text = 0 Then
TextBox1.Text = 1
Else
TextBox1.Text = TextBox1.Text & 1
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If TextBox1.Text = 0 Then
TextBox1.Text = 2
Else
TextBox1.Text = TextBox1.Text & 2
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If TextBox1.Text = 0 Then
TextBox1.Text = 3
Else
TextBox1.Text = TextBox1.Text & 3
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
If TextBox1.Text = 0 Then
TextBox1.Text = 4
Else
TextBox1.Text = TextBox1.Text & 4
End If
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
If TextBox1.Text = 0 Then
TextBox1.Text = 5
Else
TextBox1.Text = TextBox1.Text & 5
End If
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
If TextBox1.Text = 0 Then
TextBox1.Text = 6
Else
TextBox1.Text = TextBox1.Text & 6
End If
End Sub
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
If TextBox1.Text = 0 Then
TextBox1.Text = 7
Else
TextBox1.Text = TextBox1.Text & 7
End If
End Sub
Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
If TextBox1.Text = 0 Then
TextBox1.Text = 8
Else
TextBox1.Text = TextBox1.Text & 8
End If
End Sub
Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
If TextBox1.Text = 0 Then
TextBox1.Text = 9
Else
TextBox1.Text = TextBox1.Text & 9
End If
End Sub
Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
If TextBox1.Text = 0 Then
TextBox1.Text = 0
Else
TextBox1.Text = TextBox1.Text & 0
End If
End Sub
End Class
Ini untuk menentukan operasi tambah, kurang, kali, dan bagi.Perhatikan simbolnya saja "..."
Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click
Angka2 = TextBox1.Text
If simbol = "+" Then
hasil = Angka1 + Angka2
ElseIf simbol = "-" Then
hasil = Angka1 - Angka2
ElseIf simbol = "*" Then
hasil = Angka1 * Angka2
ElseIf simbol = "/" Then
hasil = Angka1 / Angka2
End If
TextBox1.Text = hasil
End Sub
Lalu selanjutnya ini untuk codingan button tombol sama dengan ( = )
TextBox1.Text = 0
Cukup sekian sampai disini, kurang lebihnya mohon maaf dan semoga berguna untuk belajar yah.Salam mahasiswa!! :)
Dont forget guys.. ;)
Follow me on twitter: @deaprantika @nogatasbrig
Cara Membuat Kalkulator Visual Basic
Hanya sekedar share ilmu aja tentang VB(dot)net.Disini saya akan memberikan rumus dan codingan untuk membuat kalkulator dalam VB.Net.Ini adalah sebagian materi kuliah saya juga workshop maupun kursus.Cukup simple dan tidak bertele-tel, okeyyy saya akan langsung jabarkan dibawah ini.Mari disimak..
Ini dia contoh codingan dasar membuat kalkulator.
Imports WindowsApplication1.Coba.Kalkulator
Public Class Form1
Dim c As New Coba.Kalkulator
Private Sub btnTambah_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTambah.Click
Me.LblOperator.Text = Me.btnTambah.Text
Me.TxtHasil.Text = c.Add(Val(Me.TxtAngka1.Text), Val(Me.TxtAngka2.Text))
End Sub
Private Sub BtnKali_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnKali.Click
Me.LblOperator.Text = Me.btnKali.Text
Me.TxtHasil.Text = c.Mult(Val(Me.TxtAngka1.Text), Val(Me.TxtAngka2.Text))
End Sub
Private Sub BtnBagi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBagi.Click
Me.LblOperator.Text = Me.btnBagi.Text
Me.TxtHasil.Text = c.Divide(Val(Me.TxtAngka1.Text), Val(Me.TxtAngka2.Text))
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.TxtAngka1.Text = ""
Me.TxtAngka2.Text = ""
Me.TxtHasil.Text = ""
Me.LblOperator.Text = ""
Me.TxtAngka1.Focus()
End Sub
Private Sub BtnKurang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnKurang.Click
Me.LblOperator.Text = Me.btnKurang.Text
Me.TxtHasil.Text = c.Less(Val(Me.TxtAngka1.Text), Val(Me.TxtAngka2.Text))
End Sub
End Class
Setelah membuat codingan dasarnya, disini kita akan menentukan codingan untuk rumus dibagi, ditambah, dikali, dan dikurang.Disimak baik-baik yah kawan :)
Namespace Coba
Public Class Kalkulator
Public Function Add(ByVal a As Integer, ByVal b As Integer) As Integer
Add = a + b
End Function
Public Function Mult(ByVal a As Integer, ByVal b As Integer) As Integer
Mult = a * b
End Function
Public Function Divide(ByVal a As Byte, ByVal b As Byte) As Decimal
Divide = a / b
End Function
Public Function Less(ByVal a As Integer, ByVal b As Integer) As Integer
Less = a - b
End Function
End Class
End Namespace
Selamat mencoba untuk belajar menggunakan kalkulator.Semoga bermanfaat untuk kalian yang ingin belajar nya secara pelan-pelan dan mendasar.
Ini dia contoh codingan dasar membuat kalkulator.
Imports WindowsApplication1.Coba.Kalkulator
Public Class Form1
Dim c As New Coba.Kalkulator
Private Sub btnTambah_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTambah.Click
Me.LblOperator.Text = Me.btnTambah.Text
Me.TxtHasil.Text = c.Add(Val(Me.TxtAngka1.Text), Val(Me.TxtAngka2.Text))
End Sub
Private Sub BtnKali_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnKali.Click
Me.LblOperator.Text = Me.btnKali.Text
Me.TxtHasil.Text = c.Mult(Val(Me.TxtAngka1.Text), Val(Me.TxtAngka2.Text))
End Sub
Private Sub BtnBagi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBagi.Click
Me.LblOperator.Text = Me.btnBagi.Text
Me.TxtHasil.Text = c.Divide(Val(Me.TxtAngka1.Text), Val(Me.TxtAngka2.Text))
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.TxtAngka1.Text = ""
Me.TxtAngka2.Text = ""
Me.TxtHasil.Text = ""
Me.LblOperator.Text = ""
Me.TxtAngka1.Focus()
End Sub
Private Sub BtnKurang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnKurang.Click
Me.LblOperator.Text = Me.btnKurang.Text
Me.TxtHasil.Text = c.Less(Val(Me.TxtAngka1.Text), Val(Me.TxtAngka2.Text))
End Sub
End Class
Setelah membuat codingan dasarnya, disini kita akan menentukan codingan untuk rumus dibagi, ditambah, dikali, dan dikurang.Disimak baik-baik yah kawan :)
Namespace Coba
Public Class Kalkulator
Public Function Add(ByVal a As Integer, ByVal b As Integer) As Integer
Add = a + b
End Function
Public Function Mult(ByVal a As Integer, ByVal b As Integer) As Integer
Mult = a * b
End Function
Public Function Divide(ByVal a As Byte, ByVal b As Byte) As Decimal
Divide = a / b
End Function
Public Function Less(ByVal a As Integer, ByVal b As Integer) As Integer
Less = a - b
End Function
End Class
End Namespace
Selamat mencoba untuk belajar menggunakan kalkulator.Semoga bermanfaat untuk kalian yang ingin belajar nya secara pelan-pelan dan mendasar.
Follow me on Twitter: @deaprantika @nogatasbrig
Senin, 01 Juli 2013
Contoh Program VB.Net
Codingan 1
Create directory
Imports System.IO
Module Module1
Sub Main()
Console.WriteLine("Creating directories...")
Directory.CreateDirectory("D:\ test01")
Directory.CreateDirectory("D:\ Temp\Sample02")
Directory.CreateDirectory("Sam ple03")
Console.WriteLine("Directories created")
End Sub
End Module
Codingan 2
Imports System.IO
Module Module1
Sub Main()
Try
Directory.Delete("C:\Sample01" , True)
Catch E As Exception
Console.WriteLine("Error deleting directory C:\Sample01")
Console.WriteLine("Error (0)", E.Message)
End Try
End Sub
End Module
Codingan 3
Imports System.IO
Module Module1
Sub Main()
Try
If (Directory.Exists("D:\test01") ) Then
Console.WriteLine("D:\test01 already exists")
Else
Directory.CreateDirectory("C:\ test01")
End If
Console.WriteLine("Directories created")
Catch E As Exception
Console.WriteLine("Error creating directory")
Console.WriteLine("Error: (0)", E.Message)
End Try
End Sub
End Module
Codingan 4
Imports System.IO
Module Module1
Sub Main()
Dim Root As New DirectoryInfo("C:\")
Dim Files As FileInfo() = Root.GetFiles("*.*")
Dim Dirs As DirectoryInfo() = Root.GetDirectories("*.*")
Console.WriteLine("Root Files")
Dim Filename As FileInfo
For Each Filename In Files
Try
Console.Write(Filename.FullNam e)
Console.Write(" Size: (0) bytes", Filename.Length)
Console.WriteLine(" Last use: (0)", Filename.LastAccessTime)
Catch E As Exception
Console.WriteLine("Error accessing File")
End Try
Next
End Sub
End Module
Codingan 5
Contoh Unconditional Branching
Option Strict On
Imports System
Module Module1
Sub Main()
Console.WriteLine("Ini method utamanya,,") SomeMethod()
Console.WriteLine("Kembali ke method utama...")
Console.ReadLine()
End Sub
Sub SomeMethod()
Console.WriteLine("Sekarang tampil deh method percabangannya!!")
End Sub
End Module
Codingan 6
Contoh If Statement
Module Module1
Sub Main()
Dim nilai1 As Integer = 10
Dim nilai2 As Integer = 20
Dim maxValue As Integer
maxValue = Cint(IIf((nilai1 > nilai2), nilai1, nilai2))
Console.WriteLine("Nilai pertama: (0), Nilai kedua (1),
maxValue: (2)", nilai1, nilai2, maxValue)
Console.ReadLine()
End Sub
End Module
Codingan 7
Module Module1
Dim kal As String
Dim a() As Char
Dim x, i, k As Integer
Dim hrf(20) As Char
Dim bnyk(20) As Byte
Sub Main()
Dim m As Byte
Console.Write("masukan kalimat : ")
kal = Console.ReadLine
x = kal.Length()
ReDim a(x + 1)
For i = 0 To x
a(i) = Mid(kal, i + 1, 1)
Console.Write(a(i))
Next i
urut(a)
Console.WriteLine("data yang telah urut")
For i = 0 To x
Console.WriteLine(a(i))
Next
Console.WriteLine()
Console.WriteLine()
HitungKata(a)
For i = 0 To k - 1
If (hrf(i) = " ") Then
Console.WriteLine("spasi banyaknya = (0)", bnyk(i))
Else
Console.WriteLine("huruf (0) banyaknya = (1)", hrf(i), bnyk(i))
End If
Next
Console.ReadLine()
End Sub
Sub urut(ByRef a() As Char)
Dim i, j As Byte
Dim temp As Char
Console.WriteLine("banyak huruf : (0)", x)
For i = 0 To x
For j = i + 1 To x
If (a(i) > a(j)) Then
temp = a(j)
a(j) = a(i)
a(i) = temp
End If
Next
Next
End Sub
Sub HitungKata(ByVal a() As Char)
i = 1
Dim m As Byte
k = 0
m = 0
While (i < x + 1)
bnyk(k) = 1
hrf(k) = a(i)
While (a(i) = a(i + 1))
bnyk(k) += 1
i += 1
End While
k += 1
i += 1
End While
End Sub
End Module
Codingan 8
Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.Collections.Specialized
Public Class MainClass
Shared Sub Main(ByVal args As String())
Dim dict As New StudentListDictionary
dict.Add("111-11-1111", New Student("A", "A"))
dict.Add("333-22-3333", New Student("B", "B"))
dict.Add("444-44-4444", New Student("C", "C"))
dict.Add("555-55-5555", New Student("D", "D"))
Dim student As Student = dict.Item("111-11-1111")
Console.WriteLine(student.ToSt ring)
Console.ReadLine()
End Sub
End Class
Public Class Student
Private m_FirstName As String
Private m_LastName As String
Public Sub New(ByVal first_name As String, ByVal last_name As String)
m_FirstName = first_name
m_LastName = last_name
End Sub
Public Overrides Function ToString() As String
Return m_FirstName & " " & m_LastName
End Function
End Class
Public Class StudentListDictionary
Inherits ListDictionary
'Add a Dictionary entry.
Public Shadows Sub Add(ByVal new_key As String, ByVal new_student As Student)
MyBase.Add(new_key, new_student)
End Sub
'Return an object with the given key.
Default Public Shadows Property Item(ByVal key As String) As Student
Get
Return DirectCast(MyBase.Item(key), Student)
End Get
Set(ByVal Value As Student)
MyBase.Item(key) = Value
End Set
End Property
'Return True if the Dictionary contains this Student.
Public Shadows Function Contain(ByVal key As String) As Boolean
Return MyBase.Contains(key)
End Function
'Remove this entry.
Public Shadows Sub Remove(ByVal key As String)
MyBase.Remove(key)
End Sub
End Class
Create directory
Imports System.IO
Module Module1
Sub Main()
Console.WriteLine("Creating directories...")
Directory.CreateDirectory("D:\
Directory.CreateDirectory("D:\
Directory.CreateDirectory("Sam
Console.WriteLine("Directories
End Sub
End Module
Codingan 2
Imports System.IO
Module Module1
Sub Main()
Try
Directory.Delete("C:\Sample01"
Catch E As Exception
Console.WriteLine("Error deleting directory C:\Sample01")
Console.WriteLine("Error (0)", E.Message)
End Try
End Sub
End Module
Codingan 3
Imports System.IO
Module Module1
Sub Main()
Try
If (Directory.Exists("D:\test01")
Console.WriteLine("D:\test01 already exists")
Else
Directory.CreateDirectory("C:\
End If
Console.WriteLine("Directories
Catch E As Exception
Console.WriteLine("Error creating directory")
Console.WriteLine("Error: (0)", E.Message)
End Try
End Sub
End Module
Codingan 4
Imports System.IO
Module Module1
Sub Main()
Dim Root As New DirectoryInfo("C:\")
Dim Files As FileInfo() = Root.GetFiles("*.*")
Dim Dirs As DirectoryInfo() = Root.GetDirectories("*.*")
Console.WriteLine("Root Files")
Dim Filename As FileInfo
For Each Filename In Files
Try
Console.Write(Filename.FullNam
Console.Write(" Size: (0) bytes", Filename.Length)
Console.WriteLine(" Last use: (0)", Filename.LastAccessTime)
Catch E As Exception
Console.WriteLine("Error accessing File")
End Try
Next
End Sub
End Module
Codingan 5
Contoh Unconditional Branching
Option Strict On
Imports System
Module Module1
Sub Main()
Console.WriteLine("Ini method utamanya,,") SomeMethod()
Console.WriteLine("Kembali ke method utama...")
Console.ReadLine()
End Sub
Sub SomeMethod()
Console.WriteLine("Sekarang tampil deh method percabangannya!!")
End Sub
End Module
Codingan 6
Contoh If Statement
Module Module1
Sub Main()
Dim nilai1 As Integer = 10
Dim nilai2 As Integer = 20
Dim maxValue As Integer
maxValue = Cint(IIf((nilai1 > nilai2), nilai1, nilai2))
Console.WriteLine("Nilai pertama: (0), Nilai kedua (1),
maxValue: (2)", nilai1, nilai2, maxValue)
Console.ReadLine()
End Sub
End Module
Codingan 7
Module Module1
Dim kal As String
Dim a() As Char
Dim x, i, k As Integer
Dim hrf(20) As Char
Dim bnyk(20) As Byte
Sub Main()
Dim m As Byte
Console.Write("masukan kalimat : ")
kal = Console.ReadLine
x = kal.Length()
ReDim a(x + 1)
For i = 0 To x
a(i) = Mid(kal, i + 1, 1)
Console.Write(a(i))
Next i
urut(a)
Console.WriteLine("data yang telah urut")
For i = 0 To x
Console.WriteLine(a(i))
Next
Console.WriteLine()
Console.WriteLine()
HitungKata(a)
For i = 0 To k - 1
If (hrf(i) = " ") Then
Console.WriteLine("spasi banyaknya = (0)", bnyk(i))
Else
Console.WriteLine("huruf (0) banyaknya = (1)", hrf(i), bnyk(i))
End If
Next
Console.ReadLine()
End Sub
Sub urut(ByRef a() As Char)
Dim i, j As Byte
Dim temp As Char
Console.WriteLine("banyak huruf : (0)", x)
For i = 0 To x
For j = i + 1 To x
If (a(i) > a(j)) Then
temp = a(j)
a(j) = a(i)
a(i) = temp
End If
Next
Next
End Sub
Sub HitungKata(ByVal a() As Char)
i = 1
Dim m As Byte
k = 0
m = 0
While (i < x + 1)
bnyk(k) = 1
hrf(k) = a(i)
While (a(i) = a(i + 1))
bnyk(k) += 1
i += 1
End While
k += 1
i += 1
End While
End Sub
End Module
Codingan 8
Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.Collections.Specialized
Public Class MainClass
Shared Sub Main(ByVal args As String())
Dim dict As New StudentListDictionary
dict.Add("111-11-1111", New Student("A", "A"))
dict.Add("333-22-3333", New Student("B", "B"))
dict.Add("444-44-4444", New Student("C", "C"))
dict.Add("555-55-5555", New Student("D", "D"))
Dim student As Student = dict.Item("111-11-1111")
Console.WriteLine(student.ToSt
Console.ReadLine()
End Sub
End Class
Public Class Student
Private m_FirstName As String
Private m_LastName As String
Public Sub New(ByVal first_name As String, ByVal last_name As String)
m_FirstName = first_name
m_LastName = last_name
End Sub
Public Overrides Function ToString() As String
Return m_FirstName & " " & m_LastName
End Function
End Class
Public Class StudentListDictionary
Inherits ListDictionary
'Add a Dictionary entry.
Public Shadows Sub Add(ByVal new_key As String, ByVal new_student As Student)
MyBase.Add(new_key, new_student)
End Sub
'Return an object with the given key.
Default Public Shadows Property Item(ByVal key As String) As Student
Get
Return DirectCast(MyBase.Item(key), Student)
End Get
Set(ByVal Value As Student)
MyBase.Item(key) = Value
End Set
End Property
'Return True if the Dictionary contains this Student.
Public Shadows Function Contain(ByVal key As String) As Boolean
Return MyBase.Contains(key)
End Function
'Remove this entry.
Public Shadows Sub Remove(ByVal key As String)
MyBase.Remove(key)
End Sub
End Class
Langganan:
Postingan (Atom)