Minggu, 10 Juni 2018

LOOPING (PERULANGAN)

BAB 6. LOOPING (PERULANGAN)

Syntax
While condition  
    [ statements ]  
    [ Continue While ]  
    [ statements ]  
    [ Exit While ]  
    [ statements ]  
End While  

Parts

Term Definition
condition Required. Boolean expression. If condition is Nothing, Visual Basic treats it as False.
statements Optional. One or more statements following While, which run every time condition is True.
Continue While Optional. Transfers control to the next iteration of the While block.
Exit While Optional. Transfers control out of the While block.
End While Required. Terminates the definition of the While block.

Remarks

Use a While...End While structure when you want to repeat a set of statements an indefinite number of times, as long as a condition remains True. If you want more flexibility with where you test the condition or what result you test it for, you might prefer the Do...Loop Statement. If you want to repeat the statements a set number of times, the For...Next Statement is usually a better choice.
Note
The While keyword is also used in the Do...Loop Statement, the Skip While Clause and the Take While Clause.
If condition is True, all of the statements run until the End While statement is encountered. Control then returns to the While statement, and condition is again checked. If condition is still True, the process is repeated. If it’s False, control passes to the statement that follows the End While statement.
The While statement always checks the condition before it starts the loop. Looping continues while the condition remains True. If condition is False when you first enter the loop, it doesn’t run even once.
The condition usually results from a comparison of two values, but it can be any expression that evaluates to a Boolean Data Type value (True or False). This expression can include a value of another data type, such as a numeric type, that has been converted to Boolean.
You can nest While loops by placing one loop within another. You can also nest different kinds of control structures within one another. For more information, see Nested Control Structures.

Exit While

The Exit While statement can provide another way to exit a While loop. Exit While immediately transfers control to the statement that follows the End While statement.
You typically use Exit While after some condition is evaluated (for example, in an If...Then...Else structure). You might want to exit a loop if you detect a condition that makes it unnecessary or impossible to continue iterating, such as an erroneous value or a termination request. You can use Exit While when you test for a condition that could cause an endless loop, which is a loop that could run an extremely large or even infinite number of times. You can then use Exit While to escape the loop.
You can place any number of Exit While statements anywhere in the While loop.
When used within nested While loops, Exit While transfers control out of the innermost loop and into the next higher level of nesting.
The Continue While statement immediately transfers control to the next iteration of the loop. For more information, see Continue Statement.

Example

In the following example, the statements in the loop continue to run until the index variable is greater than 10.
Dim index As Integer = 0
While index <= 10
    Debug.Write(index.ToString & " ")
    index += 1
End While

Debug.WriteLine("")
' Output: 0 1 2 3 4 5 6 7 8 9 10 

Example

The following example illustrates the use of the Continue While and Exit While statements.
Dim index As Integer = 0
While index < 100000
    index += 1

    ' If index is between 5 and 7, continue
    ' with the next iteration.
    If index >= 5 And index <= 8 Then
        Continue While
    End If

    ' Display the index.
    Debug.Write(index.ToString & " ")

    ' If index is 10, exit the loop.
    If index = 10 Then
        Exit While
    End If
End While

Debug.WriteLine("")
' Output: 1 2 3 4 9 10

Example

The following example reads all lines in a text file. The OpenText method opens the file and returns a StreamReader that reads the characters. In the While condition, the Peek method of the StreamReader determines whether the file contains additional characters.
Private Sub ShowText(ByVal textFilePath As String)
    If System.IO.File.Exists(textFilePath) = False Then
        Debug.WriteLine("File Not Found: " & textFilePath)
    Else
        Dim sr As System.IO.StreamReader = System.IO.File.OpenText(textFilePath)

        While sr.Peek() >= 0
            Debug.WriteLine(sr.ReadLine())
        End While

        sr.Close()
    End If
End Sub





LATIHAN













MESSAGE BOX

BAB 5. MESSAGE BOX



1.      Buat 6 label danatur text sesuaidengantampilan  form
2.      Buat 6buah textbox denganpengaturan properties sbb:
Namaobjek
Text
Name
Textbox1
Kosongkan
Textbox1
Textbox2
,,
Txtnokatalog
Textbox3
,,
Txtnamabarang
Textbox4
,,
Txtkemasan
Textbox5
,,
TxtSTOK
Combobox1
,,
ComboBoxJENISBARANG
Form
FORM DATA STOK
STOKBARANG
BUTTON1
Add
Btntambah
BUTTON2
Edit
Btnubah
BUTTON3
DELETE
Btnhapus
BUTTON4
Bersih
Btnbersih
BUTTON5
Exit
Btnkeluar
BUTTON6
Cari
Button2
BUTTON7
TAMPIL StokBarang
BUTTON3
LISTVIEW1
LVDATABARANG
3.      Cara membuatlistview
       KlikListviewpada toolbox  letakanpada form anda
 

