[Page 706 (continued)] Exercises 9.1 1. | The currently selected item in lstBox , Mozart, is displayed in txtOutput. | 3. | The last item, Tchaikovsky, from lstBox is displayed in txtOutput. | 5. | The number 3 is displayed in txtOutput. | 7. | The item Haydn is inserted into lstBox between Chopin and Mozart. | 9. | The first item, Bach, is removed from lstBox. | 11. | The last item, Tchaikovsky, is removed from lstBox. | 13. | The number 2 is displayed in the text box. | 15. | The list box will be empty. | 17. | cboBox.Text = CStr(cboBox.Items(0)) | 19. | cboBox.Items.Remove("Shakespeare") | | [Page 707] | 21. | cboBox.Items.RemoveAt(cboBox.Items.Count - 1) | 23. | For i As Integer = 0 To cboBox.Items.Count - 1 Step 2 lstOutput.Items.Add(cboBox.Items(i)) Next | 25. | Dim found As Boolean = False For i As Integer = 0 To cboBox.Items.Count - 1 If CStr(cboBox.Items(i)) = "Cervantes" Then found = True End If Next If found Then txtOutput.Text = "Cervantes is in the list." Else txtOutput.Text = "Cervantes not found." End If | 27. | Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim total As Double = 0 For i As Integer = 0 To lstNumbers.Items.Count - 1 total += CDbl(lstNumbers.Items(i)) Next txtOutput.Text = "The average of the numbers is " & _ total / lstNumbers.Items.Count & "." End Sub | 29. | Private Sub btnDisplay_Click(...) Handles btnDisplay.Click For i As Integer = 0 To lstNumbers.Items.Count - 1 Step 2 lstOutput.Items.Add(lstNumbers.Items(i)) Next End Sub | 31. | Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim smallest, largest As Double 1stNumbers.Sorted = True smallest = CDbl(lstNumbers.Items(0)) largest = CDbl(lstNumbers.Items(lstNumbers.Items.Count - 1)) txtOutput.Text = "The spread is " & (largest - smallest) & "." End Sub | 33. | Private Sub btnSort_Click(...) Handles btnSort.Click 'Load names from a file into a sorted List Box 'and write them to another file. Dim sr As IO.StreamReader = IO.File.OpenText("POPULAR_NAMES.TXT") Do While sr.Peek <> -1 lstOutput.Items.Add(sr.ReadLine) Loop sr.Close() 'Open the output file. Dim sw As IO.StreamWriter = IO.File.CreateText("SORTED_NAMES.TXT") For i As Integer = 0 To lstOutput.Items.Count - 1 sw.WriteLine(lstOutput.Items(i)) Next sw.Close() MsgBox("The ordered file has been created.", 0, "DONE") End Sub | | [Page 708] | 35. | Private Sub btnDisplay_Click(...) Handles btnDisplay.Click 'Convert a length from one unit to the other. 'The inches array stores the number of inches in each unit. Dim inches() As Double = {1, 12, 3 * 12, 100 / 2.54, 12 * 5280} Dim result As Double 'Result 'If input is not specified, display message. If (lstFrom.SelectedIndex < 0) Or _ (lstTo.SelectedIndex < 0) Or _ (txtAmount.Text = "") Then MsgBox("Please select both units and enter a length.",0,"Incomplete") Else 'Convert the first units to inches, then convert to second units. result = CDbl(txtAmount.Text) * inches(lstFrom.SelectedIndex) _ / inches(lstTo.SelectedIndex) 'Display the decimal places in the result depending upon the size. If result < 1 Then txtOutput.Text = FormatNumber(result, 6) Else txtOutput.Text = FormatNumber(result, 2) End If End If End Sub | 37. | 'Note: This event procedure handles events from all three combo boxes. Private Sub SelectedIndexChanged(...) Handles _ cboBrand.SelectedIndexChanged, _ cboMemory.SelectedIndexChanged, _ cboMonitor.SelectedIndexChanged 'Update output if all choices have been made. If (cboBrand.SelectedIndex >= 0) And _ (cboMemory.SelectedIndex >= 0) And _ (cboMonitor.SelectedIndex >= 0) Then txtOutput.Text = "You have a " & cboBrand.Text & " computer with " & cboMemory.Text & _ " of memory and a " & cboMonitor.Text & _ " monitor." End If End Sub | Exercises 9.2 1. | The word "Income" becomes the caption embedded in the top of GroupBox1. | 3. | The check box becomes (or remains) unchecked. | 5. | The radio button becomes (or remains) unselected. | 7. | The scroll box moves to its rightmost position. | 9. | Clicking on the arrow on either end of the scroll bar will move the button the same ("large") distance as clicking on the bar between the scroll box and an arrow. | 11. | The Tick event will be triggered every intVar seconds | 13. | Any picture placed into the picture box will be automatically enlarged or contracted to fit the picture box. | | [Page 709] | 15. | A green rectangle of width 200 pixels and height 100 pixels will be drawn in the picture box. Its upper-left corner will be 25 pixels from the left side of the picture box and 50 units from the top side. | 17. | RadioButton1.Text = "Yes" | 19. | RadioButton2.Checked = False | 21. | HScrollBar2.Value = CInt(HScrollBar2.Minimum + _ (HScrollBar2.Maximum - HScrollBar2.Minimum) / 3) | 23. | PictureBox1.CreateGraphics.DrawEllipse(Pens.Yellow, _ 10, 10, 100, 100) | 25. | RadioButton2 is selected and RadioButton1 is unselected. | 27. | Check boxes, combo boxes, horizontal scroll bars, list boxes, radio buttons, and vertical scroll bars can receive the focus. | 29. | Yes | 31. | Dim box(2) As CheckBox Private Sub frmCount_Load(...) Handles MyBase.Load box(0) = CheckBox1 box(1) = CheckBox2 box(2) = CheckBox3 End Sub Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim numChecked As Integer = 0 For i As Integer = 0 To 2 If box(i).Checked Then numChecked += 1 End If Next txtOutput.Text = CStr(numChecked) End Sub | 33. | 'This event procedure is called when any of the checkboxes 'or radio buttons change. Private Sub CheckedChanged(...) Handles _ radDeluxe.CheckedChanged, _ radSuper.CheckedChanged, _ chkMultimedia.CheckedChanged, _ chkModem.CheckedChanged, _ chkMemory.CheckedChanged 'Update the cost of the system. Dim cost As Double cost = 0 'Add amounts to the cost based upon selections. If radDeluxe.Checked Then cost += 1500 Else 'Super model cost += 1700 End If If chkMultimedia.Checked Then cost += 300 End If [Page 710] If chkModem.Checked Then cost += 100 End If If chkMemory.Checked Then cost += 50 End If txtOutput.Text = FormatCurrency(cost) End Sub | 35. | Dim phase As Integer 'Current phase of the moon Private Sub tmrMoon_Tick(...) Handles tmrMoon.Tick 'Update the phase, and display the image. 'Timer Interval setting is 2000. 'Timer Enabled setting is True. phase += 1 If phase = 9 Then phase = 1 End If picBox.Image = Image.FromFile("Moon" & phase & ".BMP") End Sub | 37. | Public Sub vsbFahren_ValueChanged(...) Handles vsbFahren.ValueChanged 'When Fahrenheit value changes, update the Celsius value. Dim celsius As Integer txtFahren.Text = CStr(vsbFahren.Value) celsius = CInt((5 / 9) * (vsbFahren.Value - 32)) 'Update Celsius value if necessary. If this IF statement 'is not included, the program would be in an infinite loop. If vsbCelsius.Value <> celsius Then 'This line will cause the other event procedure to execute. vsbCelsius.Value = celsius End If End Sub Public Sub vsbCelsius_ValueChanged(...) _ Handles vsbCelsius.ValueChanged 'When Celsius value changes, update the Fahrenheit value. Dim fahrenheit As Integer txtCelsius.Text = CStr(vsbCelsius.Value) fahrenheit = CInt((9 / 5) * vsbCelsius.Value + 32) 'Update Fahrenheit value if necessary. If this If statement 'is not included, the program would be in an infinite loop. If vsbFahren.Value <> fahrenheit Then 'This line will cause the other event procedure to execute. vsbFahren.Value = fahrenheit End If End Sub | 39. | Dim count As Integer = 10 Private Sub tmrBall_Tick(...) Handles tmrBall.Tick 'Update the value of the scroll bar and label. 'Timer Interval setting is 1000. count = count - 1 vsbBall.Value = 10 - count lblBall.Text = CStr(count) [Page 711] 'If at zero, display the message and end program. If count = 0 Then tmrBall.Enabled = False MsgBox("HAPPY NEW YEAR!!!!", 0, "NYE") End End If End Sub | Exercises 9.3 1. | The contents of the clipboard are deleted. | 3. | The text currently selected in txtBox, if any, is copied into the clipboard. | 5. | The contents of the clipboard are displayed in txtBox. | 7. | The contents of the clipboard are assigned to the variable strVar. | 9. | The name of one of the 43 U.S. Presidents is selected at random and displayed in txtBox. | 11. | Two states are selected at random and displayed in the list box. | 13. | A check mark appears in front of the mnuOrderAsc menu item. | 15. | The text appearing on the mnuOrderAsc item will be changed to Increasing Order. | 17. | The String variable address will be available to all forms in the program. | 19. | txtBox.SelectedText = Clipboard.GetText | 21. | Clipboard.SetText("Rosebud") | 23. | txtBox.SelectedText = "" | 25. | Dim randomNum As New Random() txtBox.Text = CStr(randomNum.Next(1, 101)) | 27. | Dim randomNum As New Random() 'A and Z have ANSI values 65 and 90. txtBox.Text = Chr(randomNum.Next(65, 91)) | 29. | Dim randomNum As New Random() txtBox.Text = CStr(randomNum(1, 7) + randomNum(1, 7)) | 31. | mnuOrderDesc.Checked = False | 33. | Me.Close() | 35. | The menu item mnuOrderAsc is greyed out and cannot be selected. | 37. | Private Sub mnuCopy_Click(...) Handles mnuCopy.Click 'Copy selected text into clipboard. Clipboard.SetText(txtBox.SelectedText) End Sub Private Sub mnuPaste_Click(...) Handles mnuPaste.Click 'Paste clipboard into text box. txtBox.SelectedText = Clipboard.GetText End Sub [Page 712]Private Sub mnuCut_Click(...) Handles mnuCut.Click 'Copy selected text into clipboard, and delete selected text. Clipboard.SetText(txtBox.SelectedText) txtBox.SelectedText = "" End Sub Private Sub mnuExit_Click(...) Handles mnuExit.Click End 'Terminate the program. End Sub | 39. | Private Sub btnDeal_Click(...) Handles btnDeal.Click 'Deal five cards at random. Dim deckOfCards(51) As String Dim suits() As String = {"Hearts", "Diamonds", "Clubs", _ "Spades"} Dim n As Integer Dim randomNum As New Random() Dim temp As String 'Populate each suit of the deck. For i As Integer = 0 To 3 'Populate numbers 2 through 10. For j As Integer = 2 To 10 deckOfCards(i * 13 + j) = CStr(j) & " of " & suits(i) Next 'Populate the honors cards. deckOfCards(i * 13 + 0) = "King of " & suits(i) deckOfCards(i * 13 + 1) = "Ace of " & suits(i) deckOfCards(i * 13 + 11) = "Jack of " & suits(i) deckOfCards(i * 13 + 12) = "Queen of " & suits(i) Next 'Choose five cards at random. For i As Integer = 1 To 5 'Choose a card at random. n = randomNum.Next(1, 53 - i) lstOutput.Items.Add(deckOfCards(n)) 'Swap the chosen card with the one at the end. temp = deckOfCards(n) deckOfCards(n) = deckOfCards(52 - i) deckOfCards(52 - i) = temp Next End Sub | 41. | Dim randomNum As New Random() 'Random number generator Private Sub btnToss_Click(...) Handles btnToss.Click 'Toss a coin, and keep track of results. If randomNum.Next(0, 2) = 0 Then txtOutcome.Text = "Heads" txtHeads.Text = CStr(CInt(txtHeads.Text) + 1) Else txtOutcome.Text = "Tails" txtTails.Text = CStr(CInt(txtTails.Text) + 1) End If 'Display total tosses and percentage. txtTosses.Text = CStr(CInt(txtTosses.Text) + 1) txtPercentHeads.Text = FormatPercent(CDbl(txtHeads.Text) / _ CDbl(txtTosses.Text)) End Sub | [Page 713]Exercises 9.4 1. | Private Sub btnDraw_Click(...) Handles btnDraw.Click Dim gr As Graphics = picBox.CreateGraphics Dim x, y, r As Double x = picBox.Width / 2 y = picBox.Height / 2 r = x / 2 If r > y / 2 Then r = y / 2 End If gr.FillEllipse(Brushes.Black, CSng(x - r), CSng(y - r), _ CSng(2 * r), CSng(2 * r)) End Sub | 3. | Private Sub btnDraw_Click(...) Handles btnDraw.Click Dim gr As Graphics = picBox.CreateGraphics Dim x, y, r As Double x = picBox.Width / 2 y = picBox.Height / 2 r = 20 gr.FillEllipse(Brushes.Red, CSng(x - r), CSng(y - 4), _ CSng(2 * r), CSng(2 * r)) r = 19 gr.FillEllipse(Brushes.Red, CSng(x - r), CSng(y - r), _ CSng(2 * r), CSng(2 * r)) End Sub | 5. | Private Sub btnCreate_Click(...) Handles btnCreate.Click Dim gr As Graphics = picFlag.CreateGraphics Dim br() As Brush = {Brushes.Green, Brushes.White, Brushes.Red} 'picFlag.Width = 99 'picFlag.Height = 149 For i As Integer = 0 To 2 gr.FillRectangle(br(i), 0 + i * 50, 0, 50, 99) Next gr.DrawLine(Pens.Black, 0, 0, 148, 0) 'top border gr.DrawLine(Pens.Black, 0, 0, 0, 98) 'left border gr.DrawLine(Pens.Black, 0, 98, 148, 98) 'bottom border gr.DrawLine(Pens.Black, 148, 0, 148, 98) 'right border End Sub | 7. | Private Sub btnCreate_Click(...) Handles btnCreate.Click Dim gr As Graphics = picFlag.CreateGraphics Dim br() As Brush = {Brushes.Orange, Brushes.White, Brushes.Green} Dim r As Integer = 12 'radius of circle 'picFlag.Width = 99 'picFlag.Height = 149 For i As Integer = 0 To 2 gr.FillRectangle(br(i), 0, 0 + i * 33, 149, 33) Next gr.FillPie(Brushes.Orange, 75 - r, 49 - r, 2 * r, 2 * r, 0, 360) gr.DrawLine(Pens.Black, 0, 0, 148, 0) 'top border gr.DrawLine(Pens.Black, 0, 0, 0, 98) 'left border gr.DrawLine(Pens.Black, 0, 98, 148, 98) 'bottom border gr.DrawLine(Pens.Black, 148, 0, 148, 98)'right border End Sub | | [Page 714] | 9. | Private Sub btnDraw_Click(...) Handles btnDraw.Click Dim n As Integer Dim numbers As String = "" Dim gr As Graphics = picBox.CreateGraphics n = CInt(txtNumber.Text) 'Font is Microsoft Sans Serif For i As Integer = 0 To n If i < 9 Then numbers &= CStr(i) & " " Else numbers &= CStr(i) & " " End If gr.DrawLine(Pens.Blue, 12 + (24 * i), 5, 12 + (24 * i), 15) Next gr.DrawLine(Pens.Blue, 0, 10, 24 * (n + 1), 10) gr.DrawString(numbers, Me.Font, Brushes.Blue, 8, 20) End Sub | |