1. Trang chủ
  2. » Công Nghệ Thông Tin

Visual Basic .NET at Work Building 10 Enterprise Projects phần 2 doc

52 303 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 52
Dung lượng 0,93 MB

Nội dung

Now you can add any forms you like based on the forms you just loaded. As an example, we will create a form based on the frmBase form. Right-click on the project name in the Project view and select Add/Add Inherited Form. Type in a name for the form and then click Open. On the next window, as in Figure 1.11, select the form from which you want to inherit. Select the frmBase form and click OK. Now you have an inherited form in your program. Even though you haven’t done anything to it yet, you’ll see that the form has all the controls from the base form already in place. One important point to note here is that the controls from the base form are locked and cannot be edited in the Form Designer. You’ll have to do it through properties or in code. Creating a Class Library The better way to incorporate these forms into your program is to build a class library that contains them. Didn’t know you could do that, did you? WinForms are just classes like anything else now. We can place them in a DLL and use them in other programs. Here’s the easy way to do it. Create a new Class Library project in Visual Studio. Name it prj01classes. When the project creation is complete, delete the default class, Class1, from the project and add the form files to the project. Right-click on the solution name in the solution explorer, and from the context menu, select Add Existing Item. Select all the form .VB and .RESX files, and add them to the project. Build the project. When you’re done, you’ll have a DLL you can add to any project. Want to try it out? Create a new WinForms project, naming it whatever you like. Right-click on the References section in Solution Explorer, and from the context menu, select Add Reference. In the dialog that appears click the Browse button and find the DLL we just created, select it, and click OK. Click OK on the original dialog. Now you can add a new inherited form to the project. When the Inherited Form dialog appears, type in a new name for the form and click Next. There will be nothing in the list, and you’ll have to browse to the DLL again. When this is done, you’ll see a list of all the forms in this project. Select the one you want, click OK, and you’re done. This is easier and more reusable than the first method. We’ll be using the class library of forms, located on the accompanying CD-ROM, for several future projects in this book. Now you’re ready to go. You can create inherited forms like we just did as often as you like, through the Form Designer or through code. The Test Driver There is a test driver project on the CD-ROM that you can use to see how all the forms operate. Load the project prj01 into Visual Basic .NET and give it a run. I’ll go over some of the code to illustrate some of the important usage points. It’s a simple test form, consisting of a set of buttons you can click to launch samples of all the form classes. Figure 1.12 shows what it looks like. 30 Project 1 Figure 1.11 Specifying a form from which to inherit. The code is not complex and is essentially just a set of event handlers, one for each button and window type. It illustrates how to use each type of form in code. Starting with the base form, you’ll see the basic procedure for creating one of these windows in code. The base form was not meant to be created directly, but you can create any form derived from it: Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim wBase As frmBase wBase = New frmBase() wBase.Show() End Sub Figure 1.12 The test driver form, frmTestDriver. A WinForms Library 31 Create a variable of the form type, in this case, frmBase. Then allocate a new instance of the form with New. Finally, show the form so that it ends up on the screen. You can follow the same procedure with frmDialog and frmWizard. Even frmApp can be cre- ated the same way. However, these forms are really designed be used as the basis for new forms that you derive from them. The forms frmMsgBox and frmAbout, however, are meant to be used from code. They are forms of convenience, as illustrated by the following code, which shows how to use the frmMsgBox without showing the box immediately. In this example, we create the message box first and then make an adjustment to the height. It is then displayed using the Show method: Private Sub Button2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button2.Click Dim wMsgBox As New frmMsgBox() wMsgBox = New frmMsgBox(frmMsgBox.MsgType.msgtypeERROR, _ "What were you thinking?", _ "Perhaps you had better take back what you just did." & _ "I don't think it's in your best interest. Maybe you " & _ "should take a vacation!", _ True, False, False) wMsgBox.Height = 220 wMsgBox.Show() End Sub The following example illustrates how to use the About box form in one line of code. Note that the second to last parameter, the path to the bitmap file to display on the dia- log, will have to be changed before it works correctly. Point it to the actual location of the bitmap file on your computer: Private Sub Button7_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button7.Click Dim wAbout As New frmAbout("Sample About Box", _ "This dialog is provided so that you can tell your users " + _ "what your application does. It even includes a link to " + _ "your Web site.", _ "Copyright (c) 2001 by Vulture Corporation. All rights _ "reserved.", _ "vulturecorp.bmp", _ "http://www.vulturecorp.com") End Sub The replacement constructor we built for frmAbout (and frmMsgBox) will show the form as soon as it’s created. 32 Project 1 Enhancing the Project With a tiny bit of modification, you’ll be able to use these forms in your own applica- tions, giving them a consistent look and functionality, as well as saving yourself some time. Along the way, you will get a taste of the new .NET WinForms capabilities and using classes in Visual Basic .NET and will pick up a few advanced techniques. There are all kinds of things you can do to make this project more useful and com- plete. Following is a list of just a few ideas you might try out if you feel like digging deeper: Be less error-prone. Add some error handling to the forms to allow them to pro- tect themselves from bad inputs. By this, I mean complete, robust, structured error handling. Visual Basic .NET has a completely new error-handling mecha- nism using the Try Catch Throw standard. Now you can more reliably build error traps and throw your own errors very easily. Make it MDI. Create your own version of frmApp that acts as an MDI container. You’ll be able to work with all kinds of windows and documents. Create some wizardly magic. Write some code in frmWizard to manage multiple control panels, making wizard creation easier. Start with multiple group con- trols, and hide or display them as the user clicks next or previous buttons. Autosizing messages. Add code to the frmMsgBox form to automatically size itself based on the size of the message content. Right now it just overflows, and longer text might get clipped. You’ll have to figure out how tall to make the form and stretch the size of the lblMsg control. More buttons. Update the code in frmMsgBox to handle more button options. Right now it only supports OK. It probably needs, like VB’s MsgBox, the capa- bility of displaying various additional buttons, such as Yes, No, and Cancel. You could add another enumerated type to the code to specify the button types available, and Intellisense will pick them up and display them for the program- mer. You could even override the ShowControls method from frmBase, like we did in frmApp, to handle the display of the various buttons. WHAT’S COMING NEXT If you’re interested in databases, get ready to be excited. If you don’t like databases, you’ll be happy, too. We’re covering ADO.NET next, and database people will really appreciate some of its new capabilities. Antidatabase people will be happy because it makes database work easier and more flexible than the old ADO did. When a programmer like me who has spent his life avoiding database coding enjoys it, you know it must be good. A WinForms Library 33 [...]... with ADO.NET DataSet DataRelation Collection DataTable Collection DataTable 1 DataTable n DataRow Collection DataColumn Collection Constraint Collection Figure 2. 2 The contents of the DataSet object Within the DataSet, there is no concept of current record, next record, last record, or EOF You simply access the data you want directly; it’s all there for the taking However, if you’re using data binding,... shown in Figure 2. 12 It could not generate a Select statement for the SQL that it generated itself This seems a little odd, and perhaps it will be upgraded in the future Figure 2 .10 The DataSet creation dialog 53 54 Project 2 Figure 2. 11 The Query Builder with a JOIN Figure 2. 12 The Select generation warning message Bug Tracking with ADO.NET This technique is not useless for more complicated queries... to work with a generated framework that might compromise your own design needs s s The wizard’s data access and manipulation capabilities are limited Bug Tracking with ADO.NET Figure 2. 3 The wizard-generated data form You can also use some of the built-in data wizards and tools at a lower level For example, there is a wizard to create a DataAdapter for you and the code to support it From that DataAdapter,... Figure 2. 8 and reflects the same query we entered as SQL Figure 2. 7 The DataAdapter Wizard: Create SQL statements 51 52 Project 2 Figure 2. 8 The DataAdapter Wizard: The query builder Figure 2. 9 The DataAdapter Wizard: Creation results Bug Tracking with ADO.NET The next wizard panel requires no interaction It simply informs us that everything was created successfully The really interesting part is that... creating one DataSet with correct, validated data that contains all the edits you made to the data We start with editing the data once we have it Primarily the objects contained in the DataSet provide this functionality, such as the Rows and Columns collections The basic operations and the methods used are detailed in Table 2. 2 Table 2. 2 Dealing with Changes to Data Using ADO.NET Objects OPERATION METHOD... associate parts with it The DataRelation can help with this Let’s look at how we can make this example happen using the DataRelation and other ADO.NET objects First you have to set up the DataRelation This involves linking the tables with the two matching columns in the tables In our case, we have a ComputerID field that exists in both tables to link the records together Start by creating a DataRelation... appears, select DataForm wizard and click OK This will launch a wizard that will ask you to specify a data connection to a database and, from it, a form will be generated that has fields and navigation functionality based on that database.Table 2. 3 illustrates the settings you should choose in each panel of the wizard Team-Fly® 47 48 Project 2 Table 2. 3 Settings for Each Wizard Panel in the Data Form Wizard... Project 2 1 Create the basic objects first: an instance of the Connection, a DataSet, a DataAdapter, and a Command object 2 Give your Connection object a connection string and open it You can stuff the connection object into the DataAdapter 3 Then add a select statement to the Command object so that it knows what data to pull out Add the command object to the DataAdapter 4 Load the data by calling the DataAdapter’s... the data in a disconnected state It goes something like this: 1 Once you have your DataSet populated, make any changes you like to the data using the collections and methods in the DataSet 2 When you’re ready to update the data, create a second DataSet using the GetChanges method This will return only the records that have changed since the DataSet was populated This second DataSet is more optimized to... Once you are connected, you can start working with the database To do this, you need a Command object to tell the database what you want to do, a DataSet to hold the returned data, and a DataAdapter to do the work for you Create them like you would any other object: Private objCommand As New OleDbCommand() Private objDS As New DataSet() Private objDA As New OleDbDataAdapter() Continue by adding your . _ DataSet DataRelation Collection DataTable Collection DataTable 1 DataTable n DataRow Collection DataColumn Collection Constraint Collection Bug Tracking with ADO .NET 43 "'Rotten Tomatoes, Inc.'. Select, Update, Insert, and Delete com- mands so that it can perform data operations. It is responsible for loading the DataSet with data and updating the database from changes made to the DataSet. This. database so that the DataAdapter can communi- cate with it. The Command. The Command object allows the DataAdapter to issue com- mands to the database in order to operate on the data. The DataAdapter

Ngày đăng: 14/08/2014, 01:20

TỪ KHÓA LIÊN QUAN

w