0% found this document useful (0 votes)
42 views

Contoh 1 Visual Basic

The document provides examples of code for different components in Visual Basic programs. It includes code snippets for drop down menus, variables, buttons, checkboxes, and loading data from databases into forms and grids. It also gives examples of moving labels, form designs for user login interfaces, and code for displaying different types of charts with data.

Uploaded by

yapim mandau
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Contoh 1 Visual Basic

The document provides examples of code for different components in Visual Basic programs. It includes code snippets for drop down menus, variables, buttons, checkboxes, and loading data from databases into forms and grids. It also gives examples of moving labels, form designs for user login interfaces, and code for displaying different types of charts with data.

Uploaded by

yapim mandau
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

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 S
ystem.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
 variabel di Form1:

Public Class Form1
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
 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
 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
 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
jumlah = (hrgmakanan * porsimkn) + (hrgminuman * porsimnm)
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
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
 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
 Command Button “Hapus”

Private Sub Command3_Click()


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
Contoh 4: Menggerakan Label di Visual Basic
Tampilan form:

Listing program:
Label1.Left = Label1.Left – 15
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

 (Name)  Lbluser
 Label1
Caption User ID

 (Name)  Lbluser
 Label2
Caption Nama Lengkap

 (Name)  Lbluser
 Label3
Caption Password

 (Name)  Lbluser
 Label4
Caption Ketik Ulang Password

 Label5  (Name)  Lbluser


Caption 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

 (Name)  Cmdtambah
 Command1
Caption Tam&bah

 (Name)  Cmdtambah
 Command2
Caption Sim&pan

 (Name)  Cmdtambah
 Command3
Caption Ha&pus

 (Name)  Cmdtambah
 Command4
Caption Bat&al

Contoh komponen database “Pengguna”. Anda bisa membuatnya di Microsoft Access:


 FIELD 
 DATA TYPE  SIZE
 NAME

 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”
 Tombol “Simpan”:

If Text4.Text <> Text3.Text Then


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
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
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 & “‘”
DbKoneksi.Execute SqlDelete, , adCmdText

Rs_UsrKontrol.Requery

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
 Program pada Form_Load:

Sub FormMati()
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”
‘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”
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(3, 1) = “FEB”
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(5, 1) = “APR”
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(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
 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
 Combo Box:

Private Sub Combo1_Click()


‘chart type –>run time.
MSChart1.chartType = Combo1.ListIndex
End Sub

You might also like