Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 86 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
86
Dung lượng
1,48 MB
Nội dung
Chapter 25: Where to Now? 795 Microsoft CodePlex: www.codeplex.com ASP .NET 2.0: www.asp.net Other Resources As mentioned earlier, there are hundreds of sites online that discuss both Visual Basic .NET and Visual Basic 2008. These sites give everything from news on moving from Visual Basic .NET to Visual Basic 2008, to listings of up and coming conferences worldwide. Although you can do a search for Visual Basic 2008, the number of sites returned can be extremely overwhelming. Let ’ s look at two of these possible sites, one for the United Kingdom and another for the United States. In the United Kingdom, www.vbug.co.uk offers a wealth of information on Visual Basic. This is the web site for the Visual Basic Users Group (VBUG), which you can join. Besides the web site, this group holds meetings and an annual conference, plus provides a magazine. There is a listing of further links on the web site, and you may want to use this to start your search over the Internet. In the United States you can get a journal, The Visual Studio Magazine , from a similar user group. Again, this journal is backed by meetings and four yearly conferences along with a web site, www.devx.com/ vb/ , which can give e - mail updates. On the web site, you have access to a number of different areas both in Visual Basic and other related and nonrelated Visual Studio areas. Of course, these are just two among the many out there to try to get you started. Remember, however, that the Internet is not the only place to find information, so we will go on to look at some resources not found on the Web. Offline Resources (Books) Wrox Press is committed to providing books that will help you develop your programming skills in the direction that you want. We have a selection of tutorial - style books that build on the Visual Basic 2008 knowledge gained here. These will help you to specialize in particular areas. Here are the details of a couple of key titles. Professional Visual Basic 2008 (Wrox Press, ISBN 978 - 0 - 470 - 19136 - 1) This book is different than other Visual Basic books because it explains intermediate to advanced topics in an easily understood and concise model. The comprehensive coverage provides detailed information on how to use Visual Basic in the ever - expanding .NET world, using not only explanations of the topics, but demonstrations of code. It effectively shows developers how to get tasks accomplished. This book is written to show readers what they need to know to take their abilities to new levels. The book shows developers exactly how to build everything from traditional console applications, ASP.NET applications, and XML web services. Along with these various applications, the book deals with the issues of security, data access (ADO.NET), and the latest Visual Studio .NET IDE, as well as introducing developers to everything they need to know to fully understand the new .NET 3.5 Framework. Topics include the following: Visual Studio 2008 Web services and .NET remoting ❑ ❑ ❑ ❑ c25.indd 795c25.indd 795 4/1/08 6:45:37 PM4/1/08 6:45:37 PM Chapter 25: Where to Now? 796 Deploying applications Windows Workflow Foundation Windows Presentation Foundation Windows Communication Foundation. .NET 3.5 Framework Common Language Runtime Applying objects and components Namespaces Error handling and debugging XML with VB.NET ASP.NET advanced features and much more! Visual Basic 2008 Programmer ’ s Reference (Wrox Press, 978 - 0 - 470 - 18262 - 8) Visual Basic 2008 Programmer ’ s Reference is a language tutorial and a reference guide to the 2008 release of Visual Basic. The tutorial provides basic material suitable for beginners but also includes in - depth content for more advanced developers. The second part of the book is a reference that quickly allows programmers to locate information for specific language features. The entries in these appendixes allow the reader to quickly review the details of important programming, objects, properties, methods, and events. Visual Basic 2008 Programmer ’ s Reference covers the latest features of the 2008 release, including: Changes to variable declaration and initialization XLinq support for XML data types; query comprehensions for using SQL - like syntax to extract data from arrays and other data structures Extension methods for adding new features to existing classes Nested subroutines and functions Anonymous subroutines and functions ( lambda expressions ) Nullable types Relaxed delegates Dynamic interfaces Dynamic identifiers ❑ ❑ ❑ ❑ ❑ ❑ ❑ ❑ ❑ ❑ ❑ ❑ ❑ ❑ ❑ ❑ ❑ ❑ ❑ ❑ c25.indd 796c25.indd 796 4/1/08 6:45:37 PM4/1/08 6:45:37 PM A Exercise Solutions Chapter 1 1. Create a Windows Application with a Textbox and Button control that will display whatever is typed in the text box when the user clicks on the button. A. To display the text from a text box on a form when the user clicks the button, you add code as highlighted here to the button ’ s Click event handler: Private Sub btnDisplay_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnDisplay.Click ‘Display the contents of the text box MessageBox.Show(txtInput.Text, “Exercise 1”) End Sub Chapter 3 1. Create a Windows application with two button controls. In the Click event for the first button, declare two Integer variables and set their values to any number that you like. Perform any math operation on these variables and display the results in a message box. In the Click event for the second button, declare two String variables and set their values to anything that you like. Perform a string concatenation on these variables and display the results in a message box. A. The first part of this exercise requires you to declare two Integer variables and set their values and then to perform a math operation of these variables and display the results in a message box. The variables can be declared and set as: ‘Declare variables and set their values Dim intX As Integer = 5 Dim intY As Integer = 10 bapp01.indd 797bapp01.indd 797 4/1/08 6:47:52 PM4/1/08 6:47:52 PM Appendix A: Exercise Solutions 798 To perform a math operation and display the results can be performed as: ‘Multiply the numbers and display the results MessageBox.Show(“The sum of “ & intX.ToString & “ * “ & _ intY.ToString & “ = “ & intX * intY, “Exercise 1”) The second part of this exercise requires you to declare two String variables and set their values and then to concatenate the variables and display the results in a message box. The String variables can be declared and set as: ‘Declare variables and set their values Dim strOne As String = “Visual Basic “ Dim strTwo As String = “2008” To concatenate the variables and display the results, you could write code such as: ‘Concatenate the strings and display the results MessageBox.Show(strOne & strTwo, “Exercise 1”) 2. Create a Windows application with a text box and a button control. In the button ’ s Click event, display three message boxes. The first message box should display the length of the string that was entered into the text box. The second message box should display the first half of the string, and the third message box should display the last half of the string. A. This exercise requires you to display the length of the string entered into a text box and then to display the first half of the string and the last half of the string. To display the length of the string, you can use the Length property of the Text property of the text box as shown here: ‘Display the length of the string from the TextBox MessageBox.Show(“The length of the string in the TextBox is “ & _ txtInput.Text.Length, “Exercise 2”) To display the first half of the string, you need to use the Substring method with a starting index of 0 and for the length you use the length of the string divided by 2 as shown here. Don ’ t forget that with the Option Strict option turned on, you must convert the results of a division operation to an Integer data type for use in the SubString method: ‘Display the first half of the string from the TextBox MessageBox.Show(txtInput.Text.Substring(0, _ CType(txtInput.Text.Length / 2, Integer)), “Exercise 2”) To display the last half of the string you again use the Substring method but this time you simply give it a starting index of the length of the string divided by 2 as shown here: ‘Display the last half of the string from the TextBox MessageBox.Show(txtInput.Text.Substring( _ CType(txtInput.Text.Length / 2, Integer)), “Exercise 2”) bapp01.indd 798bapp01.indd 798 4/1/08 6:47:52 PM4/1/08 6:47:52 PM Appendix A: Exercise Solutions 799 Chapter 4 1. Create a Windows Forms Application with a text box and a Button control. In the Click event of the Button, extract the number from the text box and use a Select Case statement with the numbers 1 through 5. In the Case statement for each number, display the number in a message box. Ensure that you provide code to handle numbers that are not in the range of 1 through 5. A. This exercise requires you to create a Select Case statement to select and display the numbers 1 through 5 from the text box on the form. The code to do this is shown here: ‘Determine which number was entered Select Case CType(txtNumber.Text, Integer) Case 1 MessageBox.Show(“The number 1 was entered”, “Exercise 1”) Case 2 MessageBox.Show(“The number 2 was entered”, “Exercise 1”) Case 3 MessageBox.Show(“The number 3 was entered”, “Exercise 1”) Case 4 MessageBox.Show(“The number 4 was entered”, “Exercise 1”) Case 5 MessageBox.Show(“The number 5 was entered”, “Exercise 1”) To handle numbers other than 1 through 5 you need to provide a Case Else statement as shown here: Case Else MessageBox.Show(“A number other that 1 - 5 was entered”, _ “Exercise 1”) End Select 2. Create a Windows Forms Application that contains a ListBox control and a Button control. In the Click event for the button, create a For . . . Next loop that will count from 1 to 10 and display the results in the list box. Then create another For . . . Next loop that will count backwards from 10 to 1 and also display those results in the list box. A. In this exercise, you are tasked with creating two For . . . Next loops. The first loop should count from 1 to 10 and display the numbers in a list box. The code to execute this loop is shown here: ‘Count from 1 to 10 For intCount As Integer = 1 To 10 lstData.Items.Add(intCount) Next The second For Next loop should count backward from 10 to 1 and display those numbers in the same list box. The code to execute this loop is shown here: ‘Count backwards from 10 to 1 For intCount As Integer = 10 To 1 Step -1 lstData.Items.Add(intCount) Next bapp01.indd 799bapp01.indd 799 4/1/08 6:47:53 PM4/1/08 6:47:53 PM Appendix A: Exercise Solutions 800 Chapter 5 1. Create a Windows Forms Application that contains three buttons. Add an enumeration of three names to your code. For the Click event for each button, display a message box containing a member name and value from the enumeration. A. This exercise requires you to create an enumeration of three names and to display the member string value as well as the numeric value when a button was clicked. To create an enumeration of names you would use code similar to this: Public Class Form1 Private Enum Names As Integer Jeannie = 1 Delinda = 2 Harry = 3 End Enum In order to display the member names and values from the enumeration, you would use code like this: Private Sub btnName1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnName1.Click MessageBox.Show(Names.Jeannie.ToString & “ = “ & Names.Jeannie, _ “Exercise 1”) End Sub Private Sub btnName2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnName2.Click MessageBox.Show(Names.Delinda.ToString & “ = “ & Names.Delinda, _ “Exercise 1”) End Sub Private Sub btnName3_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnName3.Click MessageBox.Show(Names.Harry.ToString & “ = “ & Names.Harry, _ “Exercise 1”) End Sub 2. Create a Windows Forms Application that contains a TextBox control and a Button control. At the form level, create a names array initialized with a single name. In the Click event for the button control, add the code to redimension the array by one element while preserving the existing data, add the new name from the text box to the array, and display the last name added to the array in a message box. Hint: To determine the upper boundary of the array, use the GetUpperBound(0) method. A. You are tasked with creating an application that would redimension an array, preserving its current elements, add a new element to the array, and display the new element in a message box. To create and initialize an array at the form level with just one name, you would code like this: bapp01.indd 800bapp01.indd 800 4/1/08 6:47:53 PM4/1/08 6:47:53 PM Appendix A: Exercise Solutions 801 Public Class Form1 Private strNames() As String = {“Jeannie”} To redimension the array preserving the existing data you would use code like this. Notice that you use the GetUpperBound(0) method to get the upper boundary of the array and then add 1 to it to increase the array by one element: ReDim Preserve strNames(strNames.GetUpperBound(0) + 1) To add the new name from the text box you would use code like this. Again you are using GetUpperBound(0) to determine the upper boundary of the array: strNames(strNames.GetUpperBound(0)) = txtName.Text Finally, to display the last name added to the array in a message box you would use code like this: MessageBox.Show(strNames(strNames.GetUpperBound(0)), “Exercise 2”) Chapter 6 1. Add code to the Credit Card application to display a message box containing the user ’ s state selection when they select a state in the State combo box. Hint: to access a control ’ s default event handler, double - click the control in the Forms Designer. A. This exercise requires you to create an event handler when the user makes a selection in the State combo box using the default event handler. To create this event handler, you should have double - clicked on the cboState control in the Forms Designer to create the SelectionChanged event handler. The code that you added to this event handler should resemble the highlighted code shown following. Here you display a simple message box that displays the text Selected state: and then the selected state contained in the combo box ’ s SelectedItem property. Private Sub cboState_SelectionChanged(ByVal sender As System.Object, _ ByVal e As System.Windows.Controls.SelectionChangedEventArgs) _ Handles cboState.SelectionChanged MessageBox.Show(“Selected state: “ & cboState.SelectedItem) End Sub Chapter 7 1. Create a Windows Forms application with two buttons. Add code to the MouseUp event for the first button to display a MessageBox with a message that the event has fired. Add code to the LostFocus event for the first button to also display a MessageBox with a message that the button has lost focus. bapp01.indd 801bapp01.indd 801 4/1/08 6:47:53 PM4/1/08 6:47:53 PM Appendix A: Exercise Solutions 802 A. For this exercise, you are required to create a Windows Forms application with two button controls. You were to wire up the MouseUp and LostFocus events for the first button. The code for the MouseUp event should look similar to this: Private Sub btnMouseEvents_MouseUp(ByVal sender As Object, _ ByVal e As System.Windows.Forms.MouseEventArgs) _ Handles btnMouseEvents.MouseUp ‘Display a MessageBox MessageBox.Show(“The MouseUp event has been fired.”, “Exercise 1”) End Sub And the code for the LostFocus event should look similar to this: Private Sub btnMouseEvents_LostFocus(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles btnMouseEvents.LostFocus ‘Display a MessageBox MessageBox.Show(“Mouse Events button has lost focus.”, “Exercise 1”) End Sub When you ran this application, you may have noticed some unexpected behavior when you clicked the first button. As soon as you let the mouse button up, you saw the message box indicating that the button had lost focus, and then immediately after that, you saw the message box indicating that the MouseUp event had been fired. What has actually happened here is that the code in the MouseUp event was fired, but the code in that event causes a message box to be displayed. In the course of seeing that code, Visual Basic 2008 has determined that the Button control will lose focus and has fired the LostFocus event, which displays the message box in that event handler first. 2. Create a Windows Forms application with a toolbar and status bar. Right - click the ToolStrip control and select the Insert Standard Items menu item from the context menu to have the standard buttons added to the control. For the Click event for each of the ToolStripButton controls, display a message in the status bar indicating which button was clicked. A. This exercise tasks you with creating an application that has a toolbar and status bar. You were to insert the standard buttons for the toolbar, create event handlers for the Click event of each button, and display a message in the status bar when any of the buttons was clicked. Here is the code for the event handlers: Private Sub NewToolStripButton_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles NewToolStripButton.Click ‘Update the status bar sslStatus.Text = “The New button was clicked.” End Sub Private Sub OpenToolStripButton_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles OpenToolStripButton.Click ‘Update the status bar sslStatus.Text = “The Open button was clicked.” bapp01.indd 802bapp01.indd 802 4/1/08 6:47:54 PM4/1/08 6:47:54 PM Appendix A: Exercise Solutions 803 End Sub Private Sub SaveToolStripButton_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles SaveToolStripButton.Click ‘Update the status bar sslStatus.Text = “The Save button was clicked.” End Sub Private Sub PrintToolStripButton_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles PrintToolStripButton.Click ‘Update the status bar sslStatus.Text = “The Print button was clicked.” End Sub Private Sub CutToolStripButton_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles CutToolStripButton.Click ‘Update the status bar sslStatus.Text = “The Cut button was clicked.” End Sub Private Sub CopyToolStripButton_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles CopyToolStripButton.Click ‘Update the status bar sslStatus.Text = “The Copy button was clicked.” End Sub Private Sub PasteToolStripButton_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles PasteToolStripButton.Click ‘Update the status bar sslStatus.Text = “The Paste button was clicked.” End Sub Private Sub HelpToolStripButton_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles HelpToolStripButton.Click ‘Update the status bar sslStatus.Text = “The Help button was clicked.” End Sub Chapter 8 1. Create a simple Windows application with a TextBox control and two Button controls. Set the buttons to open a file and to save a file. Use the OpenFileDialog class (not the control) and the SaveFileDialog class to open and save your files. Hint: To use the corresponding classes for the controls use the following statements: Dim objOpenFileDialog As New OpenFileDialog Dim objSaveFileDialog As New SaveFileDialog bapp01.indd 803bapp01.indd 803 4/1/08 6:47:54 PM4/1/08 6:47:54 PM Appendix A: Exercise Solutions 804 A. The exercise requires you to create a simple application that uses the OpenFileDialog and SaveFileDialog classes. The code for the Open button starts by declaring an object using the OpenFileDialog class: Private Sub btnOpen_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnOpen.Click ‘Declare a OpenFileDialog object Dim objOpenFileDialog As New OpenFileDialog The bulk of the code to display the contents of the file in your text box remains the same as the code in the Dialogs project but uses the objOpenFileDialog object versus the OpenFileDialog control: ‘Set the Open dialog properties With objOpenFileDialog .Filter = “Text Documents (*.txt)|*.txt|All Files (*.*)|*.*” .FilterIndex = 1 .Title = “Exercise 1 Open File Dialog” End With ‘Show the Open dialog and if the user clicks the Open button, ‘load the file If objOpenFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then Try ‘Save the file path and name strFileName = objOpenFileDialog.FileName Dim fileContents As String fileContents = My.Computer.FileSystem.ReadAllText(strFileName) ‘Display the file contents in the text box txtFile.Text = fileContents Catch ex As Exception MessageBox.Show(ex.Message, My.Application.Info.Title, _ MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End If Since you are using an object, you need to perform the necessary cleanup to have the object you created release its resources. You do this by calling the Dispose method on your object, and then you release your reference to the object by setting it to Nothing : ‘Clean up objOpenFileDialog.Dispose() objOpenFileDialog = Nothing End Sub bapp01.indd 804bapp01.indd 804 4/1/08 6:47:54 PM4/1/08 6:47:54 PM [...]... first screen shown When you run the installation, you see the splash screen as the first dialog box 826 bapp01.indd 826 4/1/08 6:48 :10 PM B Using the Microsoft Solutions Framework So here you are, ready to go out into the world and build applications with Visual Basic 2008 Congratulate yourself; you should be excited at having worked your way through all the chapters of the book Soon, creating applications... begin to explore this tool To get more info online, you can visit www .microsoft. com/technet/solutionaccelerators/msf/ default.mspx bapp02.indd 827 4/1/08 6:48:35 PM Appendix B: Using the Microsoft Solutions Framework Software Development Life Cycle The software development life cycle (SDLC) is a set of building blocks for software design Microsoft and others in the industry continue to develop methodologies... is shown Since this is a Boolean variable, you also provide a default value of True so that when the control is dragged onto a form, the SuppressMsgBox property will have a default value set 810 bapp01.indd 810 4/1/08 6:47:57 PM Appendix A: Exercise Solutions Public Class MyNamespace ‘Private members Private strApplicationName As String = String.Empty Private blnSuppressMsgBox As Boolean = True Next,... in the condition handler A To complete Exercise 2 you need to add the following controls to the project, set the properties as shown, and add the following code Figures A-7, A-8, A-9, and A -10 show you what the visual designer and output will look like For later projects, remember that the while activity allows only one activity to be executed during the loop When using this activity, it is common... computer If it exists, the computer should play that move rather than a random move You can add other procedures if needed A This exercise has numerous correct answers If you ask 10 programmers to complete it, you will get 10 different answers So, if your changes work, you have a valid answer The following is what we came up with to solve the problem You need to add a call to the new function, ComputerPlayToWin,... Winner(“0”) Return True End If If CheckForWin(btn10, btn11, btn12, ‘Winner on middle Row Call Winner(“O”) Return True End If If CheckForWin(btn20, btn21, btn22, ‘Winner on third Row Boolean “O”, “X”) Then “O”, “X”) Then “O”, “X”) Then 824 bapp01.indd 824 4/1/08 6:48:09 PM Appendix A: Exercise Solutions Call Winner(“O”) Return True End If If CheckForWin(btn00, btn10, btn20, “O”, “X”) Then ‘Winner on first... calling the DisplaySportsCarDetails procedure A After you add the Implements statement highlighted as follows and press Enter, the rest of the following code shown it is automatically inserted by Visual Studio 2008 to handle disposing of your class Namespace CarPerformance Public Class Car Implements IDisposable Private disposedValue As Boolean = False ‘ To detect redundant calls ‘ IDisposable Protected... takes to create a successful solution Let’s start with a basic question How is a solution different from an application? A solution is the entire process of creating a system for a customer The solution includes planning, documenting, testing, releasing, training, and supporting the application The application is just one part of the solution Microsoft has a set of processes and models that to some... (managed objects) End If ‘ TODO: free your own state (unmanaged objects) ‘ TODO: set large fields to null End If Me.disposedValue = True End Sub #Region “ IDisposable Support “ ‘ This code added by Visual Basic to correctly implement the disposable pattern 808 bapp01.indd 808 4/1/08 6:47:56 PM Appendix A: Exercise Solutions Public Sub Dispose() Implements IDisposable.Dispose ‘ Do not change this code... for further design work, more specific requirements, or a clearer definition of the problem is discovered during development or testing, and the process loops back to the earlier stage Microsoft Solutions Framework The Microsoft Framework Solution is built for the implementation of large software projects Two distinct models (Team Model and Process Model) define the entire framework To set up a large . that discuss both Visual Basic .NET and Visual Basic 2008. These sites give everything from news on moving from Visual Basic .NET to Visual Basic 2008, to listings of up and coming conferences. more! Visual Basic 2008 Programmer ’ s Reference (Wrox Press, 978 - 0 - 470 - 18262 - 8) Visual Basic 2008 Programmer ’ s Reference is a language tutorial and a reference guide to the 2008. build on the Visual Basic 2008 knowledge gained here. These will help you to specialize in particular areas. Here are the details of a couple of key titles. Professional Visual Basic 2008 (Wrox