Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Manning - Windows Forms Programming With CSharp.pdf
Скачиваний:
72
Добавлен:
24.05.2014
Размер:
14.98 Mб
Скачать

If you compile and run this application, make sure you note how the form resizes. In particular, notice how we set the Anchor properties in this example to maximize the area allocated for the image and notes controls.

Our final topic is to ensure that any changes we make to the controls in the Photo tab are reflected in the album file.

17.4.5SAVING CHANGES TO BOUND CONTROLS

Changes made to bound controls are saved much like we saw for the DataGrid control earlier in the chapter. When an object supports the IEditableObject interface, the BeginEdit method is called whenever a bound property is assigned a new value, and the EndEdit method is called when the user is done making changes to the current item.

If you experiment with our interface, you will find that the program usually but not always offers to save the most recent changes. The problem occurs when you edit a couple of values for a Photograph and then click the Close button. In this case, the EndEdit call is never made since the framework believes the edit is still active for the displayed item. As a result, the PhotoAlbum.HasEdits property will return the value false, and the changes are not saved.

We can fix this by forcing the current edit to end when we exit the program.

FINISH ANY ACTIVE EDIT WHEN THE PROGRAM EXITS

 

Action

Result

 

 

 

1

Locate the OnClosing override

protected override void OnClosing

 

in the MainForm.cs code

(CancelEventArgs e)

 

window.

{

 

 

 

 

 

2

Retrieve the binding manager for

// Complete any in-progress edits

 

the _album data source.

BindingManagerBase bm

 

 

= BindingContext[_album];

 

 

 

3

Call the EndCurrentEdit

if (bm != null)

 

method to complete any

bm.EndCurrentEdit();

 

outstanding edits.

SaveChanges();

 

 

 

 

base.OnClosing(e);

 

 

}

 

 

 

This change ensures that any modifications made to the current item are taken into account when the application is closed. Note that there is also an EndEdit method in the DataGrid class, which performs similar functionality on the data grid control. Our approach is more general, and applies to all edits on any control related to the given binding manager.

Compile and run the application and make sure this works correctly. Figure 17.8 shows the application with the message dialog for saving a change displayed.

SIMPLE DATA BINDING

601

Figure 17.8 The Save Changes dialog permits the user to save any changes made to the individual controls.

This completes our discussion on data binding. We end with the usual summary of our accomplishments.

17.5RECAP

In this chapter we investigated the concept of data binding and constructed a new MyAlbumData application. We began with the DataGrid class and saw how to create and fill a data grid, and how to customize the contents and appearance of the grid for a specific type of data.

We then looked at the IEditableObject interface as a way to support transactional updates to bound data. We implemented this interface in our Photograph class in order to track and save any changes made by a user.

Binding to data grids is referred to as complex data binding. We also examined simple data binding, used to bind a single property of a control to a value in a data source. We created a TabPage object in our application to hold a set of controls related to a Photograph object, and bound properties of our controls to the active Photograph in a PhotoAlbum collection. We finished our chapter by examining the update and storage of data sources as the user interacts with the application in general and the bound controls in particular.

While the examples here did not use the System.Data namespace, the binding of data grids and controls to database objects was discussed along the way in order to provide some insight into how such binding might be performed.

602

CHAPTER 17 DATA BINDING

C H A P T E R

1 8

 

 

Odds and ends .NET

18.1Printing 604

18.2Timers 611

18.3Drag and drop 618

18.4ActiveX controls 625

18.5Recap 635

In this last chapter of the book, it seems appropriate to mention a number of different topics worthy of further exploration. This chapter presents various concepts that might be of interest to you as you build and deploy Windows Forms applications. Since the details of each topic could easily fill all or most of a chapter, we will instead show a rather quick example for each subject. These examples should point you in the right direction as you expand your knowledge of .NET in general and Windows Forms in particular.

We will take a quick look at four different topics:

Printing, including page setup and print preview.

Windows Forms Timers, including stopping and restarting a timer.

Drag and drop, both into and out of Windows Forms applications.

Hosting ActiveX controls, by way of hosting a web browser control.

For no particular reason, these topics are presented in the same order as they are listed. We begin with printing from Windows Forms applications.

603

18.1PRINTING

Printing in Windows Forms is supported by the System.Drawing.Printing namespace in addition to Windows Forms constructs. In this section we add printing support to the MyPhotos MDI application built in chapter 16. The main classes required are as follows:

Printing related classes

Class

Description

 

 

PrintDocument

A reusable component that is used to send output to the printer. The

 

PrintPage event occurs when print data should be sent to the printer

 

device.

PrintDialog

A common dialog that offers options related to printing.

PrintPreviewDialog

A form that contains a PrintPreviewControl object for presenting

 

how a document will look when it is printed on a specific printer device.

PageSetupDialog

A common dialog that permits a user to alter the page settings

 

associated with a print document.

 

 

Our example will use each of these four classes in order to support printing of an individual photograph. Figure 18.1 shows the print preview dialog for one of our images.

Figure 18.1 Note how the text here is drawn to the right of the image. Long strings, such as the Notes text, are formatted to fit within the available page margins

604

CHAPTER 18 ODDS AND ENDS .NET