programming. Berikut ini adalah beberapa contoh yang bisa kami berikan
untuk Anda:
Contoh 1: Program Kasir Restoran
Tampilan form-form di dalam program:
Inilah listing program berdasarkan komponen yang digunakan:
- Combobox1:
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Select Case ComboBox1.Text
Case “Nasi Uduk”
Label2.Text = “Rp. 3500”
hrgmakanan = 3500
Case “Bakso”
Label2.Text = “Rp. 6000”
hrgmakanan = 6000
Case “Mie Ayam”
Label2.Text = “Rp. 7000”
hrgmakanan = 7000
End Select
End Sub
Select Case ComboBox1.Text
Case “Nasi Uduk”
Label2.Text = “Rp. 3500”
hrgmakanan = 3500
Case “Bakso”
Label2.Text = “Rp. 6000”
hrgmakanan = 6000
Case “Mie Ayam”
Label2.Text = “Rp. 7000”
hrgmakanan = 7000
End Select
End Sub
- variabel di Form1:
Public Class Form1
Public hrgmakanan, hrgminuman As Integer
Public hrgmakanan, hrgminuman As Integer
- Combobox2:
Private
Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
Select Case ComboBox2.Text
Case “Es Jeruk”
Label4.Text = “Rp. 3000”
hrgminuman = 3000
Case “Jus Alpukat”
Label4.Text = “Rp. 7000”
hrgminuman = 7000
Case “Teh Manis”
Label4.Text = “Rp. 4000”
hrgminuman = 4000
End Select
End Sub
Select Case ComboBox2.Text
Case “Es Jeruk”
Label4.Text = “Rp. 3000”
hrgminuman = 3000
Case “Jus Alpukat”
Label4.Text = “Rp. 7000”
hrgminuman = 7000
Case “Teh Manis”
Label4.Text = “Rp. 4000”
hrgminuman = 4000
End Select
End Sub
- Sub-routine Public:
Public Sub New()
‘ This call is required by the designer. InitializeComponent()
ComboBox2.Visible = False
Label3.Visible = False
‘ Add any initialization after the InitializeComponent() call.
End Sub
ComboBox2.Visible = False
Label3.Visible = False
‘ Add any initialization after the InitializeComponent() call.
End Sub
- Checkbox1:
Private Sub
CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Enabled = False Then
ComboBox2.Visible = False
End If
If CheckBox1.Enabled = True Then
ComboBox2.Visible = True
Label3.Visible = True
End If
End Sub
If CheckBox1.Enabled = False Then
ComboBox2.Visible = False
End If
If CheckBox1.Enabled = True Then
ComboBox2.Visible = True
Label3.Visible = True
End If
End Sub
- Button1:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim porsimkn, porsimnm, jumlah As Integer
porsimkn = TextBox1.Text
porsimnm = TextBox2.Text
Dim porsimkn, porsimnm, jumlah As Integer
porsimkn = TextBox1.Text
porsimnm = TextBox2.Text
jumlah = (hrgmakanan * porsimkn) + (hrgminuman * porsimnm)
Label7.Text = “Rp. ” & jumlah.ToString
End Sub
Label7.Text = “Rp. ” & jumlah.ToString
End Sub
Contoh 2: Program VB Sederhana
Tampilan form
Listing program pada komponen “commandbutton”:
If Text1.Text = “” Then
MsgBox (“Isi dulu Nama anda”)
Else
MsgBox (“Nama Saya” + Text1.Text + “Dan saya sedang belajar Visual Basic 6.0”)
End If
MsgBox (“Isi dulu Nama anda”)
Else
MsgBox (“Nama Saya” + Text1.Text + “Dan saya sedang belajar Visual Basic 6.0”)
End If
Contoh 3: Program Edit Database
Tampilan pada form:
Berikut ini listing program berdasarkan komponen-komponen di dalamnya:
- Command Button “Simpan”:
Private Sub Command1_Click()
‘Addnew = untuk membuat atau menambah data baru
Data1.Recordset.AddNew
‘perintah untuk menyimpan data yang di inputkan pada textbox ke dalam field database
Data1.Recordset!nrp = Text1.Text
Data1.Recordset!nama = Text2.Text
Data1.Recordset!jurusan = Text3.Text
‘update = perintah untuk mengupdate data yang telah ditambahkan
Data1.Recordset.Update
‘perintah untuk merefresh grid ketika data sudah ditambahkan
DBGrid1.Refresh
‘perintah untuk mengkosongkan textbox setelah data disimpan
Text1.Text = “”
Text2.Text = “”
Text3.Text = “”
End Sub
‘Addnew = untuk membuat atau menambah data baru
Data1.Recordset.AddNew
‘perintah untuk menyimpan data yang di inputkan pada textbox ke dalam field database
Data1.Recordset!nrp = Text1.Text
Data1.Recordset!nama = Text2.Text
Data1.Recordset!jurusan = Text3.Text
‘update = perintah untuk mengupdate data yang telah ditambahkan
Data1.Recordset.Update
‘perintah untuk merefresh grid ketika data sudah ditambahkan
DBGrid1.Refresh
‘perintah untuk mengkosongkan textbox setelah data disimpan
Text1.Text = “”
Text2.Text = “”
Text3.Text = “”
End Sub
- Command Button “Edit”
Private Sub Command2_Click()
‘Edit = untuk mengedit data yang sudah ada
Data1.Recordset.Edit
‘perintah untuk menyimpan data yang di sudah di edit
Data1.Recordset!nrp = Text1.Text
Data1.Recordset!nama = Text2.Text
Data1.Recordset!jurusan = Text3.Text
Data1.Recordset.Update
DBGrid1.Refresh
Text1.Text = “”
Text2.Text = “”
Text3.Text = “”
End Sub
‘Edit = untuk mengedit data yang sudah ada
Data1.Recordset.Edit
‘perintah untuk menyimpan data yang di sudah di edit
Data1.Recordset!nrp = Text1.Text
Data1.Recordset!nama = Text2.Text
Data1.Recordset!jurusan = Text3.Text
Data1.Recordset.Update
DBGrid1.Refresh
Text1.Text = “”
Text2.Text = “”
Text3.Text = “”
End Sub
- Command Button “Hapus”
Private Sub Command3_Click()
Data1.Recordset.Delete
DBGrid1.Refresh
Text1.Text = “”
Text2.Text = “”
Text3.Text = “”
End Sub
Data1.Recordset.Delete
DBGrid1.Refresh
Text1.Text = “”
Text2.Text = “”
Text3.Text = “”
End Sub
- DBGrid1:
Private Sub DBGrid1_Click()
‘kode perintah untuk menampilkan data yang sudah disimpan di dalam grid ke textbox
Text1.Text = Data1.Recordset!nrp
Text2.Text = Data1.Recordset!nama
Text3.Text = Data1.Recordset!jurusan
End Sub
‘kode perintah untuk menampilkan data yang sudah disimpan di dalam grid ke textbox
Text1.Text = Data1.Recordset!nrp
Text2.Text = Data1.Recordset!nama
Text3.Text = Data1.Recordset!jurusan
End Sub
Listing program:
Label1.Left = Label1.Left – 15
If (Label1.Left <= -(Label1.Width)) Or (Label1.Left >= Me.ScaleWidth) Then Label1.Left = Me.ScaleWidth
If (Label1.Left <= -(Label1.Width)) Or (Label1.Left >= Me.ScaleWidth) Then Label1.Left = Me.ScaleWidth
Contoh 5: Form Pengguna
Tampilan Form:
Pengaturan properties pada tiap komponen:
OBJECK | PROPERTIES | PENGATURAN |
Label1 | (Name) Caption | Lbluser User ID |
Label2 | (Name) Caption | Lbluser Nama Lengkap |
Label3 | (Name) Caption | Lbluser Password |
Label4 | (Name) Caption | Lbluser Ketik Ulang Password |
Label5 | (Name) Caption | Lbluser Type User |
Text1 | (Name) | TxtUser |
Text2 | (Name) | TxtNama |
Text3 | (Name) | TxtPassword |
Text4 | (Name) | TxtUlang |
Text5 | (Name) | TxtType |
Option1 | (Name) | optAdm |
Option2 | (Name) | optOperator |
Listview1 | (Name) | Lv1 |
Command1 | (Name) Caption | Cmdtambah Tam&bah |
Command2 | (Name) Caption | Cmdtambah Sim&pan |
Command3 | (Name) Caption | Cmdtambah Ha&pus |
Command4 | (Name) Caption | Cmdtambah Bat&al |
Contoh komponen database “Pengguna”. Anda bisa membuatnya di Microsoft Access:
FIELD NAME | DATA TYPE | SIZE |
Username | Text | 25 |
Nama | Text | 25 |
Password | Text | 10 |
Type | Text | 10 |
Oh
iya, jangan lupa untuk menambahkan references “Microsoft Activex Data
objeck 2.0 Library” agar database tersebut terhubung pada program Visual
Basic Anda.
Berikut ini adalah listing program berdasarkan komponen-komponen pada form:
- tombol “Tambah”:
Text1.Enabled = True
Text1.SetFocus
Text1.Text = “”
Text2.Text = “”
CmdTambah.Enabled = False
CmdSimpan.Enabled = True
CmdSimpan.Caption = “Simpan”
CmdKeluar.Caption = “Batal”
Text1.SetFocus
Text1.Text = “”
Text2.Text = “”
CmdTambah.Enabled = False
CmdSimpan.Enabled = True
CmdSimpan.Caption = “Simpan”
CmdKeluar.Caption = “Batal”
- Tombol “Simpan”:
If Text4.Text <> Text3.Text Then
MsgBox “Password tidak sama, ketik ulang password “, vbInformation
Exit Sub
End If
MsgBox “Password tidak sama, ketik ulang password “, vbInformation
Exit Sub
End If
If Text1.Text = “” Or Text2.Text = “” Or Text3.Text = “” Or Text4.Text = “” Or Text5.Text = “” Then
MsgBox “DATA tidak boleh kosong!”, _
vbInformation + vbOKOnly, “Perhatian”
Text1.SetFocus
Else
‘On Error Resume Next
If TbSimpan.Caption = “Simpan” Then
SqlInsert = “”
SqlInsert = “INSERT INTO UsrKontrol” _
& ” (UsrName,” _
& ” Nama,” _
& ” Pass,” _
& ” Tipe)” _
& ” VALUES(‘” & Text1.Text & “‘,'” _
& Text2 & “‘,'” _
& Text3 & “‘,'” _
& Text5.Text & “‘)”
MsgBox “data telah disimpan”, vbInformation, “keterangan”
DbKoneksi.Execute SqlInsert, , adCmdText
Rs_UsrKontrol.Requery
Call FormNormal
Call Form_Load
MsgBox “DATA tidak boleh kosong!”, _
vbInformation + vbOKOnly, “Perhatian”
Text1.SetFocus
Else
‘On Error Resume Next
If TbSimpan.Caption = “Simpan” Then
SqlInsert = “”
SqlInsert = “INSERT INTO UsrKontrol” _
& ” (UsrName,” _
& ” Nama,” _
& ” Pass,” _
& ” Tipe)” _
& ” VALUES(‘” & Text1.Text & “‘,'” _
& Text2 & “‘,'” _
& Text3 & “‘,'” _
& Text5.Text & “‘)”
MsgBox “data telah disimpan”, vbInformation, “keterangan”
DbKoneksi.Execute SqlInsert, , adCmdText
Rs_UsrKontrol.Requery
Call FormNormal
Call Form_Load
ElseIf TbSimpan.Caption = “Ubah” Then
‘ eksekusiSQL “update UsrKontrol set UsrName='” & safeSQL(Text1) & “‘,Nama='” & safeSQL(Text2) & “‘, Pass='” & safeSQL(Text3) & “‘ tipe='” & safeSQL(Text5) & “‘”
eksekusiSQL “update UsrKontrol set nama='” & safeSQL(Text2) & “‘,pass='” & safeSQL(Text3) & “‘, tipe='” & safeSQL(Text5) & “‘ where usrname='” & safeSQL(Text1) & “‘”
MsgBox “Update data sukses.”, vbInformation
Call FormNormal
Call Form_Load
End If
‘ eksekusiSQL “update UsrKontrol set UsrName='” & safeSQL(Text1) & “‘,Nama='” & safeSQL(Text2) & “‘, Pass='” & safeSQL(Text3) & “‘ tipe='” & safeSQL(Text5) & “‘”
eksekusiSQL “update UsrKontrol set nama='” & safeSQL(Text2) & “‘,pass='” & safeSQL(Text3) & “‘, tipe='” & safeSQL(Text5) & “‘ where usrname='” & safeSQL(Text1) & “‘”
MsgBox “Update data sukses.”, vbInformation
Call FormNormal
Call Form_Load
End If
End If
- Tombol “Hapus”:
Konfirmasi = MsgBox(“Anda yakin akan menghapus data ini?”, vbYesNo + vbQuestion, “informasi”)
If Konfirmasi = vbYes Then
SqlDelete = “DELETE FROM UsrKontrol WHERE ” _
& ” Usrname='” & Text1.Text & “‘”
If Konfirmasi = vbYes Then
SqlDelete = “DELETE FROM UsrKontrol WHERE ” _
& ” Usrname='” & Text1.Text & “‘”
DbKoneksi.Execute SqlDelete, , adCmdText
Rs_UsrKontrol.Requery
Call FormNormal
Call Form_Load
Else
Call FormNormal
Call Form_Load
Else
Call FormNormal
End If
- Tombol “Batal”:
If TbKeluar.Caption = “Keluar” Then
‘ frmMain.Enabled = True
Unload Me
Else
FormNormal
End If
‘ frmMain.Enabled = True
Unload Me
Else
FormNormal
End If
- Program pada Form_Load:
Sub FormMati()
Text1.Enabled = False
Text2.Enabled = False
Text3.Enabled = False
Text4.Enabled = False
Text5.Text = False
Text1.Enabled = False
Text2.Enabled = False
Text3.Enabled = False
Text4.Enabled = False
Text5.Text = False
End Sub
Contoh 6: Menampilkan Grafik di Visual Basic
Tampilan form:
Object Properties:
Listing Program berdasarkan komponen yang digunakan:
- Form Load:
Private Sub Form_Load()
‘List Combo: chart types
With Combo1
.AddItem “3D Bar”
.AddItem “2D Bar”
.AddItem “3D Line”
.AddItem “2D LIne”
.AddItem “3D Area”
.AddItem “2D Area”
.AddItem “3D Step”
.AddItem “2D Step”
.AddItem “3D Combination”
.AddItem “2D Combination”
.ListIndex = 1 ‘default:2D Bar
End With
Check1.Caption = “&Show Legends”
‘List Combo: chart types
With Combo1
.AddItem “3D Bar”
.AddItem “2D Bar”
.AddItem “3D Line”
.AddItem “2D LIne”
.AddItem “3D Area”
.AddItem “2D Area”
.AddItem “3D Step”
.AddItem “2D Step”
.AddItem “3D Combination”
.AddItem “2D Combination”
.ListIndex = 1 ‘default:2D Bar
End With
Check1.Caption = “&Show Legends”
‘2D array –> chart values
Dim X(1 To 7, 1 To 6) As Variant
X(1, 2) = “Jakarta”
X(1, 3) = “Bandung”
X(1, 4) = “Cirebon”
X(1, 5) = “Bogor”
X(1, 6) = “Sukabumi”
Dim X(1 To 7, 1 To 6) As Variant
X(1, 2) = “Jakarta”
X(1, 3) = “Bandung”
X(1, 4) = “Cirebon”
X(1, 5) = “Bogor”
X(1, 6) = “Sukabumi”
X(2, 1) = “JAN”
X(2, 2) = 3
X(2, 3) = 4
X(2, 4) = 5
X(2, 5) = 6
X(2, 6) = 7
X(2, 2) = 3
X(2, 3) = 4
X(2, 4) = 5
X(2, 5) = 6
X(2, 6) = 7
X(3, 1) = “FEB”
X(3, 2) = 4
X(3, 3) = 5
X(3, 4) = 3
X(3, 5) = 8
X(3, 6) = 12
X(3, 2) = 4
X(3, 3) = 5
X(3, 4) = 3
X(3, 5) = 8
X(3, 6) = 12
X(4, 1) = “MAR”
X(4, 2) = 1
X(4, 3) = 3
X(4, 4) = 8
X(4, 5) = 10
X(4, 6) = 9
X(4, 2) = 1
X(4, 3) = 3
X(4, 4) = 8
X(4, 5) = 10
X(4, 6) = 9
X(5, 1) = “APR”
X(5, 2) = 4
X(5, 3) = 6
X(5, 4) = 12
X(5, 5) = 10
X(5, 6) = 14
X(5, 2) = 4
X(5, 3) = 6
X(5, 4) = 12
X(5, 5) = 10
X(5, 6) = 14
X(6, 1) = “MAY”
X(6, 2) = 2
X(6, 3) = 9
X(6, 4) = 7
X(6, 5) = 12
X(6, 6) = 8
X(6, 2) = 2
X(6, 3) = 9
X(6, 4) = 7
X(6, 5) = 12
X(6, 6) = 8
X(7, 1) = “JUN”
X(7, 2) = 12
X(7, 3) = 19
X(7, 4) = 5
X(7, 5) = 19
X(7, 6) = 10
‘2D array –> chart data
MSChart1.ChartData = X
End Sub
X(7, 2) = 12
X(7, 3) = 19
X(7, 4) = 5
X(7, 5) = 19
X(7, 6) = 10
‘2D array –> chart data
MSChart1.ChartData = X
End Sub
- Check Box:
Private Sub Check1_Click()
If Check1.Value = 1 Then
‘Show Legends
MSChart1.ShowLegend = True
Check1.Caption = “&Hide Legends”
Else
‘Hide Legends
MSChart1.ShowLegend = False
Check1.Caption = “&Show Legends”
End If
End Sub
If Check1.Value = 1 Then
‘Show Legends
MSChart1.ShowLegend = True
Check1.Caption = “&Hide Legends”
Else
‘Hide Legends
MSChart1.ShowLegend = False
Check1.Caption = “&Show Legends”
End If
End Sub
- Combo Box:
Private Sub Combo1_Click()
‘chart type –>run time.
MSChart1.chartType = Combo1.ListIndex
End Sub
‘chart type –>run time.
MSChart1.chartType = Combo1.ListIndex
End Sub
Sekian contoh-contoh program Visual Basic yang kami
jelaskan pada artikel kali ini. Semoga contoh-contoh di atas dapat
membuat Anda paham tentang bahasa pemrograman Visual Basic. Jika Anda
hendak melihat beberapa contoh bahasa pemrogaman lain yang mirip dengan
Visual Basic dan bersifat multi platform, simak contoh program Delphi yang ada di situs ini.
0 Comments:
Posting Komentar