Using a StreamWriter to Save Control Values to a File
To get started with saving the Text values of controls, add two buttons to the form, called btnWriteControls and btnReadControls. Next, add a bunch of TextBoxes to the form. These will be used for writing and retrieving values. It really doesn't matter what they are called or what values they show initially, so you can accept the default names and values TextBox1, TextBox2, and so on . It also doesn't matter how many or few there are, because the StreamWriter object, using the code I'll show...
Using XMLSerializer
Under the hood, VB .NET and XML work hand and glove together. This can be seen in a quite simple example using an XMLSerializer object to serialize an instance of a class to a StreamReader which can save it as file . The XMLSerializer can also be used in a reverse fashion to convert or deserialize an I O stream, pulled from a file with a StreamWriter object, back to an instance of a class, making the members of the object programmatically available. For more information about working with...
Working with the ErrorProvider
One problem with the code in the ListBox example is that the user input has not been validated. The user might try to add an item without supplying text for the item. This would produce an item with blank text somewhat silly, perhaps, but not tragic. But the index values for the items collection present a somewhat worse problem. A user could try to enter non-numeric text in the index field. Or, the user could enter an index value that was out of range for the collection. Either case would...
Adding ClientSide Scripting
You may also be interested to know that the HTML tab of the designer provides a framework via the Objects and Procedures lists for adding client-side scripting, using JavaScript. For example, try selecting Window from the objects shown under Client Objects amp Events drop-down list in the upper-left side of the designer. Next, choose Onclick from the drop-down list of events in the Procedures list. -. - . 1 Lfl - ki. h lua LI hill I ,1 L ILLl r rci -nftnit-llmTOlii 74 wt iN rrii-rnimicc jgrt m...
Using Predefined Color Values
In VB6, color values were stored as long integers specifically, they were typed as Long . In other words, every color that you could use was reduced to a numerical value. In contrast, in VB .NET, colors are stored as two new types System.Drawing.Color and System.Drawing.SystemColors. Both types return a color structure that contains the color value as a 32-bit number. You can use the members of the System.Drawing.Color and System.Drawing.SystemColors classes as predefined color values in your...
Setting the BackColor
Various color properties can be set to change the color of elements of a form. For example, the BackColor property sets the background color used in a form. By default, it is set to a color determined by the Windows System color scheme, as shown here. j Tin hrfKjftfH C M jlnil ID 41 4n It Ift 11 ir A GMMiil. You can also set BackColor to a color of your choice using the Custom palette Another option is to set BackColor to a color that is designed to display well on the Web well, actually in...
Throwing Exceptions
Throwing an exception means creating your own exception under certain conditions. It is comparable to raising an error in VB6. Throwing custom exceptions should not be done to facilitate normal program communication, but it may make sense as part of a scheme for dealing with logical errors, and it is what your application should do in many circumstances if it asked to do something impossible. To throw an exception, use the Throw keyword with a new instantiation of the Exception class, or one of...
Reading XML with XMLTextReader
The counterpart to the XMLTextWriter class is the XMLTextReader class. Realize that this is by no means the only class available in .NET to read or parse XML files XMLNodeReader and XMLValidatingReader are also powerful classes. Much of the time, you will know a great deal about the formatting of the XML file you want to read. That's certainly the case with the example that I'll show you, in which we know that the root element is named lt UserChoice gt and is followed by elements and values...
Finishing the Web Setup Project
After you've run the Setup Wizard to create your Web Setup project, you can then add the project output to the Setup project. Select the root node of the Setup project, right-click, and select Add Project Output. In the Add Project Output dialog, make sure Primary Output is selected, as shown in Figure 20.10, and click OK. igure 20.10 The Add Project Output Group dialog is used to add the output of the Web Setup project to the solution. Next, make changes to the Web Setup project settings using...
Creating a Web Service
In theory, a sample web service could do anything you can do in a class module, from the trivial to the very useful. Our web service will determine if the number input is a prime not evenly divisible by another number except one or itself . First, use the New Project dialog to create an ASP.NET Web Service project named IsPrime, as shown in Figure 19.10. amp gt Web A3 - per tffb Web Cortrd ApficflfcOT SenrtM Lbr-wr I Hit rJii.iVlniil pumu J I- it iep jiKitim ljfvii , 'igure 19.10 Web services...
Setting the Tab Order
It's important that all applications have an easy way to navigate that does not require the mouse. Generally, this is done using keyboard shortcuts that open menus see Chapter 11, Creating Menus and with the Tab and arrow keys. When you create an application, you set the tab order, so that users who prefer to use the Tab key are taken through the controls on a form in the proper order. VB .NET provides an easy and nifty way to set the tab order. But before we get to it, you should understand...
Monitoring Form Closing Events
Does closing sound more tentative to you than closed In the brave new world of events, it is indeed. The Closing event gives the programmer a chance to allow the user to change her mind see the Unloading Forms section later in this chapter , while the Closed event is the real thing the form is gone. As this implies, Closed comes after Closing in the sequence, with Deactivate in between. You can add calls to the Debug.WriteLine method to observe this, as shown in Listing 5.4. Listing 5.4...
Manipulating Menus in Code
One of the easiest ways to manipulate menus in response to application state is to add a second MainMenu control to the form and toggle between the two menu structures. To see an example of this in action, add a Toggle Menus menu item to the original MainMenu structure we've created. Next, add a new MainMenu control to the form. As you can see, the menu items that make up this menu structure are pretty fantastic, so let's give the menu item that toggles back to the first menu the text Back to...
Appendix C Key Syntax Differences Between VB6 and VB NET
There are a great many differences between VB6 and VB .NET. In some ways, the most important changes are conceptual, rather than having to do with syntax or language. To understand these changes, a good starting place is to read this book. Another useful reference is the Introduction to Visual Basic .NET for Visual Basic Veterans Help topic and the links within that topic. See Appendix A, Using VB .NET's Help System. This appendix lists most of the changed programming elements and their...







