- •brief contents
- •about this book
- •The Windows Forms namespace
- •Part 1: Hello Windows Forms
- •Part 2: Basic Windows Forms
- •Part 3: Advanced Windows Forms
- •Who should read this book?
- •Conventions
- •Action
- •Result
- •Source code downloads
- •Author online
- •acknowledgments
- •about .NET
- •Casting the .NET
- •Windows Forms overview
- •about the cover illustration
- •Hello Windows Forms
- •1.1 Programming in C#
- •1.1.1 Namespaces and classes
- •1.1.2 Constructors and methods
- •1.1.3 C# types
- •1.1.4 The entry point
- •1.1.5 The Application class
- •1.1.6 Program execution
- •1.2 Adding controls
- •1.2.1 Shortcuts and fully qualified names
- •1.2.2 Fields and properties
- •1.2.3 The Controls property
- •1.3 Loading files
- •1.3.1 Events
- •1.3.2 The OpenFileDialog class
- •1.3.3 Bitmap images
- •1.4 Resizing forms
- •1.4.1 Desktop layout properties
- •1.4.2 The Anchor property
- •1.4.3 The Dock property
- •1.5 Recap
- •2.1 Programming with Visual Studio .NET
- •2.1.1 Creating a project
- •Action
- •Result
- •2.1.2 Executing a program
- •Action
- •Result
- •2.1.3 Viewing the source code
- •View the code generated by Visual Studio .NET
- •Action
- •Result
- •2.2 Adding controls
- •2.2.1 The AssemblyInfo file
- •Action
- •Results
- •2.2.2 Renaming a form
- •Action
- •Result
- •2.2.3 The Toolbox window
- •Action
- •Result
- •2.3 Loading files
- •2.3.1 Event handlers in Visual Studio .NET
- •Action
- •Result
- •2.3.2 Exception handling
- •Action
- •Result
- •Action
- •Results and Comments
- •2.4 Resizing forms
- •2.4.1 Assign the Anchor property
- •Action
- •Result
- •2.4.2 Assign the MinimumSize property
- •Action
- •Result
- •2.5 Recap
- •Basic Windows Forms
- •Menus
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •3.3 Click events
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •3.5 Context menus
- •Action
- •Result
- •Action
- •Result
- •3.6 Recap
- •Status bars
- •4.1 The Control class
- •4.2 The StatusBar class
- •Action
- •Result
- •Action
- •Result
- •4.3.1 Adding panels to a status bar
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •4.5 Recap
- •Reusable libraries
- •5.1 C# classes and interfaces
- •5.2 Class libraries
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •5.3 Interfaces revisited
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •5.4 Robustness issues
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Common file dialogs
- •Action
- •Results
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •6.3 Paint events
- •Action
- •Result
- •Action
- •Result
- •6.4 Context menus revisited
- •Action
- •Result
- •Action
- •Result
- •6.5 Files and paths
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •6.6 Save file dialogs
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •6.7 Open file dialogs
- •Action
- •Result
- •Action
- •Result
- •6.8 Recap
- •Drawing and scrolling
- •7.1 Form class hierarchy
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •7.4 Panels
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Dialog boxes
- •8.1 Message boxes
- •Action
- •Result
- •Action
- •Result
- •8.1.4 Creating A YesNoCancel dialog
- •Action
- •Result
- •Action
- •Result
- •8.2 The Form.Close method
- •8.2.1 The relationship between Close and Dispose
- •Action
- •Result
- •8.3 Modal dialog boxes
- •Action
- •Result
- •Action
- •Result
- •8.3.2 Preserving caption values
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Basic controls
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •9.1.2 Creating a derived form
- •Action
- •Result
- •9.2 Labels and text boxes
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •9.3.6 Adding AlbumEditDlg to our main form
- •Action
- •Result
- •Action
- •Result
- •9.4 Recap
- •List controls
- •10.1 List boxes
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •10.2 Multiselection list boxes
- •10.2.1 Enabling multiple selection
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •10.3 Combo boxes
- •Action
- •Result
- •Action
- •Result
- •10.4 Combo box edits
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •10.5 Owner-drawn lists
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •More controls
- •Action
- •Result
- •Action
- •Result
- •11.2 Tab pages
- •Action
- •Result
- •Action
- •Result
- •11.3.1 Dates and times
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •11.5 Recap
- •A .NET assortment
- •12.1 Keyboard events
- •Action
- •Result
- •Action
- •Result
- •12.2 Mouse events
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •12.3 Image buttons
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •12.4 Icons
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •12.5 Recap
- •Toolbars and tips
- •13.1 Toolbars
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •13.4.2 Creating tool tips
- •Action
- •Result
- •Action
- •Result
- •Advanced Windows Forms
- •List views
- •14.2 The ListView class
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •14.2.3 Populating a ListView
- •Action
- •Result
- •Action
- •14.3 ListView columns
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •14.6 Recap
- •Tree views
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •15.3 Dynamic tree nodes
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •15.4 Node selection
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •15.5 Fun with tree views
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Multiple document interfaces
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •16.3 Merged menus
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •16.4 MDI children
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •16.5 MDI child window management
- •Action
- •Result
- •Action
- •Result
- •16.6 Recap
- •Data binding
- •17.1 Data grids
- •Action
- •Result
- •Action
- •Result
- •17.2 Data grid customization
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Odds and ends .NET
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •18.2 Timers
- •Action
- •Result
- •Action
- •Result
- •18.3 Drag and drop
- •Action
- •Result
- •Action
- •Result
- •18.4 ActiveX controls
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •18.5 Recap
- •C# primer
- •A.1 C# programs
- •A.1.1 Assemblies
- •A.1.2 Namespaces
- •A.2 Types
- •A.2.1 Classes
- •A.2.2 Structures
- •A.2.3 Interfaces
- •A.2.4 Enumerations
- •A.2.5 Delegates
- •A.3 Language elements
- •A.3.1 Built-in types
- •A.3.2 Operators
- •A.3.3 Keywords
- •A.4 Special features
- •A.4.1 Exceptions
- •A.4.2 Arrays
- •A.4.3 Main
- •A.4.4 Boxing
- •A.4.5 Documentation
- •.NET namespaces
- •B.1 System.Collections
- •B.2 System.ComponentModel
- •B.3 System.Data
- •B.4 System.Drawing
- •B.5 System.Globalization
- •B.6 System.IO
- •B.7 System.Net
- •B.8 System.Reflection
- •B.9 System.Resources
- •B.10 System.Security
- •B.11 System.Threading
- •B.12 System.Web
- •B.13 System.Windows.Forms
- •B.14 System.XML
- •Visual index
- •C.1 Objects
- •C.2 Marshal by reference objects
- •C.3 Components
- •C.4 Common dialogs
- •C.7 Event data
- •C.8 Enumerations
- •For more information
- •bibliography
- •Symbols
- •Index
DISPLAY A CUSTOM DATE-TIME VALUE IN THE DATETIMEPICKER CONTROL
|
|
|
Action |
Result |
||
|
|
|
|
|
|
|
1 |
Display the properties for the |
|
||||
|
DateTimePicker control in the |
|
||||
|
PhotoEditDlg.cs [Design] window. |
|
||||
|
|
|
|
|
|
|
2 |
Modify this control to display a custom |
The control displays the new format within Visual |
||||
|
format string. |
|
|
|
Studio. |
|
|
|
|
Settings |
|
||
|
|
|
|
|
|
|
|
|
Property |
|
Value |
|
|
|
|
CustomFormat |
|
MM/dd/yy 'at' hh:mm tt |
|
|
|
|
Format |
|
Custom |
|
|
|
|
|
|
|
|
|
If you compile and run these changes, you will find that the dropdown calendar still appears. The time values can be modified by hand or with the arrow keys. You might try using some alternate format strings, or setting the ShowUpDown property to true as a way to experiment with these customized settings.
The DateTimePicker class is great for displaying a single date-time value. When multiple dates or a range of dates are required, the MonthCalendar class can be used. We will discuss this control next.
More .NET As an alternative to a DateTimePicker control, another option here is to create separate controls for the month, day, and year, and if necessary the time of day. While the TextBox or ComboBox controls could be used for this purpose, you could also use the DomainUpDown and NumericUpDown controls. These controls are derived from the UpDownBase control, which in turn is based on the ContainerControl class presented in chapter 7.
The up-down controls present a text-box-like window that displays a range of values. The DomainUpDown control presents a string value taken from a collection of objects, while the NumericUpDown control presents a numeric value, optionally over a defined range.
For separate month, day, and year controls, the properties for the DateTimeFormatInfo class shown earlier in this section may be used to obtain the default set of month strings for display within a DomainUpDown control. The day and year values can be displayed in a NumericUpDown control, with the range set based on the current month and the requirements of the application.
DATES AND TIMES |
371 |
11.4CALENDARS
Sometimes a single date will not do. A scheduling program, for example, might need to show a calendar with meeting days highlighted, or display a meeting that covers a range of dates. The MonthCalendar class allows one or more months to be displayed on a Form, with individual days highlighted or a range of days selected.
Since our PhotoAlbum class permits each photograph to specify its own date, it seems appropriate to demonstrate the calendar control by highlighting the days in a calendar on which a photograph was taken. We will do this by adding a second TabPage object to our MyAlbumEditor main window. The result of our changes is shown in figure 11.6. Note how some dates are in bold to indicate one or more photographs were taken that day. If the user clicks on a date, a context menu pops up containing the corresponding photographs. When a photograph is selected from this context menu, the properties for that photograph are displayed.
The interface in figure 11.6 provides a very different view of our album. While the order of photographs in the album is not apparent, the specific days that a collection of pictures was taken is immediately available.
This section will discuss the month calendar control in general and add the control to a new tab page in our application. We will discuss how to bold the dates when photographs were taken, and how to process and respond to mouse clicks made within the control.
Figure 11.6
The MonthCalendar control will automatically display multiple months as it is resized.
11.4.1ADDING A MONTHCALENDAR 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.
372 |
CHAPTER 11 MORE CONTROLS |
.NET Table 11.5 MonthCalendar class
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 is part of the System.Windows.Forms namespace, and inherits from the Control class. See .NET Table 4.1 on page 104 for a list of members inherited from this class.
|
AnnuallyBoldedDates |
Gets or sets an array of DateTime objects that |
|
|
indicate which days to show in bold on an annual |
|
|
basis. |
|
BoldedDates |
Gets or sets an array of DateTime objects of specific |
|
|
dates to show in bold. |
|
MaxDate |
Gets or sets the maximum date. The user will not be |
|
|
able to display months occurring after this date. |
|
MaxSelectionCount |
Gets or sets the maximum number of dates that can |
|
|
be selected in the control. Defaults to seven (7). |
Public |
ScrollChange |
Gets or sets the number of months to scroll per click |
|
of a scroll button. Defaults to one (1). |
|
Properties |
|
|
|
|
|
|
SelectionRange |
Gets or sets the range of dates selected in the |
|
|
control. |
|
SelectionStart |
Gets or sets the initial date of the range selected in |
|
|
the control. |
|
ShowToday |
Gets or sets whether to display the TodayDate |
|
|
value at the bottom of the control. |
|
ShowTodayCircle |
Gets or sets whether the TodayDate value is circled. |
|
TodayDate |
Gets or sets the DateTime value used as today’s |
|
|
date. |
|
|
|
|
AddAnnuallyBoldedDate |
Adds a day to display in bold on an annual basis. |
|
GetDisplayRange |
Retrieves the range of dates displayed by the |
|
|
control. |
Public |
HitTest |
Determines which aspect of the month calendar |
Methods |
|
control is located at a specific point. |
|
RemoveBoldedDate |
Removes a specific date from the list of nonrecurring |
|
|
bolded dates. |
|
SetDate |
Selects the given date in the control. |
|
|
|
|
DateChanged |
Occurs when the current date in the control is |
Public |
|
modified, such as when a new month is displayed. |
|
|
|
Events |
DateSelected |
Occurs when the dates selected in the control are |
|
||
|
|
modified. |
|
|
|
In our case, we will display the dates associated with a collection of photographs. Let’s begin by adding a new TabPage containing a MonthCalendar control to our form.
CALENDARS |
373 |
Set the version number of the MyAlbumEditor application to 11.4.
|
|
|
|
|
CREATE THE DATES TAB PAGE |
|||
|
|
|
|
|
|
|
|
|
|
|
|
Action |
Result |
||||
|
|
|
|
|
|
|
|
|
1 |
|
In the MainForm.cs [Design] |
|
|||||
|
|
window, add a second tab page |
|
|||||
|
|
to the TabControl object. |
|
|||||
|
|
|
Settings |
|
||||
|
|
|
|
|
|
|
|
|
|
|
|
Property |
Value |
|
|||
|
|
|
(Name) |
tabDates |
|
|||
|
|
|
Text |
Dates |
|
|||
|
|
|
|
|
|
|
|
|
2 |
|
Add a MonthCalendar control |
|
|||||
|
|
to this page. |
|
|
|
|
||
|
|
|
Settings |
|
||||
|
|
|
|
|
|
|
||
|
|
|
Property |
|
Value |
|
||
|
|
|
(Name) |
|
monthCalDates |
|
||
|
|
|
Dock |
|
Fill |
|
||
|
|
MaxSelection- |
|
1 |
|
|
|
|
|
|
|
Count |
|
|
|
|
|
|
|
|
ShowToday |
|
False |
|
||
|
|
|
|
|
|
|
|
Note: Your MonthCalendar control will circle the |
|
|
|
|
|
|
|
|
current date, which is likely not the date shown in |
|
|
|
|
|
|
|
|
the graphic. |
|
|
|
|
|
|
|
|
|
You will note that the Dock property for our month calendar object is set to Fill. This ensures that the number of months displayed will expand to fill the entire tab page as the form is enlarged. As we will see in the next section, months before the MinDate property value and after the MaxDate value will not be accessible from this control.
11.4.2INITIALIZING A CALENDAR
Now that our MonthCalendar control is on the form, we can hook it up to our PhotoAlbum class. We do not want to initialize the calendar needlessly, so we will only do so when the Dates tab is displayed. By the same token, we do not want to initialize the lstPhotos list box needlessly, so we need to ensure that this only occurs when the Photos tab is displayed. Since we used the method UpdateList for our list box, we will create an UpdateCalendar method to initialize and update our
MonthCalendar control.
The following steps are required for this change:
374 |
CHAPTER 11 MORE CONTROLS |