In this exercise you will use an Outlook Form Region to extend the Task form in Outlook. The extensions will allow tracking of billable tasks within Outlook’s storage location.
In this first task, you will create a new Outlook Form Region attached to the existing Task forms in Outlook.
- Create new Outlook 2010 add-in project
- Open VisualStudio 2010 and select File -> New -> Project
- In the New Project dialog select the Visual C#(in case of C#)/ Visual Basic(in case of VB) -> Office -> 2010 templates
- Choose an Outlook 2010 Add-in template
- Set the Name of the project to OutlookFormRegion and set the location to %Office2010DeveloperTrainingKitPath%\Labs\OfficeUI\Source\[language]\Starter
Verify Create directory for solution is unchecked and click OK to create the new project
Figure 7(a)-In case of C#
Figure 7(b)- In case of VB
New Outlook 2010 Addin Project
- Add a new form region named BillableTaskRegion to the Task form
- Right click on OutlookFormRegion in the Solution Explorer and click Add -> New Item
- In the Add New Item dialog choose the Visual C#(in case of C#)/ Visual Basic(in case of VB) -> Office templates
- Choose OutlookFormRegion and name it BillableTaskRegion.vb(in case of VB) & BillableTaskRegion.cs(in case of C#)
- Click Add to create the new item
- In the first page of the New Outlook Form Region wizard, choose Design a new form region and click Next
In the next page, select Adjoining and click Next
Figure 8
Choose Form Region Type
- Change the name to Billable Task and click Next
Choose the Task option and clear all other check boxes then click Finish
Figure 9
Choose Form Region Class
The message classes chosen determine which types of items receive the new form region. In this case you will be extending the IPM.Task message class only
- In this second task, you will add controls to the form and implement the code behind necessary to make the form region functional.
Add the controls to the form region to create the Billable Task extensions
Figure 10
Completed Custom Form Region
- Open the designer for BillableTaskRegion.vb(in case of VB) & BillableTaskRegion.cs(in case of C#) by right clicking it in the Solution Explorer and clicking View Designer
- Right click the design surface and click Properties
- In the Properties window, set the size of the canvas to 486, 150
- Using the toolbox, drag the following controls onto the canvas
- 1 CheckBox
- 1 ComboBox
- 1 NumericUpDown
- 1 TextBox
- 2 Labels
- Set the following properties on the CheckBox controls
- (Name) & Text – chkBillable & Billable
- Location & Size – 6, 6 & 59, 17
- Set the following properties on one of the Label controls
- Text –Customer
- Location & Size – 87, 7 & 51, 13
- Set the following properties on the ComboBox control
- (Name) – lstCustomer
- Location & Size – 143, 4 & 167, 21
Items – Fabrikam Inc., Adventure Works
Enter each company on its own line
- Set the following properties on the remaining Label control
- Text –Hours
- Location & Size – 347, 7 & 35, 13
- Set the following properties on the NumericUpDown control
- (Name) – numHours
- Location & Size – 386, 4 & 95, 20
- Set the following properties on the TextBox control
- (Name) - txtDetails
- Multiline - true
- Location & Size – 5, 31 & 476, 116
- Anchor – Top, Bottom, Left, Right
- Add the code behind that will implement the functionality of the form
- Open the code behind for BillableTaskRegion.vb(in case of VB) & BillableTaskRegion.cs(in case of C#) by right clicking it in the Solution Explorer and clicking View Code
Add the following properties to the BillableTaskRegion class
private Outlook.TaskItem m_taskItem; private Outlook.ItemProperty m_isBillable; private Outlook.ItemProperty m_customer; private Outlook.ItemProperty m_hours; private Outlook.ItemProperty m_details;
Private m_taskItem As Outlook.TaskItem Private m_isBillable As Outlook.ItemProperty Private m_customer As Outlook.ItemProperty Private m_hours As Outlook.ItemProperty Private m_details As Outlook.ItemProperty
Add the EnsureItemProperty method to aid in creating custom properties on the task
private void EnsureItemProperty( ref Outlook.ItemProperty property, string name, Outlook.OlUserPropertyType propertyType) { if (property == null) { property = m_taskItem.ItemProperties[name]; if (property == null) property = m_taskItem.ItemProperties.Add(name, propertyType); } }
Private Sub EnsureItemProperty(ByRef [property] As Outlook.ItemProperty, ByVal name As String, ByVal propertyType As Outlook.OlUserPropertyType) If [property] Is Nothing Then [property] = m_taskItem.ItemProperties(name) If [property] Is Nothing Then [property] = m_taskItem.ItemProperties.Add(name, propertyType) End If End If End Sub
Add the EnsureProperties method to verify all of the custom task properties exists
private void EnsureProperties() { EnsureItemProperty(ref m_isBillable, "Billable", Outlook.OlUserPropertyType.olYesNo); EnsureItemProperty(ref m_customer, "Billable Customer", Outlook.OlUserPropertyType.olText); EnsureItemProperty(ref m_hours, "Billable Hours", Outlook.OlUserPropertyType.olNumber); EnsureItemProperty(ref m_details, "Billing Details", Outlook.OlUserPropertyType.olText); }
Private Sub EnsureProperties() EnsureItemProperty(m_isBillable, "Billable", Outlook.OlUserPropertyType.olYesNo) EnsureItemProperty(m_customer, "Billable Customer", Outlook.OlUserPropertyType.olText) EnsureItemProperty(m_hours, "Billable Hours", Outlook.OlUserPropertyType.olNumber) EnsureItemProperty(m_details, "Billing Details", Outlook.OlUserPropertyType.olText) End Sub
Add UpdateEnableState to update the state of the controls based on the state of the Billable checkbox
public void UpdateEnableState() { lstCustomer.Enabled = chkBillable.Checked; numHours.Enabled = chkBillable.Checked; txtDetails.Enabled = chkBillable.Checked; }
Public Sub UpdateEnableState() lstCustomer.Enabled = chkBillable.Checked numHours.Enabled = chkBillable.Checked txtDetails.Enabled = chkBillable.Checked End Sub
Implement FormRegionShowing to load the current property values into the controls and setup the initial state of the form region
m_taskItem = this.OutlookItem as Outlook.TaskItem; EnsureProperties(); chkBillable.Checked = m_isBillable.Value; UpdateEnableState(); lstCustomer.SelectedText = m_customer.Value; numHours.Value = (decimal)m_hours.Value; txtDetails.Text = m_details.Value;
m_taskItem = TryCast(Me.OutlookItem, Outlook.TaskItem) EnsureProperties() chkBillable.Checked = m_isBillable.Value UpdateEnableState() lstCustomer.SelectedText = m_customer.Value numHours.Value = CDec(m_hours.Value) txtDetails.Text = m_details.Value
- Add code behind that will write the contents of the controls as they change
Create a method named chkBillable_CheckedChanged to handle the Billable check box’s checked event
private void chkBillable_CheckedChanged(object sender, EventArgs e) { m_isBillable.Value = chkBillable.Checked; UpdateEnableState(); }
Private Sub chkBillable_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) m_isBillable.Value = chkBillable.Checked UpdateEnableState() End Sub
Create a method named lstCustomer_TextChanged to handle the Customer drop down control’s changed event
private void lstCustomer_TextChanged(object sender, EventArgs e) { m_customer.Value = lstCustomer.Text; }
Private Sub lstCustomer_TextChanged(ByVal sender As Object, ByVal e As EventArgs) m_customer.Value = lstCustomer.Text End Sub
Create a method named numHours_ValueChanged to handle changes in the Hours control
private void numHours_ValueChanged(object sender, EventArgs e) { m_hours.Value = (double)numHours.Value; }
Private Sub numHours_ValueChanged(ByVal sender As Object, ByVal e As EventArgs) m_hours.Value = CDbl(numHours.Value) End Sub
Create a method named txtDetails_TextChanged to handle changes to the Details control
private void txtDetails_TextChanged(object sender, EventArgs e) { m_details.Value = txtDetails.Text; }
Private Sub txtDetails_TextChanged(ByVal sender As Object, ByVal e As EventArgs) m_details.Value = txtDetails.Text End Sub
- Connect the code behind to the controls using the designer
- Open the designer for BillableTaskRegion.vb(in case of VB) & BillableTaskRegion.cs(in case of C#) by right clicking it in the Solution Explorer and clicking View Designer
- Right click the Billable check box control and click Properties
- In the Properties window, click the Event icon (the lightning bolt)
Set the CheckedChanged event to chkBillable_CheckedChanged using the drop down list
- Repeat the above process to attach the Customer control’s TextChanged event to the lstCustomer_TextChanged method
- Repeat the above process to attach the Hours control’s ValueChanged event to the numHours_ValueChanged method
- Repeat the above process to attach the Details control’s TextChanged event to the txtDetails_TextChanged method
Exercise 3 Verification
In order to verify that you have correctly performed all steps in the above exercise, proceed as follows:
Test the Add-in
Test your add-in to confirm that the export ribbon and backstage buttons work as expected.
- Run the add-in and verify the enable checkboxes work
- In the Debug menu, click Start Without Debugging
- Once Outlook 2010 loads, switch to the Home ribbon tab
- Click the New Items -> Task option to create a new task
Verify the Billable Task form region is visible
Figure 12
Custom Task Form Region
- Enter some data into the new task and save it
- Switch to the Tasks section using the buttons in the lower left and open the task you just created.
- Verify the information in the Billable Task region is loading correctly
- When you are done cleanup and remove the add-in
- Close Word 2010
- In the Solution Explorer, right click OutlookFormRegion and click Clean
|
|