a.       Kliktandapanahpadaujungkananataslistview
b.      Klikedit colom
c.       Klik view  padapilihanDetail
d.      Akan tampil menu
e.       Klik add sebanyak 5 kali danketik  text padacolumnheader properties
Sesuaidengantampilan yang di inginkandanaturlebar data sesuaitampilan.
f.       Klik ok jikaselesai
g.      Aktifkan gridlines pada properties padaposisi TRUE
1.      Mengetik listing program
 2.      Klik form kosongpada form danklik view code pada properties anda




Imports System.Data
PublicClass STOKBARANG
Sub ListViewDataBarang()
Dim buka AsString = "Select * From Q_Stok Order By Nomor_Katalog Asc"
Try
Using STOK AsNew OleDbConnection(My.Settings.STRKONEKSI)
Using Cmd AsNew OleDbCommand(buka, STOK)
                    STOK.Open()
Using Data As OleDbDataReader = Cmd.ExecuteReader()
                        LVDATABARANG.Items.Clear()
Dim x AsInteger = 0
DoWhile Data.Read
LVDATABARANG.Items.Add(Data("Nomor_Katalog"))
                            LVDATABARANG.Items(x).SubItems.Add(Data("Nama_Barang"))
                            LVDATABARANG.Items(x).SubItems.Add(Data("Kemasan"))
                            LVDATABARANG.Items(x).SubItems.Add(Data("Jumlah_Stok"))
                            LVDATABARANG.Items(x).SubItems.Add(Data("Jenis_Barang"))
                            x = x + 1
Loop
EndUsing
EndUsing
EndUsing
Catch ex As Exception
            MsgBox(ex.Message())
Finally
            buka = Nothing
EndTry
EndSub
1.  KLIK 2X FORM STOKBARANG
PrivateSub Stok_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
        ComboBoxJenisBarang.Items.Add("Elektronic")
        ComboBoxJenisBarang.Items.Add("komputer")
        ComboBoxJenisBarang.Items.Add("Tehnis")
        ComboBoxJenisBarang.Items.Add("Buku")
        ComboBoxJenisBarang.Items.Add("PENA")
        ComboBoxJenisBarang.Items.Add("Lain-lain")
