Dates and times

Our Photo Properties dialog with a DateTimePicker control in place is shown in figure 11.5. As you can see, the dropdown calendar control is displayed for the object. The DateTimePicker shown here displays the Long date format, which is the default. We can add this control to our dialog using the following steps. We will begin with the default display settings for this control, and look at how to modify these settings later in the section. Set the version number of the MyPhotoAlbum application...

Arranging MDI forms

In an MDI application, as well as on the Windows desktop, a number of windows are created and strewn about in various locations. It would be nice if our application permitted automatic organization of the windows at the user's request. This would allow the user to immediately see all open windows and select the desired one. Such support is provided by the LayoutMdi method of the Form class. This method accepts an enumeration value specifying the type of layout to apply to the MDI container, as...

Using the web browser control

So far we have defined a user interface and created a wrapper class for the Web Browser ActiveX control. In this section we will implement the AboutBox form to work as described earlier. The following table begins this process by describing the changes required for our standard Windows Forms controls. In the AboutBox.cs code window, create two constants for the two types of icons in our image list. protected const int SDI_ICON 0 protected const int MDI_ICON 1 Implement an IsMdiApplication...

Populating the columns

The columns in a Details listing contain both the item label and the text associated with each subitem. The first column always contains the item label, and the subsequent columns contain the contents of the list item's Subitems property. The Subitems property contains a collection of ListViewSubltem objects. The ListViewSub-Item class is only valid within the ListViewltem class, so the fully qualified class name within the Windows Forms namespace is ListViewltem.ListViewSubltem. This class is...

Defining a shared handler

The submenu for the Image menu item pops up whenever the Image menu is clicked. Our submenu items are selected by the user to control how the image should appear in the window. To implement this behavior, we will alter the SizeMode property of our PictureBox control depending on which menu was selected. The SizeMode values for these menus are as follows The SizeMode settings for the Image submenu items The SizeMode settings for the Image submenu items As we have already seen, this value causes...

Creating a list view

This section will drop some menu objects and a list view control onto our form so we can examine and manipulate these controls in Visual Studio .NET. These steps will also create some menus we will use as we move through the chapter. Add a menu and list view to our form Add a MainMenu object to the form in the MainForm.cs Design window. Create the following top-level menus. Settings This graphic is the result of steps 2 and 3. Create the following top-level menus. Settings Create four menus...

Casting the NET

Against this backdrop of technologies and products, the Internet has been redefining the way we as programmers think about user interfaces and application development. With the success of the Internet and companies such as Netscape and Amazon.com, a product or interface may now appear in a web browser rather than a more traditional Windows application. The Java programming language has had much success in UNIX and web server environments, and is the language of choice for many large-scale web...

Supporting node selection

As we saw for the expand and collapse operations, there are two events associated with node selection. The BeforeSelect event occurs before the node is selected in the control, and receives a TreeViewCancelEventArgs instance containing the event data. The AfterSelect event occurs after the node has been selected, and receives a TreeViewEventArgs instance. 1 It also contains disks, the desktop, the control panel, and other objects. For the purposes of our example, we can pretend that it contains...

Implementing the Stretch to Fit option

The modification of our Image menu handlers fully eradicates the PictureBox control from our application. While the PictureBox control created some problems, it also drew the current photograph for us. Now we will need to do this by hand, the result of which is shown in figure 7.2. You will note in this figure that the application is lacking the border previously shown around the image by the PictureBox control. This will be addressed later in the chapter. The System.Drawing namespace expands...

Creating tool tips

While it is certainly possible to assign tool tips for our MainForm controls in the MyPhotos project, this would not be a very exciting example. Since menu objects are not controls, we cannot assign tool tip text to our menu items. As we saw earlier in this chapter and in chapter 4, the ToolBar and StatusBar controls provide their own tool tip mechanism. That leaves the Panel object, which is only a single control. Instead, we will look to our now-famous MyPhotoAlbum library for a rich source...

Desktop layout properties

The first change in our program sets the MinimumSize property to define the minimum possible size for the form. This ensures that the form never becomes so small that the PictureBox disappears and our image cannot be seen. The MinimumSize property is a Size structure representing the minimum width and height of the form. As you may recall, structures are value types and store their data directly, either on the stack or as part of the containing type. As we discussed in section 1.1.3, the new...

Creating tab pages in Visual Studio

