Listing programnya:
Public Class Form1
Dim vButton As System.Windows.Forms.Button
'buat variabel general utk menyimpan tombol yang invisible, misal vButton
'buat procedure aktif untuk memindah tulisan dan warna tombol ke lokasi yang kosong(tombol yang invisible)
Sub aktif(ByVal b As System.Windows.Forms.Button)
vButton.Visible = True
vButton.Text = b.Text
vButton.BackColor = b.BackColor
b.Visible = False
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button6.Click, Button5.Click, Button4.Click, Button3.Click, Button2.Click
Call aktif(sender)
vButton = sender
'panggil procedure aktif
'ganti variable vButton
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
vButton = Button6
'setting awal tombol 6 di invisible
End Sub
End Class
cara lain yang lebih panjang tapi secara logika lebih mudah dimengerti:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'cara 1 masukkan kode ini di setiap tombol, dan ganti tombol yang visible dan invisible
'warna tolbol belum dimasukkan
'If Button2.Visible = False Then
' Button2.Visible = True
' Button2.Text = Button1.Text
' Button1.Visible = False
'ElseIf Button3.Visible = False Then
' Button3.Visible = True
' Button3.Text = Button1.Text
' Button1.Visible = False
'ElseIf Button4.Visible = False Then
' Button4.Visible = True
' Button4.Text = Button1.Text
' Button1.Visible = False
'ElseIf Button5.Visible = False Then
' Button5.Visible = True
' Button5.Text = Button1.Text
' Button1.Visible = False
'ElseIf Button6.Visible = False Then
' Button6.Visible = True
' Button6.Text = Button1.Text
' Button1.Visible = False
'End If
'===========================sampai sini cara panjang 1;
'cara 2
'===========================
Call aktif(Button1)
'panggil prosedur aktif, buat disemua tombol seperti ini
End Sub
'buat prosedure aktif
Sub aktif(ByVal b As System.Windows.Forms.Button)
If Button1.Visible = False Then
Button1.Visible = True
Button1.Text = b.Text
Button1.BackColor = b.BackColor
b.Visible = False
ElseIf Button2.Visible = False Then
Button2.Visible = True
Button2.Text = b.Text
Button2.BackColor = b.BackColor
b.Visible = False
ElseIf Button3.Visible = False Then
Button3.Visible = True
Button3.Text = b.Text
Button3.BackColor = b.BackColor
b.Visible = False
ElseIf Button4.Visible = False Then
Button4.Visible = True
Button4.Text = b.Text
Button4.BackColor = b.BackColor
b.Visible = False
ElseIf Button5.Visible = False Then
Button5.Visible = True
Button5.Text = b.Text
Button5.BackColor = b.BackColor
b.Visible = False
ElseIf Button6.Visible = False Then
Button6.Visible = True
Button6.Text = b.Text
Button6.BackColor = b.BackColor
b.Visible = False
End If
End Sub
Rabu, 30 Maret 2011
Rabu, 09 Maret 2011
Visual Basic 2008
Variabel public : Variabel yang bisa digunakan kesemua program/ form (dalam satu project)
Variabel General : Variabel dimana variabel bisa digunakan/ dipanggil dalam satu class
Variabel Lokal : Variabel yang bisa dibaca/ dipanggil pada satu private/ satu sub/ satu procedure saja.
Setiap objek dalam pembuatan coding pasti diikuti dengan properties
contoh :
TextBox1.Text (Text dibelakang merupakan properties)
public class form1
Dim Jurusan as string (variabel general)
Dim Kelas, Jenjang as string, Waktu as string (variabel general)
Dim d as string (variabel konstanta)
private sub Button Click....
Dim lo as sring (variabel lokal)
lo=6
If TextBox1.Text="TI" Then
TextBox2.Text="Tehnik Informatika" + Trim(lo)
else
If TextBox1.Text="SI" Then
TextBox2.Text="Sistem Informatika"
end if
end if
end sub
untuk lebih mudah kita dapat menggunakan "call" karena perintah ini dapat dipakai pada beberapa sub.
sub perintah()
Dim lo as sring
private sub Button Click....
call perintah (perintah call)
end sub
call perintah (perintah call)
end sub
sub perintah()
Dim lo as sring
lo=6
If TextBox1.Text="TI" Then
TextBox2.Text="Tehnik Informatika" + Trim(lo)
else
If TextBox1.Text="SI" Then
TextBox2.Text="Sistem Informatika"
end if
end if
end sub
Membuat cursor berpindah aktif ke text berikutnya dengan tombol enter.
if e.key char = chr (13) Then
TextBox1.focus() (perintah focus untuk cursor aktif ke text berikutnya)
ListBox
Listbox1.Items.add(nilai yang dimasukkan)
contoh:
Listbox1.Items.add(TextBox1.text+""+Textbox2.text)
Combobox
Combobox.Items.add(Textbox1.text)
u=4
y=7
for j=4 To Val(Textbox2.text)
If j Mod u=0 Then
Listbox1.Items.add(j)
u=j
else
If j mod y=0 Then
Listbox1.Items.add(j)
y=j
end if
end if
hasilnya=4 7 8 14 16 28 32
end sub
Membuat cursor berpindah aktif ke text berikutnya dengan tombol enter.
if e.key char = chr (13) Then
TextBox1.focus() (perintah focus untuk cursor aktif ke text berikutnya)
ListBox
Listbox1.Items.add(nilai yang dimasukkan)
contoh:
Listbox1.Items.add(TextBox1.text+""+Textbox2.text)
Combobox
Combobox.Items.add(Textbox1.text)
Looping
Contoh:
Dim j,u,y as integeru=4
y=7
for j=4 To Val(Textbox2.text)
If j Mod u=0 Then
Listbox1.Items.add(j)
u=j
else
If j mod y=0 Then
Listbox1.Items.add(j)
y=j
end if
end if
hasilnya=4 7 8 14 16 28 32
Jumat, 18 Februari 2011
Langganan:
Postingan (Atom)