Techniques - DevSource
DevSource: Microsoft Developer Resource DevSource Home Sponsored by Microsoft Home Add Ons Architecture Languages Techniques Using VS Forums
Home arrow Techniques arrow Page 6 - RibbonX for Dummies: Chapter 6 (Part 2)
RibbonX for Dummies: Chapter 6 (Part 2)
By John Mueller

Rate This Article: Add This Article To:

RibbonX for Dummies: Chapter 6 (Part 2)
( 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.

ADVERTISEMENT

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.

 



 
 
>>> More Techniques Articles          >>> More By John Mueller
 



DevSource video
Devsource Video Series
Manipulating Society through Technology
Jeremy Bailenson, Director of the Virtual Human Interaction Lab at Stanford University, talks about virtual reality, avatars, Moore's law, how real world behaviors influence online reality, and societal manipulation through technology!
>> Play video
>> Read article
>> See all videos
DevLife Blog

Julia explores the Robotics Studio! (It's for more than you think.)

MSDev Blog

Messages for Bill Gates!

Make it Work
.NET makes runtime type checking a breeze. See what Peter has to say about it in this week's tips!
News
Microsoft Counts on App Support for Vista
Microsoft has taken pains to demonstrate that Windows Vista will have ample application support.
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.