Previous Page

Search for a president by order of office (by number) - Visual Basic

Below is the code, but make sure the text file (USPRES.TXT) is saved as the same name as in the code below.
- Save it in the bin/Debug folder, also.

Here is that text file you'll need. (right click to save - left click to view contents) USPRES.TXT


Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim n, num As Integer
        Dim name As String
        Dim sr As IO.StreamReader = IO.File.OpenText("USPRES.TXT")
        n = CInt(TextBox1.Text)
        If (1 <= n) And (n <= 44) Then
            num = 0
            Do
                name = sr.ReadLine
                num += 1
            Loop Until num = n
            TextBox2.Text = "President number " & n & " was " & name
            sr.Close()
        End If
        TextBox1.Clear()
        TextBox1.Focus()
    End Sub
End Class


Below is a sample input/output.

presidents visual basic form