Advertisement
EchoLops

Dial

Mar 20th, 2023 (edited)
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.17 KB | None | 0 0
  1. Imports System.Data.Common
  2. Imports System.IO
  3. Imports System.Xml
  4.  
  5. Public Class Form1
  6.  
  7.  
  8. ' Structure to store president info
  9. Structure PresidentsInfo
  10. Dim strFirstName As String 'President first name
  11. Dim strLastName As String 'President last name
  12. Dim intExtension As Integer 'President extension number
  13. Dim strDialCode As String 'First 4 characters of president last name
  14. End Structure
  15.  
  16. ' Constants
  17. Const conFILENAME As String = "USPres.txt"
  18. Const conINCREASESIZEBY As Integer = 10
  19. Const conEXTENSIONSTART As Integer = 1001
  20. Const conINCREMENTEXTSBY As Integer = 2
  21. Dim aryPI(0) As PresidentsInfo
  22. Dim intArySize As Integer 'Number of presidents
  23.  
  24. Dim intSelectNumber As Integer
  25.  
  26.  
  27.  
  28. 'Main Form
  29. Private Sub Form1_Load(sender As Object, e As EventArgs)
  30. Call Restart()
  31. Call LoadAry()
  32.  
  33.  
  34.  
  35. End Sub
  36.  
  37. Private Sub Restart()
  38. 'Setup or restart program
  39. lblInfo.Text = " "
  40.  
  41. End Sub
  42.  
  43. Private Sub LoadAry()
  44. 'Read PresidentFile and load arySC
  45. Dim i As Integer 'File placement value
  46. Dim strName As String
  47. Dim intPos As Integer
  48. Dim aryTemp() As String
  49. Dim strData As String
  50. Dim intExtension As Integer = conEXTENSIONSTART
  51.  
  52. strData = My.Computer.FileSystem.ReadAllText(conFILENAME)
  53. aryTemp = Split(strData, vbCrLf)
  54.  
  55. For i = 0 To aryTemp.Length - 1
  56. intArySize += 1
  57. If intArySize = aryPI.Length Then
  58. ReDim Preserve aryPI(aryPI.Length - 1 + conINCREASESIZEBY)
  59. End If
  60. aryPI(intArySize - 1) = New PresidentsInfo()
  61. intPos = aryTemp(i).IndexOf(", ")
  62. Dim strLname As String = aryTemp(i).Substring(0, intPos)
  63. Dim strFname As String = aryTemp(i).Substring(intPos + 2)
  64. strName = aryTemp(i)
  65. aryPI(intArySize - 1).strLastName = aryTemp(i).Substring(0, intPos)
  66. aryPI(intArySize - 1).strFirstName = aryTemp(i).Substring(intPos + 2)
  67. aryPI(intArySize - 1).strDialCode = GetDialCode(strLname, strFname)
  68. aryPI(intArySize - 1).intExtension = intExtension
  69. intExtension += conINCREMENTEXTSBY
  70. Next
  71.  
  72.  
  73. End Sub
  74. Private Sub DialButton_Click(sender As Object, e As EventArgs) Handles btnABC.Click, btnDEF.Click, btnGHI.Click, btnJKL.Click,
  75. btnMNO.Click, btnPQRS.Click, btnTUV.Click, btnWXYZ.Click
  76. Dim button As Button = sender
  77.  
  78. 'Is this the first one?
  79. If intSelectNumber < 4 Then
  80. intSelectNumber += 1
  81. If intSelectNumber = 4 Then
  82. txtButtonsPressed.Text = txtButtonsPressed.Text & button.Tag
  83. Else
  84. txtButtonsPressed.Text = txtButtonsPressed.Text & button.Tag
  85. End If
  86. End If
  87.  
  88. If intSelectNumber = 4 Then
  89. lblInfo.Text = "Already Selected name."
  90. End If
  91.  
  92. End Sub
  93.  
  94. Private Function GetDialCode(strL As String, strF As String) As String
  95. Dim aryStrConvert() As String = {"X", "1", "ABC", "DEF", "GHI", "JKL", "MNO", "PQRS", "TUV", "WXYZ"}
  96. Dim strCheck As String = (strL & strF).Substring(0, 4).ToUpper()
  97. Dim strOut As String = ""
  98. For i = 0 To 3
  99. For j = 2 To 9
  100. Dim digit As Integer = Val(strCheck(i))
  101. If aryStrConvert(j).Contains(strCheck(i)) Then
  102. strOut &= j.ToString()
  103. Exit For
  104. End If
  105. Next
  106. Next
  107.  
  108.  
  109. Return strOut
  110. End Function
  111.  
  112. 'Handle the button clicks To add a digit To the pressed buttons text box.
  113.  
  114.  
  115. Private Sub txtButtonsPressed_TextChanged(sender As Object, e As EventArgs) Handles txtButtonsPressed.TextChanged
  116. 'Check if need to display name
  117. If intSelectNumber = 4 Then
  118. Call DisplayPresidents(txtButtonsPressed.Text.ToUpper())
  119. End If
  120.  
  121. End Sub
  122.  
  123. Private Sub DisplayPresidents(ByRef strDCode As String)
  124. Dim strFormat As String = "{0,-15}{1,-15} x{2,4}"
  125. Dim strLine As String
  126. Dim strLastName As String
  127. LstOut.Items.Clear()
  128.  
  129. For i = 1 To intArySize
  130. strLastName = aryPI(i).strLastName
  131. If strLastName.Length >= 4 AndAlso strLastName.Substring(0, 4).ToUpper() = strDCode Then
  132. strLine = String.Format(strFormat, aryPI(i).strFirstName, strLastName, aryPI(i).intExtension)
  133. LstOut.Items.Add(strLine)
  134. End If
  135. Next
  136. End Sub
  137.  
  138.  
  139. Private Sub LstOut_SelectedIndexChanged(sender As Object, e As EventArgs)
  140. Dim strExt As String
  141. Dim intPos As Integer
  142.  
  143. For i As Integer = 0 To LstOut.SelectedItems.Count - 1
  144. ' Get the selected item from lstOut
  145. strExt = LstOut.SelectedItems(i).ToString()
  146.  
  147. ' Find the position of " x" in strExt
  148. intPos = strExt.IndexOf(" x")
  149.  
  150. ' Extract the extension number from strExt
  151. If intPos >= 0 Then
  152. strExt = strExt.Substring(intPos + 2)
  153. End If
  154.  
  155. ' Search for the president with the extension number
  156. For j As Integer = 1 To intArySize
  157. If strExt = aryPI(j).intExtension Then
  158. ' Display the president's information in lblInfo
  159. lblInfo.Text = "Dialing: " & aryPI(j).strFirstName & " " &
  160. aryPI(j).strLastName & " was " & j & " president"
  161. Exit Sub
  162. End If
  163. Next
  164. Next
  165. End Sub
  166.  
  167. Private Sub BtnRestart_Click_1(sender As Object, e As EventArgs)
  168. txtButtonsPressed.Clear()
  169. LstOut.Items.Clear()
  170. End Sub
  171.  
  172. Private Sub BtnClose_Click_1(sender As Object, e As EventArgs)
  173. Me.Close()
  174. End Sub
  175.  
  176. Private Sub BtnClear_Click_1(sender As Object, e As EventArgs)
  177. txtButtonsPressed.Clear()
  178. End Sub
  179.  
  180. Private Sub BtnInstruction_Click_1(sender As Object, e As EventArgs)
  181.  
  182. End Sub
  183.  
  184. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement