Opening a child form

The Open menu in the parent form should work much like the now hidden Open menu for the MainForm class. The handler for this menu should display an Open-FileDialog and create a new child window containing the selected album. To create the MainForm instance, we will create a new constructor that accepts an album file name with which to initialize the window. The code required here is nothing new to us, so let's get to it. IMPLEMENT HANDLER FOR OPEN MENU IN PARENT FORM Add a using statement for...

Adding a toggle button

Our final toolbar button will illustrate the ToggleButton style to show and hide the pixel data dialog created in chapter 8. Toggle buttons, as you may recall from chapter 9, provide two different states one when the button is pressed in, and one when it is not. We will do something a little different here that seems appropriate for our example. If you are keeping track of the images available in our image list, you may realize there are two images left, while only one more button. We will use...

Customizing a DateTimePicker control

As can be seen in .NET Table 11.4, a custom display setting for the DateTimePicker control is used when the Format property is set to DateTimePicker- Format.Custom. The CustomFormat property contains the string value to use in this case. A number of format codes are available within this string. These codes are managed by the sealed DateTimeFormatInfo class. The following table shows a number of these codes, along with some corresponding properties in the DateTimeFormatInfo class, which can be...

DRAWING the status bar panel

Our status bar is drawn in the statusBar1_DrawItem method. This method must calculate the percentage of the image shown in the window. Since the image will now be displayed inside the Panel object, we must modify this routine to use the Panel client area rather than the MainForm one. Update the statusBar1_DrawItem method to use the panel protected void statusBarl DrawItem object sender, StatusBarDrawItemEventArgs sbdevent Update the statusBar1_DrawItem method to use the panel continued Modify...

Supporting item selection

Like the ListBox control, a list view can support single item or multiple item selection. The MultiSelect property is a boolean property that indicates which type of selection to support. We looked at multiple selection within list boxes in chapter 10, so we will stick with single selection in this chapter. The SelectedIndices property holds the collection of indices corresponding to the selected items in the list. These index values refer to the position of the items within the collection...

Using check box buttons

The CheckBox control is similar to a radio button. While a radio button is used for a set of mutually exclusive options, a check box is used for a single option that can be turned on or off. A check box normally appears as a small square followed by a textual description of the option. The settings for this control are rather similar to those provided for RadioButton objects, and are summarized in .NET Table 9.7. The CheckBox class represents a button that displays an option to the user....

Drawing a print page

With our user interface logic in place, we are ready to implement the printing of a page. This is done using the PrintPageEventArgs parameter provided to the PrintPage event handler. This class is summarized in .NET Table 18.1. .NET Table 18.1 PrintPageEventArgs class The PrintPageEventArgs class represents an event argument containing Information required for printing pages to a printer. This class is part of the system.Drawing.Printing namespace, and inherits from the system.EventArgs class....

Adding the dropdown menu

So far, we have added the main menu and inserted a File menu in it. Next we will create the dropdown menu that appears when this menu is clicked. Create a Load menu item within the File menu. Use the text amp Load a. Make sure the designer window is displayed. c. Type in amp Load below the File menu where it says Type Here The Load menu appears as the first item in the drop-down list for the File menu. Display the Properties window for the Load menu item and set the following property values...

Using the Splitter class

As a short aside to our discussion on tree views, the Splitter class is useful for dividing all or part of a form or other container into two resizable sections. While some readers may not consider a splitter control to be an advanced concept, it fits nicely into our discussion of the MyAlbumExplorer application, so this is where it goes. Typically a splitter provides separate areas for two collection or container controls, normally one of the ListBox, ListView, TreeView, or Panel controls. An...

Form class hierarchy

If you have actually read this book from the beginning, you will recall that we looked at the Menu class hierarchy in chapter 3 and the StatusBar hierarchy in chapter 4. Chapter 3 introduced some of the low-level classes used by Windows Forms, and chapter 4 extended this hierarchy to include the Control class. The Form class is based on additional extensions to enable scrolling and containment. The complete hierarchy is shown in figure 7.1. See figure 3.3 in chapter 3 for information on these...

Placing images on our buttons

We have created the buttons in our PhotoEditDlg form, and created bitmaps for these buttons within our project. The next step is to reconfigure our buttons to display these images. A button can contain both an image and text string, or only an image, or only text. For our example, we will include both the image and text on our buttons. The steps required are as follows Add the bitmap images to the buttons In the PhotoEditDlg.cs Design window, modify the Next button to display its text on the...

The Windows Forms namespace

In addition to imposing structure on the vast collection of objects supported by the .NET Framework, the namespace concept also provides some direction and focus for writing a book. This book focuses on the System.Windows.Forms namespace, affectionately known as Windows Forms- Windows Forms applications are programs that are executed by the Windows operating system, and that employ the user interface features familiar to Windows desktop users everywhere- The book attempts to provide a somewhat...

Using radio buttons

The bottom part of our dialog will contain some radio and check box buttons. We will begin with the radio buttons in the middle of the form. Radio buttons display a set of mutually exclusive options. In our case, they indicate which Photograph setting should be used to represent the photograph in a main window. Radio buttons in .NET work much the same as radio buttons in other graphical environments. An overview of this class appears in .NET Table 9.6. In our code, three radio buttons will...

The AssemblyInfo file

When you create a C Windows application project, an AssemblyInfo.cs file is created to define various attributes for the program assembly. This includes the version number shown in the Version tab when you display the program's properties dialog box from Windows Explorer. An attribute in C is a declarative tag that affects the settings or behavior exhibited by an assembly, type such as a class , or type member such as a method or property . All attributes are based on the System.Attribute class...

Disposing of resources

