Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Data.Common
- Imports System.IO
- Imports System.Xml
- Public Class Form1
- ' Structure to store president info
- Structure PresidentsInfo
- Dim strFirstName As String 'President first name
- Dim strLastName As String 'President last name
- Dim intExtension As Integer 'President extension number
- Dim strDialCode As String 'First 4 characters of president last name
- End Structure
- ' Constants
- Const conFILENAME As String = "USPres.txt"
- Const conINCREASESIZEBY As Integer = 10
- Const conEXTENSIONSTART As Integer = 1001
- Const conINCREMENTEXTSBY As Integer = 2
- Dim aryPI(0) As PresidentsInfo
- Dim intArySize As Integer 'Number of presidents
- Dim intSelectNumber As Integer
- 'Main Form
- Private Sub Form1_Load(sender As Object, e As EventArgs)
- Call Restart()
- Call LoadAry()
- End Sub
- Private Sub Restart()
- 'Setup or restart program
- lblInfo.Text = " "
- End Sub
- Private Sub LoadAry()
- 'Read PresidentFile and load arySC
- Dim i As Integer 'File placement value
- Dim strName As String
- Dim intPos As Integer
- Dim aryTemp() As String
- Dim strData As String
- Dim intExtension As Integer = conEXTENSIONSTART
- strData = My.Computer.FileSystem.ReadAllText(conFILENAME)
- aryTemp = Split(strData, vbCrLf)
- For i = 0 To aryTemp.Length - 1
- intArySize += 1
- If intArySize = aryPI.Length Then
- ReDim Preserve aryPI(aryPI.Length - 1 + conINCREASESIZEBY)
- End If
- aryPI(intArySize - 1) = New PresidentsInfo()
- intPos = aryTemp(i).IndexOf(", ")
- Dim strLname As String = aryTemp(i).Substring(0, intPos)
- Dim strFname As String = aryTemp(i).Substring(intPos + 2)
- strName = aryTemp(i)
- aryPI(intArySize - 1).strLastName = aryTemp(i).Substring(0, intPos)
- aryPI(intArySize - 1).strFirstName = aryTemp(i).Substring(intPos + 2)
- aryPI(intArySize - 1).strDialCode = GetDialCode(strLname, strFname)
- aryPI(intArySize - 1).intExtension = intExtension
- intExtension += conINCREMENTEXTSBY
- Next
- End Sub
- Private Sub DialButton_Click(sender As Object, e As EventArgs) Handles btnABC.Click, btnDEF.Click, btnGHI.Click, btnJKL.Click,
- btnMNO.Click, btnPQRS.Click, btnTUV.Click, btnWXYZ.Click
- Dim button As Button = sender
- 'Is this the first one?
- If intSelectNumber < 4 Then
- intSelectNumber += 1
- If intSelectNumber = 4 Then
- txtButtonsPressed.Text = txtButtonsPressed.Text & button.Tag
- Else
- txtButtonsPressed.Text = txtButtonsPressed.Text & button.Tag
- End If
- End If
- If intSelectNumber = 4 Then
- lblInfo.Text = "Already Selected name."
- End If
- End Sub
- Private Function GetDialCode(strL As String, strF As String) As String
- Dim aryStrConvert() As String = {"X", "1", "ABC", "DEF", "GHI", "JKL", "MNO", "PQRS", "TUV", "WXYZ"}
- Dim strCheck As String = (strL & strF).Substring(0, 4).ToUpper()
- Dim strOut As String = ""
- For i = 0 To 3
- For j = 2 To 9
- Dim digit As Integer = Val(strCheck(i))
- If aryStrConvert(j).Contains(strCheck(i)) Then
- strOut &= j.ToString()
- Exit For
- End If
- Next
- Next
- Return strOut
- End Function
- 'Handle the button clicks To add a digit To the pressed buttons text box.
- Private Sub txtButtonsPressed_TextChanged(sender As Object, e As EventArgs) Handles txtButtonsPressed.TextChanged
- 'Check if need to display name
- If intSelectNumber = 4 Then
- Call DisplayPresidents(txtButtonsPressed.Text.ToUpper())
- End If
- End Sub
- Private Sub DisplayPresidents(ByRef strDCode As String)
- Dim strFormat As String = "{0,-15}{1,-15} x{2,4}"
- Dim strLine As String
- Dim strLastName As String
- LstOut.Items.Clear()
- For i = 1 To intArySize
- strLastName = aryPI(i).strLastName
- If strLastName.Length >= 4 AndAlso strLastName.Substring(0, 4).ToUpper() = strDCode Then
- strLine = String.Format(strFormat, aryPI(i).strFirstName, strLastName, aryPI(i).intExtension)
- LstOut.Items.Add(strLine)
- End If
- Next
- End Sub
- Private Sub LstOut_SelectedIndexChanged(sender As Object, e As EventArgs)
- Dim strExt As String
- Dim intPos As Integer
- For i As Integer = 0 To LstOut.SelectedItems.Count - 1
- ' Get the selected item from lstOut
- strExt = LstOut.SelectedItems(i).ToString()
- ' Find the position of " x" in strExt
- intPos = strExt.IndexOf(" x")
- ' Extract the extension number from strExt
- If intPos >= 0 Then
- strExt = strExt.Substring(intPos + 2)
- End If
- ' Search for the president with the extension number
- For j As Integer = 1 To intArySize
- If strExt = aryPI(j).intExtension Then
- ' Display the president's information in lblInfo
- lblInfo.Text = "Dialing: " & aryPI(j).strFirstName & " " &
- aryPI(j).strLastName & " was " & j & " president"
- Exit Sub
- End If
- Next
- Next
- End Sub
- Private Sub BtnRestart_Click_1(sender As Object, e As EventArgs)
- txtButtonsPressed.Clear()
- LstOut.Items.Clear()
- End Sub
- Private Sub BtnClose_Click_1(sender As Object, e As EventArgs)
- Me.Close()
- End Sub
- Private Sub BtnClear_Click_1(sender As Object, e As EventArgs)
- txtButtonsPressed.Clear()
- End Sub
- Private Sub BtnInstruction_Click_1(sender As Object, e As EventArgs)
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement