2008-02-06
| Table of Contents: |
| Rate This Article: | Add This Article To: |
( Page 6 of 6 )
Including a date
One of the problems that occurs most often with forms is that users enter the date incorrectly. People are forever forgetting the current date (or they don't have a calendar handy for looking up the precise date when they do remember the approximate date). The code in this section helps a user enter an accurate date using one of two methods. Either the user lets the application calculate the date (based on some static criterion such as the time interval) or the user uses a calendar to choose the date. Listing 6-15 shows examples of both scenarios.
Listing 6-15: Choosing the Correct Date
public void DateLastMonth(
Office.IRibbonControl control)
{
// Obtain the correct date.
int DaysInMonth =
DateTime.DaysInMonth(DateTime.Today.Year,
DateTime.Today.Month);
DateTime TargetDate = DateTime.Today.Subtract(
new TimeSpan(DaysInMonth, 0, 0, 0));
string InsertDate = TargetDate.ToShortDateString();
// Insert the date into the field.
Globals.ThisAddIn.InsertDateInEmpReqField(
InsertDate);
}
public void DateCalendar(Office.IRibbonControl control)
{
// Create and display the dialog box.
Select_Date ChooseDate = new Select_Date();
if (ChooseDate.ShowDialog() == DialogResult.OK)
{
// Obtain the correct date.
string InsertDate =
ChooseDate.SelectDate.SelectionStart.ToShortDateString();
// Insert the date into the field.
Globals.ThisAddIn.InsertDateInEmpReqField(
InsertDate);
}
}
The DateLastMonth() callback shows an example of a date calculation (the most complex for the example). The code begins by creating a variable that contains the number of days in the current month. When the application subtracts this value from the current date, it receives the proper date from the previous month. The calculation relies on the DateTime.Today.Subtract() method, which requires a TimeSpan as input. As always, the code must call the ThisAddIn class to make the actual entry.
The calendar method relies on the form shown in Figure 6-15. The user chooses a date in the dialog box and clicks OK to make the date entry. The code obtains the information directly from the MonthCalendar control (make sure you make the control public). Notice that the code calls the same ThisAddIn class method as the DateLastMonth() callback to add the date to the form.
The final method appears as part of the ThisAddIn class. The application uses the following method to add all of the dates to the form:
public void InsertDateInEmpReqField(String InsertDate)
{
// Define the field to fill.
object SelectEmpReq = (object)"ReqDate";
// Insert the date into the field.
Application.ActiveDocument.FormFields.get_Item(
ref SelectEmpReq).Result = InsertDate;
}
As with the other form methods, the InsertDateInEmpReqField() method begins by creating a variable to represent the name of the field on the form. It then uses the Application.ActiveDocument.FormFields. get_Item() method to add the date to the form.
Figure 6-15: Select a date from the calendar when a static choice won't work.
![]() |
|