In this section we will replace the existing Photographs group box in the MyAlbum-Editor application with a tab control. This control will contain the controls currently in the Photographs group box. Here we will only create a single tab page. A second tab page will be added later in the chapter. Figure 11.4 shows how this new window will look. C My Documents'',, Albums'-,leeds.abm The Photos tab will contain the controls we created in chapter 10. The tab, shown here on the left, can be placed...

Creating a slide show form

We begin our discussion with the design of the new form. This form will use a control we have not previously discussed, namely the TrackBar control. A summary of this control is given in .NET Table 18.3. The TrackBar class represents a control that supports tracking of an integer value through a scrolling interface. The control may appear horizontally or vertically. This class is part of the System.windows.Forms namespace, and inherits from the Control class. Gets or sets whether the control...

Handling Popup events

Users appreciate feedback on the current settings for an application. Our current interface does not yet do this. The user has to understand the possible displays modes in order to know what is currently selected and to choose a different setting. A nicer interface would somehow highlight the current selection in the menuImage submenu. This would immediately indicate what mode is currently displayed, and help our user make a more informed selection. If you look at the MenuItem class, there is a...

Adding a status bar

As you might expect, a status bar can be added to our application in Visual Studio by dragging one from the Toolbox window onto the form. Set the version number of the application to 4.2. Place a status bar at the base of the MyPhotos application. a. Display the MainForm.cs Design window. b. Drag a StatusBar object from the Toolbox window onto the form. Set the Text property for the StatusBar control to Ready The new status bar appears In the designer window. For lack of a better choice, we'll...

Revisiting the list view

The contents of our ListView control can be modified directly by the user through the control itself and through the menu bar items. There are three actions a user can perform to alter the list contents 1 Select the Albums menu item under the View menu. This invokes a Click event for the menu, and our menuAlbums_Click event handler. 2 Select the Photos menu item under the View menu, which can only be done when an item representing an album is selected in the list view. This invokes a Click...

Supporting the IEditable Object interface

Looking at the Photograph class, there are four modifiable properties. These are the Caption, Photographer, DateTaken, and Notes properties. As a result, these are the properties we need to consider in our IEditableObject implementation. The following table summarizes the implementation of the required methods Implementation of IEditableObject methods for the Photograph class Implementation of IEditableObject methods for the Photograph class Should record the existing values of the modifiable...

Displaying the photograph

Our first topic is displaying the photograph when a photograph node is selected in the tree view. You might think that we could draw directly on the ListView control. In fact, the ListView class does not permit the Paint event to be handled by an instance of the class. So an alternate approach is required. Instead, we will use a PictureBox control for this purpose. Since the Picture-Box control does not support a proper aspect ratio for its contained image, we will handle the Paint event and...

Handling the Image menu

One of the unfortunate but necessary side effects of nuking the PictureBox control is that our processing of the Image menu no longer makes sense. We built this with the PictureBoxSizeMode enumeration values in mind. Since we are no longer using this control, we need to change how our menu works. As an alternative solution, we will create our own enumerator for this purpose to use in place of the PictureBoxSizeMode enumeration. We could provide a long discussion of the individual changes...

Adding a Month Calendar control

An overview of the MonthCalendar class is provided in .NET Table 11.5. This class handles the entire range of dates possible in DateTime objects, which is basically any date with a four-digit century. This class is a good way to display a series of dates related to an object or collection of objects. The MonthCalendar class represents a control that displays one or more months to the user. Days in each month can be displayed in bold, and the user can select single or multiple dates. This class...

Defining new columns

As you'll recall, we defined the list of columns for our control using the Column-Header Collection Editor dialog in Visual Studio .NET. Now that we need to display different columns depending on what is displayed, this method no longer makes sense. Instead, we will create the columns programmatically in the LoadAlbumData method. Our new LoadPhotoData method we have yet to implement will define the columns for displaying photographs. The easiest way to add columns to a ListView control...

Handling item activation

The ultimate goal here is to display either the list of albums or the list of photos in an album within our ListView control. To do this, we must keep track of whether albums or photographs are currently shown in the view, and whether the PhotoAlbum object corresponds to the view when photographs are displayed. The following steps create private fields in our Form to track this information, and also implement an event handler for the ItemActivate event. Once these are available, we will look at...

The Anchor property

Vb6 Height Scale Height

