UMB Blog Network Part of the UMB Blog Network

jon.frankel’s blog

Blog Network

Perfect Numbers

‘ Check out the Perfect Numbers page on WikiPedia to learn what a perfect number is.

   Private Sub btnEvaluate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEvaluate.Click
        Dim x As Integer
        Dim sum As Integer

        For x = 1 To 9999
            sum = 0
            lstFactors.Items.Clear()
            For i As Integer = 1 To x / 2
                If x Mod i = 0 Then
                    lstFactors.Items.Add(i)
                    sum += i
                End If
            Next

            If sum = x Then
                MessageBox.Show(x & " is perfect!")
            End If
        Next

    End Sub

.Split - Used for processing .CSV files

 

 Private Sub btnProcess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnProcess.Click
        Dim sr As IO.StreamReader = IO.File.OpenText("inventory.csv")
        Dim strArray() As String

        Do While sr.Peek <> -1
            strArray = sr.ReadLine.Split(","c)
        Loop

        sr.Close()
    End Sub

Final Project

The grading criteria for the final project is that it should include all of the following:

Vars Named, Loops, If Test, File Read, File Write, Array, Procedure, Creative (try something new)!, GUI

Writing Files - A simple Word Processor

    Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
        Dim sr As IO.StreamReader

        If IO.File.Exists(txtFileName.Text) Then
            sr = IO.File.OpenText(txtFileName.Text)
            lstFileRead.Items.Clear()

            Do While sr.Peek <> -1
                lstFileRead.Items.Add(sr.ReadLine)
            Loop

            sr.Close()
        Else
            MessageBox.Show("Sorry buddy - your file doesn’t exist")
        End If

    End Sub

    Private Sub btnWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWrite.Click
        Dim sw As IO.StreamWriter

        If rdoAppend.Checked Then
            sw = IO.File.AppendText(txtFileName.Text)
        Else
            sw = IO.File.CreateText(txtFileName.Text)
        End If

        sw.WriteLine(txtNewLine.Text)

        sw.Close()

    End Sub

 

Read file into array in Form Load

    Dim state(50) As String

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim sr As IO.StreamReader =    IO.File.OpenText("states.txt")                  
        Dim i As Integer = 1

        Do While sr.Peek <> -1
            state(i) = sr.ReadLine
            i += 1

        Loop

        sr.Close()
    End Sub

 

Arrays - Find a state tax rate

Public Class Form1

    Dim strState() As String = {"", "MA", "NH", "VT", "NY"}
    Dim intTax() As Integer = {0, 5, 0, 6, 7}
    Dim intNumStates = 4

    Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click

        For i As Integer = 1 To intNumStates
            If txtState.Text = strState(i) Then
                MessageBox.Show("Your rate is " & intTax(i))
            End If
        Next

    End Sub

End Class
 

Nested For..Next Loop

 

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        For row As Integer = 1 To 4
            For col As Integer = row To 5
                lstSearch.Items.Add("At row/Col " & _
                        row & " / " & col)
            Next
        Next
    End Sub
 

Nested Loops with Reading 2 File

‘ This program reads the states.txt database file, and looks for matches corresponding to the ones in the file
‘ typed into the txtMyStates text box. 

        Dim strReadLine, strReadMyList As String

        ‘Note file location is in bin\debug of this project
        Dim srdatabase As IO.StreamReader

        Dim srmylist As IO.StreamReader = _
            IO.File.OpenText(txtMyStates.Text & ".txt")

        Do While srmylist.Peek <> -1

            srdatabase = IO.File.OpenText("states.txt")
            strReadMyList = srmylist.ReadLine

            Do While srdatabase.Peek <> -1
                strReadLine = srdatabase.ReadLine

                If strReadLine.Length > strReadMyList.Length Then
                    If strReadMyList = _
                      strReadLine.Substring(0, strReadMyList.Length) Then
                        lstResults.Items.Add(strReadLine)
                    End If
                End If

            Loop

            srdatabase.Close()
        Loop

        srmylist.Close()

    End Sub
 

Better Read of File using Loop/Peek

 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim strReadLine As String
        Dim intTaxRate, maxTaxRate As Integer

        Dim sr As IO.StreamReader = _
            IO.File.OpenText("states.txt")                    ‘Note file location is in bin\debug of this project

        Do While sr.Peek <> -1
            strReadLine = sr.ReadLine

            lstStates.Items.Add(strReadLine.Substring(0, 2))

            intTaxRate = strReadLine.Substring(3, 1)
            If intTaxRate > maxTaxRate Then
                lblMaxRate.Text = strReadLine
                maxTaxRate = intTaxRate
            End If

        Loop

        sr.Close()

    End Sub
 

For Next Loop

‘ Two ways to do the same loop (which add up numbers from 1 to whatever I put into the textbox)

    Private Sub BtnSummation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSummation.Click

        ‘Dim i, sum As Integer

        ‘Do While i <= txtMaxNum.Text
        ‘    sum = sum + i
        ‘    i += 1
        ‘Loop

        ‘MessageBox.Show("Your sum is " & sum)

        Dim sum As Integer

        For i As Integer = 1 To txtMaxNum.Text step 1
            sum += i
        Next

        MessageBox.Show("Your sum is " & sum)

    End Sub