Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 57 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
57
Dung lượng
1,57 MB
Nội dung
428 Part III Designing the User Interface The PrintPreviewDialog control displays a custom Print Preview dialog box. The PageSetupDialog control displays a custom Page Setup dialog box. As with other dialog boxes, you can add these printing controls to your form by using the Toolbox, or you can create them programmatically. In the following exercise, you’ll add Print Preview and Page Setup dialog boxes to the Print File program you’ve been working with. In the completed practice fi les, I’ve named this project Print Dialogs so that you can distinguish the code of the two projects, but you can add the dialog box features directly to the Print File project if you want. Add PrintPreviewDialog and PageSetupDialog controls 1. If you didn’t complete the previous exercise, open the Print File project from the c:\vb08sbs\chap17\print fi le folder. The Print File project is the starting point for this project. 2. Display the form, and then use the Button control to add two additional buttons to the top of the form. 3. Double-click the PrintPreviewDialog control on the Printing tab of the Toolbox. A print preview dialog object is added to the component tray. 4. Double-click the PageSetupDialog control on the Printing tab of the Toolbox. A page setup dialog object is added to the component tray. If the objects in the component tray obscure one another, you can drag them to a better (more visible) location, or you can right-click the component tray and select Line Up Icons. 5. Set the following properties for the button objects on the form: Object Property Setting Button1 Name Enabled Text btnSetup False “Page Setup” Button2 Name Enabled Text btnPreview False “Print Preview” Your form looks like this: Ob j ect Propert y S ettin g Chapter 17 Working with Printers 429 6. Double-click the Page Setup button (btnSetup) to display the btnSetup_Click event procedure in the Code Editor. 7. Type the following program code: Try 'Load page settings and display page setup dialog box PageSetupDialog1.PageSettings = PrintPageSettings PageSetupDialog1.ShowDialog() Catch ex As Exception 'Display error message MessageBox.Show(ex.Message) End Try The code for creating a Page Setup dialog box in this program is quite simple because the PrintPageSettings variable has already been defi ned at the top of the form. This variable holds the current page defi nition information, and when it’s assigned to the PageSettings property of the PageSetupDialog1 object, the ShowDialog method automatically loads a dialog box that allows the user to modify what the program has selected as the default page orientation, margins, and so on. The Try Catch error handler simply handles any er- rors that might occur when the ShowDialog method is used. 8. Display the form again, and then double-click the Print Preview button (btnPreview) to display the btnPreview_Click event procedure. 9. Type the following program code: 430 Part III Designing the User Interface Try 'Specify current page settings PrintDocument1.DefaultPageSettings = PrintPageSettings 'Specify document for print preview dialog box and show StringToPrint = RichTextBox1.Text PrintPreviewDialog1.Document = PrintDocument1 PrintPreviewDialog1.ShowDialog() Catch ex As Exception 'Display error message MessageBox.Show(ex.Message) End Try In a similar way, the btnPreview_Click event procedure assigns the PrintPageSettings variable to the DefaultPageSettings property of the PrintDocument1 object, and then it copies the text in the rich text box object to the StringToPrint variable and opens the Print Preview dialog box. Print Preview automatically uses the page settings data to display a visual representation of the document as it will be printed—you don’t need to display this information manually. Now you’ll make a slight modifi cation to the program code in the btnOpen_Click event procedure. 10. Scroll up to the btnOpen_Click event procedure in the Code Editor. This is the procedure that displays the Open dialog box, opens a text fi le, and enables the printing buttons. Because you just added the Page Setup and Print Preview buttons, you have to add program code to enable those two printing buttons as well. 11. Scroll to the bottom of the event procedure, just before the fi nal Catch code block, and locate the following program statement: btnPrint.Enabled = True 12. Below that statement, add the following lines of code: btnSetup.Enabled = True btnPreview.Enabled = True Now your program will enable the print buttons when there’s a document available to print. 13. Click the Save All button on the toolbar to save your changes. Chapter 17 Working with Printers 431 Test the Page Setup and Print Preview features Tip The complete Print Dialogs program is located in the c:\vb08sbs\chap17\print dialogs folder. 1. Click the Start Debugging button on the toolbar. The program opens, with only the fi rst button object enabled. 2. Click the Open button, and then open the longfi le.txt fi le in the c:\vb08sbs\chap17 folder. The remaining three button objects are now enabled, as shown here: 3. Click the Page Setup button. Your program displays the Page Setup dialog box, as shown here: 432 Part III Designing the User Interface Page Setup provides numerous useful options, including the ability to change the paper size and source, the orientation of the printing (Portrait or Landscape), and the page margins (Left, Right, Top, and Bottom). 4. Change the Left margin to 2, and then click OK. The left margin will now be 2 inches. 5. Click the Print Preview button. Your program displays the Print Preview dialog box, as shown in the following illustration: One page Two pages Three pages Four pages Six pages Print Zoom Page Select box If you’ve used the Print Preview command in Microsoft Offi ce Word or Microsoft Offi ce Excel, you will recognize several of the buttons and preview features in this Print Preview dialog box. The Zoom, One Page, Two Pages, Three Pages, Four Pages, Six Pages, and Page Select box controls all work automatically in the dialog box. No program code is required to make them operate. 6. Click the Four Pages button to display your document four pages at a time. Chapter 17 Working with Printers 433 7. Click the Maximize button on the Print Preview title bar to make the window full size. 8. Click the Zoom arrow, and then click 150%. Your screen looks like this: 9. Click the Zoom button and return the view to Auto. 10. Click the Three Pages button, and then click the Up arrow in the Page Select box to view pages 2 through 4. As you can see, this Print Preview window is quite impressive—and you incorporated it into your program with just a few lines of code! 11. If you want to test printing the entire document again, click the Print button. 12. When you’re fi nished experimenting, click the Close button to close the Print Preview dialog box, and then click the Close button to close the program. You’re done working with printers for now. 434 Part III Designing the User Interface Chapter 17 Quick Reference To Do this Make it easier to refer- ence the printing classes in your projects Add the following Imports statement to the top of your form: Imports System.Drawing.Printing Create a printing event handler Double-click the PrintDocument1 object in the component tray or Use the AddHandler statement and the AddressOf operator. For example: AddHandler PrintDocument1.PrintPage, _ AddressOf Me.PrintGraphic Create a PrintDocument object in your project Double-click the PrintDocument control on the Printing tab of the Toolbox. or Include the following variable declaration in your program code: Dim PrintDoc As New PrintDocument Print graphics from a printing event handler Use the Graphics.DrawImage method. For example: ev.Graphics.DrawImage(Image.FromFile _ (TextBox1.Text), ev.Graphics.VisibleClipBounds) Print text from a printing event handler Use the Graphics.DrawString method in an event handler. For example: ev.Graphics.DrawString(TextBox1.Text, _ New Font("Arial", 11, FontStyle.Regular), _ Brushes.Black, 120, 120) Call a printing event handler Use the Print method of an object of type PrintDocument. For example: PrintDoc.Print() Print multipage text documents Write a handler for the PrintPage event, which receives an argument of the type PrintPageEventArgs. Compute the rectangular area on the page for the text, use the MeasureString method to determine how much text will fi t on the current page, and use the DrawString method to print the text on the page. If additional pages are needed, set the HasMorePages prop- erty to True. When all text has been printed, set HasMorePages to False. Open a text fi le by using the FileStream class, and load it into a RichTextBox object Create a variable of type FileStream, specifying the path and fi le mode, load the stream into a RichTextBox, and then close the stream. For ex- ample: Imports System.IO 'at the top of the form Dim MyFileStream As New FileStream( _ FilePath, FileMode.Open) RichTextBox1.LoadFile(MyFileStream, _ RichTextBoxStreamType.PlainText) MyFileStream.Close() Display printing dialog boxes in your programs Use the PrintDialog, PrintPreviewDialog, and PageSetupDialog controls on the Printing tab of the Toolbox. T o D o th i s Microsoft Visual Basic 2008 Step by Step 435 Part IV Database and Web Programming In this part: Chapter 18, Getting Started with ADO.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437 Chapter 19, Data Presentation Using the DataGridView Control. . . . . . . . . . . . . 465 Chapter 20, Creating Web Sites and Web Pages by Using Visual Web Developer and ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . . 489 In Part IV, you’ll learn how to work with information stored in databases and Web sites. First, you’ll learn about Microsoft ADO.NET, an important paradigm for working with database information, and you’ll learn how to display, modify, and search for database content by using a combination of program code and Windows Forms controls. Microsoft Visual Studio 2008 was specifi cally designed to create applications that provide access to a rich variety of data sources. These custom interfaces have traditionally been called database front ends, meaning that through your Microsoft Visual Basic application, the user is given a more useful window into database information than simply manipulating raw database records. However, a more appropriate description in Visual Studio 2008 is that you can build datacentric applications, meaning that through your application, the user is invited to explore the full potential of any number of rich data source connections, whether to local or remote locations, and that the application places this data at the center of the user‘s computing experience. [...]... technologies in Visual Studio 2008 that will be of considerable use to experienced database programmers These technologies are Language-Integrated Query (LINQ) and the ADO.NET Entity Framework LINQ is included with Visual Studio 2008 and offers the capability to write object-oriented database queries directly within Visual Basic code Some time after the initial release of Visual Studio 2008, Microsoft also... Part IV Database and Web Programming You can use Visual Studio 2008 to create new databases, but Visual Studio 2008 is primarily designed for displaying, analyzing, and manipulating the information in existing databases ADO.NET, first introduced in Microsoft Visual Studio NET 2002, is still the standard data model for database programming in Visual Studio 2008 ADO.NET has been improved over the years to... example, it uses the same basic method for accessing local, client-server, and Internet-based data sources, and the internal data format of ADO.NET is XML Fortunately, most of the database applications that programmers created using Visual Basic 2005 and ADO.NET still function very well, and the basic techniques for accessing a database are mostly the same in Visual Basic 2008 However, there are two... we’ll be starting at the beginning, and with database programming more than almost any other subject, you really need to be exposed to topics step by step Let’s start by understanding some basic database terminology Chapter 18 Getting Started with ADO.NET 4 39 A field (also called a column) is a category of information stored in a database Typical fields in a customer database might contain customer... organizing database information By chaining together a group of these statements, programmers can create complex search directives, or queries, that extract just the data that is needed from a database Realizing the industry-wide acceptance of SQL statements, previous versions of the Visual Studio and Visual Basic IDEs have included mechanisms for using SQL statements Visual Studio 2008 features an exciting... following: The connection string identifies a provider (also called a managed provider) named Microsoft. Jet.OLEDB.4.0, which is an underlying database component that understands how to connect to a database and extract data from it The two most popular providers offered by Visual Studio are Microsoft Jet OLE DB and Microsoft SQL Server, but thirdparty providers are available for many of the other popular... advantageous because they enable the Microsoft IntelliSense feature of the Visual Studio Code Editor, and they give you specific information about the fields and tables you’re using 19 Click the schema file in Solution Explorer, and then click the View Designer button You see a visual representation of the tables, fields, and data adapter commands related to your new dataset in a visual tool called the Dataset... automatically by the Data Sources window, but we need to add this one manually 6 Adjust the spacing between the two labels and text boxes so that they are aligned consistently When you’re finished, your form looks similar to the following: Now you’ll bind the PhoneNumber field in StudentsDataSet to the new masked text box object In Visual Studio 2005 and 2008, the process is easier than it was in Visual Basic. .. DataGridView control discussed in Chapter 19, take a moment to see how you can further customize your dataset by using a few SQL statements Chapter 18 Getting Started with ADO.NET 4 59 One Step Further: SQL Statements, LINQ, and Filtering Data You have used the Data Source Configuration Wizard to extract just the tables and fields you wanted from the Students database by creating a custom dataset named StudentsDataSet... creating a custom dataset named StudentsDataSet In addition to this filtering, however, you can further organize and fine-tune the data displayed by bound controls by using SQL statements and the Visual Studio Query Builder This section introduces these tools For Visual Basic users who are familiar with Access or SQL Server, filtering data with SQL statements is nothing new But the rest of us need to learn . PageSetupDialog controls on the Printing tab of the Toolbox. T o D o th i s Microsoft Visual Basic 2008 Step by Step 435 Part IV Database and Web Programming In this part: Chapter 18, Getting. with Visual Studio 2008 and offers the capability to write object-oriented database queries directly within Visual Basic code. Some time after the initial release of Visual Studio 2008, Microsoft. programmers created using Visual Basic 2005 and ADO.NET still function very well, and the basic techniques for accessing a database are mostly the same in Visual Basic 2008. However, there are