The remaining two lines added to our program use the Anchor property to fix the control on the form in relation to the form's edges. Create and configure the Button btnLoad.Anchor AnchorStyles.Top AnchorStyles.Left Create and configure the PictureBox pboxPhoto.Anchor AnchorStyles.Top AnchorStyles.Bottom AnchorStyles.Left AnchorStyles.Right IgB Start I on II J5amples lf WForms- B5elect Vis OSMicrosoFt Microsoft ig Hello For jg unti ed - Figure 1.7 The properties for the Form class that relate to...

The Draw Item event

The DrawItem event is used by a number of classes to draw an item contained within some sort of larger collection. For instance, the MenuItem, ListBox, and ComboBox classes all include a DrawItem event for custom drawing of their contents. These classes use the DrawItemEventArgs class to provide the data associated with the event. The StatusBar class uses a derived version of this class, but the bulk of the drawing information is in the base class. An overview of this base class is provided in...

Data collection classes

Looking over the interfaces in the table, the IList interface seems particularly appropriate for the task at hand. This allows elements to be added and removed from the collection, and supports array-style indexing. Some of the data collection classes in the .NET Framework are shown in the following table. Note, in particular, those classes in the table that support the IList interface. Some .NET classes related to data collections Some .NET classes related to data collections The base class...

Customizing table styles

The DataGridTableStyle class permits a custom style for a specific type of table to be defined. Many of the members of this class are duplicates of similar members in the DataGrid class. The members of the active table style always override the default settings for the data grid. A summary of this class appears in .NET Table 17.2. .NET Table 17.2 DataGridTableStyle class The DataGridTableStyle class represents the style In which to display a particular table that can appear in a DataGrid...

Adding a context menu

Since we would like to dynamically switch between an owner-drawn and a framework-drawn control, we need a way for the user to select the desired drawing method. We will use a menu for this purpose, and include a check mark next to the menu when the thumbnail images are shown. Context menus were discussed in chapter 3, so the following steps should be somewhat familiar. Set the version number of the MyAibumEditor application to 10.5. Add a ContextMenu control named ctxtPhotoList to the form in...

Updating the title bar

An MDI application should normally update its title bar to reflect the contents of the currently active child form. This provides good feedback to your users, especially when the application is minimized and only appears in the task bar. In our case, we will also include the version number on the title bar as is our custom. The result of this change is shown in figure 16.8. We could like to update the title bar whenever a new form is activated within the MDI container. There is an event for...

The Open FileDialog class

Once our OnLoadClick event handler is registered, we are ready to load a new image into the application. The signature of the OnLoadClick method must match the signature of the EventHandler delegate by being a void function that accepts an object and EventArgs parameter. Note how this is a private method so that it is not available except within the MyForm class. private void OnLoadClick(object sender, System.EventArgs e) OpenFileDialog dlg new OpenFileDialog() dlg.Filter jpg files (*.jpg)...

Creating a project

To begin, let's create a Visual Studio project called MyPhotos for our new application. This application will duplicate the functionality presented in section 1.1. The following table enumerates the steps required. We discuss the term project and other aspects of the application following this table. Locate the appropriate item in the Start menu. The Microsoft Development Environment displays with the Start Page shown. Note This window is illustrated in figure 2.1. You may want to consider...

Customizing column styles

Now that we know how to customize the properties of a table, let's look at how to customize the columns that appear in the table. The DataGridColumnStyle class is used for this purpose, and is summarized in .NET Table 17.3. This is an abstract class from which various types of columns are derived. The .NET Framework currently provides classes to represent boolean and text columns, namely the Data-GridBoolColumn and DataGridTextBoxColumn classes. .NET Table 17.3 DataGridColumnStyle class The...

Constructors and methods

Take another look at the declaration of our MyForm class. Note how two members of this class are defined, namely the MyForm constructor and the Main method. Both members are declared as public, as is the class MyForm. C provides the accessibility levels public, protected, and private that C++ programmers should be familiar with. These are discussed in appendix A, as are the additional access levels provided by C , namely internal and protected internal. public class MyForm...

Drawing bitmaps for our buttons

The creation of graphics for a new product is typically left to the graphic designers and marketing folks. Still, you never know when you may feel the urge to draw a bitmap or icon yourself, so let's look at how to create them from scratch. Fortunately, Microsoft provides a fairly extensive collection of bitmaps, cursors, and icons that can be used within our application. We'll talk about where to find these in a moment. A bitmap is created much like any other item in a project. We will begin...

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 Date TimePicker 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...

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

Radio Button Visual Basic For

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 Assembly Info 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 Photo Album 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...