Sabtu, 16 April 2011
Contoh Program Visual Studio 2008
Public Class latihan3Dwi
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
'Menmggunakan fungsi Keypress
If e.KeyChar = Chr(13) Then
'enter kode ascci nya chr 13
TextBox2.Focus()
'textBox2.focus() --- utk memindahkan kursor ke berikutnya
End If
End Sub
Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
If e.KeyChar = Chr(13) Then
'enter kode ascci nya chr 13
TextBox3.Focus()
'textBox2.focus() --- utk memindahkan kursor ke berikutnya
End If
End Sub
Sub gabung()
' gabung nama methodenya ' membuat sebuah methode yang berisi program didalammnya
ListBox1.Items.Add(Trim(TextBox1.Text) & " " & Trim(TextBox2.Text) & " " & Trim(TextBox3.Text))
'Perintah ini untuk menggabungkan,Fungsi trim berfumgsi utk menghilangkan spasi
End Sub
Private Sub TextBox3_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox3.KeyPress
If e.KeyChar = Chr(13) Then
'enter kode ascci nya chr 13
Call gabung()
End If
'call gabung utk memanggil kalau kita enter di box 3
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call gabung()
End Sub
End Class
Contoh Program Vb 2008
Public Class Form3
'membuat Variabel array
Dim listtxt(3) As String
Dim listitem As ListViewItem
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'memasukkan nilai ke variabel array
listtxt(0) = Trim(TextBox1.Text)
listtxt(1) = Trim(TextBox2.Text)
listtxt(2) = Trim(TextBox3.Text)
'untuk memasukkan ke listview dari variabel array
listitem = New ListViewItem(listtxt)
ListView1.Items.Add(listitem)
End Sub
'setting kolom listview
Sub setlistview()
ListView1.View = View.Details
'untuk menentukan kolom
ListView1.Columns.Add("NPM", 100, HorizontalAlignment.Center)
ListView1.Columns.Add("Nama", 200, HorizontalAlignment.Left)
ListView1.Columns.Add("Alamat", 400, HorizontalAlignment.Left)
End Sub
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call setlistview()
End Sub
Private Sub ListView1_ItemSelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.ListViewItemSelectionChangedEventArgs) Handles ListView1.ItemSelectionChanged
'mengambil nilai dari listview
TextBox4.Text = e.Item.Text
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
End Sub
End Class
Contoh Program Vb.net dengan menggunakan modul
Public Class Form2
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
h = TextBox1.Text
b = TextBox2.Text
Call hitung()
TextBox3.Text = hasil
End Sub
End Class
Module Modul3
'variabel public
Public nama As String, hasil, h, b As Integer
Sub tampilnama()
nama = " Adit Bangkit Prastya "
End Sub
Sub hitung()
hasil = Val(h) * Val(b)
End Sub
End Module
Contoh Program Vb.net
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call gfor()
Call dowhile()
End Sub
sub gfor()
Dim I As Integer
ListBox1.Items.Clear()
For I = Val(TextBox1.Text) To Val(TextBox2.Text)
If I Mod 3 = 0 And I Mod 7 > 0 Then
ListBox1.Items.Add(I)
End If
Next
End Sub
Sub dowhile()
Dim A As Integer
ListBox2.Items.Clear()
A = Val(TextBox1.Text)
Do While A <= Val(TextBox2.Text)
If Microsoft.VisualBasic.Right(A, 1) = 3 Then
ListBox2.Items.Add(A)
End If
A = A + 1
Loop
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
End Sub
End Class
PENGULANGAN
Pengulangan adalah suatu instruksi dalam pemrograman agar sekelompok perintah dilaksanakan dengan berulang sampai dengan suatu kondisi terpenuhi.
Berikut contoh pengulangan dalan visual studio 2008
1. For
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim I As Integer
ListBox1.Items.Clear()
For I = 1 To 10
ListBox1.Items.Add(I)
Next
End Sub
2. Do While
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim I As Integer
ListBox1.Items.Clear()
Do While I <= 10
ListBox1.Items.Add(I)
I = I + 1
Loop
End Sub
3. For Next
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim I As Integer
ListBox1.Items.Clear()
For I = Val(TextBox2.Text) To Val(TextBox3.Text)
ListBox1.Items.Add(I)
Next
End Sub
4. Do While Next
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim I As Integer
ListBox1.Items.Clear()
I = Val(TextBox2.Text)
Do While I <= Val(TextBox3.Text)
ListBox1.Items.Add(I)
I = I + 1
Loop
End Sub
5. For Ganjil & Genap
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Dim I As Integer
ListBox1.Items.Clear()
For I = Val(TextBox2.Text) To Val(TextBox3.Text)
If I Mod 2 = 0 Then
ListBox1.Items.Add("Bil Genap " & I)
Else
ListBox1.Items.Add("Bil Ganjil " & I)
End If
Next
End Sub
6.Do While Ganjil & Genap
Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim I As Integer
ListBox1.Items.Clear()
Do I = Val(TextBox1.Text)to Val(TextBox2.Text)
Do I Mod 2 = 0 while
ListBox1.Items.Add("Bil genap " & I)
while
ListBox1.Items.Add("Bil ganjil " & I)
Loop
End Sub
7. For 3,-6,9,-12, 15
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
Dim I As Integer
ListBox1.Items.Clear()
For I = Val(TextBox2.Text) To Val(TextBox3.Text)
If I Mod 3 = 0 And I Mod 2 = 0 Then
ListBox1.Items.Add(I * -1)
Else
If I Mod 3 = 0 Then
ListBox1.Items.Add(I)
End If
End If
Next
End Sub
8. Do While 3,-6,9,-12, 15
Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
Dim I As Integer
ListBox1.Items.Clear()
I = Val(TextBox2.Text)
Do While I <= Val(TextBox3.Text)
If I Mod 3 = 0 And I Mod 2 = 0 Then
ListBox1.Items.Add(I * -1)
Else
If I Mod 3 = 0 Then
ListBox1.Items.Add(I)
End If
End If
I = I + 1
Loop
End Sub
Berikut contoh pengulangan dalan visual studio 2008
1. For
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim I As Integer
ListBox1.Items.Clear()
For I = 1 To 10
ListBox1.Items.Add(I)
Next
End Sub
2. Do While
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim I As Integer
ListBox1.Items.Clear()
Do While I <= 10
ListBox1.Items.Add(I)
I = I + 1
Loop
End Sub
3. For Next
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim I As Integer
ListBox1.Items.Clear()
For I = Val(TextBox2.Text) To Val(TextBox3.Text)
ListBox1.Items.Add(I)
Next
End Sub
4. Do While Next
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim I As Integer
ListBox1.Items.Clear()
I = Val(TextBox2.Text)
Do While I <= Val(TextBox3.Text)
ListBox1.Items.Add(I)
I = I + 1
Loop
End Sub
5. For Ganjil & Genap
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Dim I As Integer
ListBox1.Items.Clear()
For I = Val(TextBox2.Text) To Val(TextBox3.Text)
If I Mod 2 = 0 Then
ListBox1.Items.Add("Bil Genap " & I)
Else
ListBox1.Items.Add("Bil Ganjil " & I)
End If
Next
End Sub
6.Do While Ganjil & Genap
Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim I As Integer
ListBox1.Items.Clear()
Do I = Val(TextBox1.Text)to Val(TextBox2.Text)
Do I Mod 2 = 0 while
ListBox1.Items.Add("Bil genap " & I)
while
ListBox1.Items.Add("Bil ganjil " & I)
Loop
End Sub
7. For 3,-6,9,-12, 15
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
Dim I As Integer
ListBox1.Items.Clear()
For I = Val(TextBox2.Text) To Val(TextBox3.Text)
If I Mod 3 = 0 And I Mod 2 = 0 Then
ListBox1.Items.Add(I * -1)
Else
If I Mod 3 = 0 Then
ListBox1.Items.Add(I)
End If
End If
Next
End Sub
8. Do While 3,-6,9,-12, 15
Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
Dim I As Integer
ListBox1.Items.Clear()
I = Val(TextBox2.Text)
Do While I <= Val(TextBox3.Text)
If I Mod 3 = 0 And I Mod 2 = 0 Then
ListBox1.Items.Add(I * -1)
Else
If I Mod 3 = 0 Then
ListBox1.Items.Add(I)
End If
End If
I = I + 1
Loop
End Sub
Langganan:
Postingan (Atom)