Add a new data source to the Project
1. Create a Word document project named "My word operation pane. In the wizard, select "create new document ".
2. In Solution Explorer, select the my word actions pane project.
3. Click Add new item on the project menu ".
4. In the "Add new project" dialog box, select "Operation pane control", name it "actionscontrol", and click "add"
5. If the "Data Source" window is invisible, click "show data source" on the "data" menu ".
6. Click "Add new data source" to start the "Data Source Configuration Wizard ".
7. Select the data connection to the northwind sample SQL Server database, or use the "New Connection" button to add a new connection.
8. Expand the "table" node in the "database object" window.
9. Select the check boxes next to the "Suppliers" and "Products" tables.
10. Click Finish ".
Bind data to Windows Forms controls and add them to the Operations pane controls
1. Expand the "Suppliers" table in the "Data Source" window. Click the drop-down arrow on the "Company Name" node and select "ComboBox ".
2. Drag "companyName" from the "Data Source" window to the operation pane control.
A ComboBox control is created in the Action pane control. In addition, a bindingsource named suppliersbindingsource, a table adapter, and a dataset are added to the project in the component bar.
3. Select suppliersbindingnavigator in the "components" column and press Delete.
4. In the "Data Source" window, expand the sub-table "Products" of the "Suppliers" table ".
5. Click the drop-down arrow on the "productname" node and select "ListBox ".
6. Drag "productname" to the action pane control.
7. Right-click the button, click "properties" on the shortcut menu, and change the following properties:
Name: insert, text: insert
Use Data to load controls
1. Load event processing in the actionscontrol classProgram, Add the followingCode.
Private VoidActionscontrol_load (ObjectSender, eventargs E)
{
This. Supplierstableadapter. Fill (This. Northwinddataset. Suppliers );
This. Productstableadapter. Fill (This. Northwinddataset. products );
}
2. in C #, the event handler must be appended to the load event. You can put the code behind the call of initializecomponent in the actionscontrol constructor.
This. Load + =NewEventhandler (actionscontrol_load );
Set the data binding property of the control
Select the companynamecombobox control.
In the Properties window, click the button on the right of the datasource attribute and select suppliersbindingsource ".
Click the button on the right of the "displaymember" attribute and select "companyName ".
Expand the "databindings" attribute, click the button on the right of the "text" attribute, and select "NONE ".
Select the productnamelistbox control.
In the Properties window, click the button on the right of the datasource attribute and select productsbindingsource ".
Click the button on the right of the "displaymember" attribute and select "productname ".
Expand the "databindings" attribute, click the button on the right of the "selectedvalue" attribute, and select "NONE ".
Add method to insert data into a table
The next task is to read data from the bound control and fill in the tables in the Word document. First, create a process to set the table title format, and then add the adddata method to create a Word Table and set its format.
Set the table title format
Create a method in the actionscontrol class to set the table title format.
Static VoidSetheadings (Microsoft. Office. InterOP. Word. Cell tblcell,StringText)
{
Tblcell. range. Text = text;
Tblcell. range. Font. Bold = 1;
Tblcell. range. paragraphformat. Alignment =
Microsoft. Office. InterOP. Word. wdparagraphalignment. wdalignparagraphcenter;
}
Create a table
Write a method in the actionscontrol class. This method creates a table without a table and adds data in the Operation pane to the table.
Private Void Adddata (system. Data. datarow row, String CompanyName)
{
Object Missing = system. type. missing;
// Create a table if it doesn' t already exist.
If (Globals. thisdocument. Tables. Count = 0)
{
Try
{
// Create a table.
Microsoft. Office. InterOP. Word. Table TBL = globals. thisdocument. Tables. Add
(Globals. thisdocument. application. selection. Range, 1, 4, Ref Missing, Ref Missing );
// Insert headings.
Setheadings (TBL. Cell (1, 1 ), "Company Name" );
Setheadings (TBL. Cell (1, 2 ), "Product name" );
Setheadings (TBL. Cell (1, 3 ), "Quantity" );
Setheadings (TBL. Cell (1, 4 ),"Unit price" );
}
Catch (Exception ex)
{
MessageBox. Show ( "Problem creating products table :" + Ex. Message,
"Actions pane" , Messageboxbuttons. OK, messageboxicon. Error );
}
}
// Add data from data row to the table.
Microsoft. Office. InterOP. Word. Selection selection = globals. thisdocument. application. selection;
If (Selection. Tables. Count> 0)
{
Microsoft. Office. InterOP. Word. Row newrow = globals. thisdocument. Tables [1]. Rows. Add ( Ref Missing );
Newrow. range. Font. Bold = 0;
Newrow. range. paragraphformat. Alignment =
Microsoft. Office. InterOP. Word. wdparagraphalignment. wdalignparagraphleft;
Newrow. cells [4]. range. paragraphformat. Alignment =
Microsoft. Office. InterOP. Word. wdparagraphalignment. wdalignparagraphright;
Newrow. cells [1]. range. Text = companyName;
Newrow. cells [2]. range. Text = row [ "Productname" ]. Tostring ();
Newrow. cells [3]. range. Text = row [ "Quantityperunit" ]. Tostring ();
Newrow. cells [4]. range. Text = math. Round (convert. todouble (row [ "Unitprice" ]). Tostring ( "#,## 0.00" );
}
Else
{
MessageBox. Show ( "Cursor must be within a table ." ,
"Actions pane" , Messageboxbuttons. OK, messageboxicon. Error );
}
}
Insert text into the Word Table
1. Add the following code to the click event handler of the insert button.
Private Void Insert_click ( Object Sender, system. eventargs E)
{
System. Data. datatable TBL = northwinddataset. Products;
System. Data. datarow [] rows;
// Check if a product is selected.
If ( This . Productnamelistbox. selectedindex> = 0)
{
System. Data. datarowview productrow = (system. Data. datarowview) This . Productnamelistbox. selecteditem;
String Product = productrow. Row [ "Productname" ]. Tostring ();
String Company = This . Companynamecombobox. text;
// Return the data row from the selected product.
Rows = TBL. Select ( "[Productname] = '" + Product. Replace ( "'" , "''" ) + "'" );
This . Adddata (rows [0], company );
}
Else
{
MessageBox. Show ( "Please select a product ." , "Actions pane" , Messageboxbuttons. OK );
}
}
2. in C #, you must create an event handler for the button's click event. You can place this code in the load event handler of the actionscontrol class.
This. Insert. Click + =NewEventhandler (insert_click );
Show action pane
1. In Solution Explorer, right-click thisdocument. VB or thisdocument. CS, and then click View code on the shortcut menu ".
2. Create a new control instance at the top of the thisdocument class to make it look similar to the following example.
PrivateActionscontrol actions =NewActionscontrol ();
3. Add code to the startup event handler of thisdocument to make it look similar to the following example.
This. Actionspane. Controls. Add (actions );
Test the application
Now you can test the document to verify whether the action Pane appears when the document opens. Test the master/slave relationship between controls on the Operation pane, and make sure that the data is filled in the word table when you click the insert button.
Press F5 to run the project.
The confirm operation pane is visible.
Select a company from the combo box and confirm that the items in the "Products" list box change accordingly.
Select a product, click "insert" in the Action pane, and confirm that the product details are added to the word table.
Insert other products from different companies.
download source code:/files/Timy/mybasicactionspane.rar