Call ListViewDataBarang()
EndSub
2.  KLIK TXTNOKATALOG PILIH MODUS KEYDOWN
PrivateSub TxtNoKatalog_KeyDown(ByVal sender AsObject, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TXTNOKATALOG.KeyDown
Try
If e.KeyCode = Keys.Enter Then
Using STOK AsNew OleDbConnection(My.Settings.STRKONEKSI)
Using Cmd AsNew OleDbCommand("Select * From StokBarang Where Nomor_Katalog='"& TXTNOKATALOG.Text &"'", STOK)
                        STOK.Open()
Using Data As OleDbDataReader = Cmd.ExecuteReader
                            Data.Read()
If Data.HasRows = TrueThen
                                TXTNOKATALOG.Text = Data("Nomor_Katalog")
                                TXTNAMABARANG.Text = Data("Nama_Barang")
                                TXTKEMASAN.Text = Data("Kemasan")
                                TXTSTOK.Text = Data("Stok")
                                ComboBoxJENISBARANG.Text = Data("Jenis_Barang")
EndIf
EndUsing
EndUsing
EndUsing
EndIf
Catch ex As Exception
            MsgBox(ex.Message())
EndTry
EndSub
3.  KLIK LVDATABARANG
PrivateSub LVDataBarang_Click(ByVal sender AsObject, ByVal e As System.EventArgs) Handles LVDATABARANG.Click
Me.TxtNoKatalog.Text = Me.LVDataBarang.SelectedItems(0).Text.ToString
Me.TxtNamaBarang.Text = Me.LVDataBarang.SelectedItems(0).SubItems(1).Text.ToString
Me.TxtKemasan.Text = Me.LVDataBarang.SelectedItems(0).SubItems(2).Text.ToString
Me.TxtStok.Text = Me.LVDataBarang.SelectedItems(0).SubItems(3).Text.ToString
Me.ComboBoxJenisBarang.Text = Me.LVDataBarang.SelectedItems(0).SubItems(4).Text.ToString
EndSub
4.  KLIK TOMBOL ADD
PrivateSub BtnTambah_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNTAMBAH.Click
If TxtNoKatalog.Text = ""Then
            MsgBox("isikan data!", MsgBoxStyle.Information)
            TxtNoKatalog.Focus()
EndIf
Try
Dim Tambah AsString = "insert into StokBarang values ('"& TxtNoKatalog.Text &"','"& TxtNamaBarang.Text &"','"& TxtKemasan.Text &"','"& TxtStok.Text &"','"& ComboBoxJenisBarang.Text &"')"
Using STOK AsNew OleDbConnection(My.Settings.STRKONEKSI)
Using cmd AsNew OleDbCommand(Tambah, STOK)
                    STOK.Open()
                    cmd.CommandType = CommandType.Text
                    cmd.ExecuteNonQuery()
                    MsgBox("datanya sudah bertambah!")
Call ListViewDataBarang()
EndUsing
EndUsing
Catch ex As Exception
            MsgBox("DATA SUDAH ADA")
EndTry
EndSub
5.  KLIK TOMBOL EDIT
PrivateSub BtnUbah_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNUBAH.Click
Try
Dim Ubah AsString = "update StokBarang set Nomor_Katalog='"&Me.TxtNoKatalog.Text &"',"& _
"Nama_Barang='"&Me.TxtNamaBarang.Text &"',"& _
"Kemasan='"&Me.TxtKemasan.Text &"',"& _
"Stok='"&Me.TxtStok.Text &"',"& _
"Jenis_Barang='"&Me.ComboBoxJenisBarang.Text &"' where Nomor_Katalog='"&Me.TxtNoKatalog.Text &"'"
Using STOK AsNew OleDbConnection(My.Settings.STRKONEKSI)
Using cmd AsNew OleDbCommand(Ubah, STOK)
                    STOK.Open()
                    cmd.CommandType = CommandType.Text
                    cmd.ExecuteNonQuery()
EndUsing
EndUsing
            MsgBox("data telah di ubah...")
Call ListViewDataBarang()
            TxtNoKatalog.Focus()
Catch ex As Exception
            MsgBox(ex.Message())
EndTry
EndSub
6.  KLIK TOMBOL DELETE
PrivateSub BtnHapus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNHAPUS.Click
Try
Dim Hapus AsString = "delete * from StokBarang where Nomor_Katalog ='"&Me.TxtNoKatalog.Text &"'"
Using STOK AsNew OleDbConnection(My.Settings.STRKONEKSI)
Using cmd AsNew OleDbCommand(Hapus, STOK)
                    STOK.Open()
                    cmd.CommandType = CommandType.Text
                    cmd.ExecuteNonQuery()
EndUsing
EndUsing
Me.TxtNoKatalog.Text = ""
Me.TxtNamaBarang.Text = ""
Me.TxtKemasan.Text = ""
Me.TxtStok.Text = ""
Me.ComboBoxJenisBarang.Text = ""
            Hapus = Nothing
            MsgBox("Data Anda Telah Dihapus")
Call ListViewDataBarang()
Me.TxtNoKatalog.Focus()
Catch ex As Exception
            MsgBox(ex.Message())
EndTry
EndSub
7.  KLIK TOMBOL CLEAR
PrivateSub BtnBersih_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNBERSIH.Click
        TxtNoKatalog.Text = ""
        TxtNamaBarang.Text = ""
        TxtKemasan.Text = ""
        TxtStok.Text = ""
        ComboBoxJenisBarang.Text = ""
        TxtNoKatalog.Focus()
Call ListViewDataBarang()
EndSub
13.KLIK TOMBOL CARI
PrivateSub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim buka AsString = "Select * From StokBarang where Nama_Barang like '%"& TextBox1.Text &"%' Order By Nomor_Katalog Asc"
Try
Using STOK AsNew OleDbConnection(My.Settings.STRKONEKSI)
Using Cmd AsNew OleDbCommand(buka, STOK)
                    STOK.Open()
Using Data As OleDbDataReader = Cmd.ExecuteReader()
                        LVDATABARANG.Items.Clear()
Dim x AsInteger = 0
DoWhile Data.Read
LVDATABARANG.Items.Add(Data("Nomor_Katalog"))                            LVDATABARANG.Items(x).SubItems.Add(Data("Nama_Barang"))                          LVDATABARANG.Items(x).SubItems.Add(Data("Kemasan"))                          LVDATABARANG.Items(x).SubItems.Add(Data("Stok"))                            LVDATABARANG.Items(x).SubItems.Add(Data("Jenis_Barang"))
                            x = x + 1
Loop
EndUsing
EndUsing
EndUsing
Catch ex As Exception
            MsgBox(ex.Message())
Finally
            buka = Nothing
EndTry
EndSub
8.  KLIK TOMBOL TAMPIL DATA BARANG
PrivateSub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Call ListViewDataBarang()
EndSub
9.  KLIK TOMBOL EXIT
PrivateSub BtnKeluar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.Close()
EndSub
 end class






latihan 

 











KONEKSI FORM VB .NET DENGAN DATABASE ACCES DENGAN MENGGUNAKAN CODE PROGRAM

Buat Sebuah Database dengan nama BukuTamu.mdb dan tabel dengan Identitas. Desain form berikut ini: Buatlah perintah  berikut ini ...