Jumat, 11 Maret 2011

mengunakan FOR pada VB









Public Class Form1
    Private Sub clearList()
        'clear the list
        ListBox1.Items.Clear()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Declare Variable
        Dim intCount As Integer
        'Clear the list
        clearList()
        'perform a loop
        For intCount = 1 To 5
            'Add the item to the list
            ListBox1.Items.Add("I'm item " & intCount.ToString & _
            " int the list!")
        Next
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'clear the list
        clearList()
        For intCount As Integer = 4 To 62 Step 7
            ListBox1.Items.Add(intCount.ToString)
        Next
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        'clear the list
        clearList()
        For intCount As Integer = 10 To 1 Step -1
            ListBox1.Items.Add(intCount.ToString)
        Next
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        'clear the list
        clearList()
        'List each folder at the root of your C drive
        For Each strFolder As String In _
            My.Computer.FileSystem.GetDirectories("C:\")
            ListBox1.Items.Add(strFolder)
        Next
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Dim objRandom As New Random
        Dim intRandomNumber As Integer = 0
        clearList()
        Do Until intRandomNumber = 10
            intRandomNumber = objRandom.Next(25)
            ListBox1.Items.Add(intRandomNumber.ToString)
        Loop

    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Dim objRandom As New Random
        Dim intRandomNumber As Integer = 0
        clearList()
        Do While intRandomNumber < 15
            intRandomNumber = objRandom.Next(25)
            ListBox1.Items.Add(intRandomNumber.ToString)
        Loop
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        clearList()
        For intOuterLoop As Integer = 1 To 5
            For intInnerLoop As Integer = 1 To 5
                ListBox1.Items.Add(intOuterLoop.ToString & _
                " , " & intInnerLoop.ToString)
            Next
        Next
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        clearList()
        For Each strFolder As String In _
        My.Computer.FileSystem.GetDirectories("C:\")
            ListBox1.Items.Add(strFolder)
            If String.Compare(strFolder, "C:\program files", True) = 0 Then
                MessageBox.Show("Found it, eiting the loop now.", "loops")
                Exit For
            End If
        Next
    End Sub
End Class

Tidak ada komentar:

Posting Komentar