Our PhotoAlbum and Photograph classes are now fairly well-defined. We can create photographs from image files, add and remove photos to albums, and iterate through the contents of an album. A topic we haven't touched on is the issue of cleaning up a photo or album when we are finished. You might be wondering why we even care. Isn't this the purpose of garbage collection When we are finished with an album, the garbage collector will clean it up eventually, so we do not need to worry about it....

Updating data bound controls

With our controls bound, our next task is to properly update them as the selected album changes. We also need to hook up the Next and Prev buttons so that they display the next or previous photo from the current album. Doing this requires the BindingManagerBase class. A summary of this class appears in .NET Table 17.7. An instance of this class is created for each data source active in an application, and stored in a BindingContext instance associated with a Control object. Normally, the...

Message boxes

Sample Message Box

Developers, especially object-oriented developers, are always looking for shortcuts. Classes such as OpenFileDialog and SaveFileDialog not only provide a standard way to prompt a user for files, they also save programmers a lot of time and effort by encapsulating the required window display and interaction code. Another common task programmers face is the need to display a simple message to the user. Our photo album application, for example, should really display an error message when an album...

Handling mouse clicks in a calendar control

Our MonthCalendar control is on the form and displays the dates assigned to an album's photographs in bold. The next step is to handle clicks by the user and link them up with associated photographs. We will handle the MouseDown event for this purpose, and create a Context-Menu object on the fly to display any photos associated with the selection point. The MonthCalendar class also provides the DateChanged event that occurs whenever a valid date is clicked. We could use this event instead,...

Expanding the PhotoAlbum class

In order to support title, display name, and password settings in our albums, we need to make a few changes. For starters, let's add some variables to hold these values and properties to provide external access. Set the version number of the MyPhotoAlbum library to 9.3. Set the version number of the MyPhotoAlbum library to 9.3. Add new settings to the PhotoAlbum class In the PhotoAlbum.cs file, add some variables to hold the new title, password, and display option settings. private string title...

Supporting the IList interface

Our implementation for IList will be very similar in spirit to our implementation for ICollection. A key difference between the signatures of the IList members and our implementation is that we will use the Photograph class explicitly rather than the more generic object. Since a Photograph is still an object instance, a construct that requires an IList instance will still be able to use our PhotoAlbum object. This may seem a bit boring and tedious, but it needs to be done for a complete...

Supporting label edits

Editing an item label in place is one of the advantages the ListView class has over ListBox objects. In our application it would be nice if the user could edit the album name in order to rename an album file. This section will show how to support this feature. Label editing is disabled by default, and turned on by setting the LabelEdit property to true. An actual edit of an item is initiated by the BeginEdit method of the ListViewltem class. The corresponding ListView control receives two...

Assigning merge types

As mentioned at the start of this chapter, the MenuItem class contains two properties that control exactly how two menus are merged together. This section will discuss the MergeType property that controls how the menus are merged. Later we will look at the MergeOrder property that controls the final position of a merged item. The MergeType property gets or sets a MenuMerge enumeration value specifying how this menu should be merged with other menus. An overview of this enumeration appears in...

Using the print classes

The parent form will make direct use of the print classes previously mentioned, contain the menu items for printing, and maintain the required PrintDocument object. Placing the print document on the parent form ensures that any changes made to the page margins or other document settings are seen by all child forms in the application. The following tables detail the changes required on the parent form. Set the version number for the MyPhotos application to 18.1. Modify parent form to support...

Creating an image list

For the ToolBar object we created in our MainForm class, we need an ImageList containing the set of images required for our ToolBarButton objects. We will use some of the bitmaps and icons in the common image directory provided with Visual Studio. If you skipped chapter 12, or were simply not paying attention, this directory is typically C Program Files Microsoft Visual Studio .NET Common7 Graphics. The following steps create an ImageList and associate the required image files with it. Set the...

Creating the Photograph class

The Photograph class represents a photograph stored in a file. Earlier, we laid out this class as follows. Create a new instance from a file name. Properties - get the file name for the Photograph - get the Bitmap for the Photograph - see if two Photographs are equal While we could implement this class within the PhotoAlbum.cs file, it makes more sense to separate these two classes into two separate files. In this section we create this new class file and add some initial properties for the...

Drawing the list items

As you may recall, the Drawltem event and related DrawltemEventArgs class were discussed in chapter 4. See .NET Table 4.4 on page 119 for an overview of the DrawltemEventArgs class. Before we look at how to draw the list items in our application, let's make a small change to the Photograph class to improve the performance of our drawing. Since we may have to draw an item multiple times, it would be nice to avoid drawing the thumbnail from the entire image each time. To avoid this, let's create...

Replacing the toolbar

Our toolbar was designed to interact with the menu bar for our MainForm class, and not the merged menu in our MDI application. As a result, it is no longer appropriate for our purposes. As a simple solution to this problem, we will simply hide the toolbar when the MainForm object is an MDI child form. While we are at it, we can create a very simple toolbar in the ParentForm class to demonstrate such a control in an MDI application. Figure 16.6 shows our application after these changes have been...

The relationship between Close and Dispose

Before we return to the topic of calling CloseCurrentAlbum when our application exits, let's look at the relationship between Close and Dispose in .NET. It's actually quite simple they are the same. For all classes in the .NET Framework, a call to Close is equivalent to calling the Dispose method, and a call to Dispose is equivalent to calling the Close method. The term close traditionally applies to objects like files and windows, and .NET has preserved this terminology. When you are finished...

Creating an MDI child list

It is common in MDI applications to provide a list of open windows as part of the Window menu. This permits the user to quickly jump to an open window at the click of the mouse. The .NET folks at Microsoft were kind enough to provide a quick way to do this through the MdiList property of the MenuItem class. Figure 16.10 In the list of child forms, note how the active child form is automatically checked by the framework. Figure 16.10 In the list of child forms, note how the active child form is...