-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
95 lines (73 loc) · 3.7 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System.IO;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.Commands;
using DevExpress.XtraRichEdit.Services;
namespace RichEditMailMergeTemplateCreator {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
richEditControl1.Options.Fields.HighlightColor = Color.Yellow;
richEditControl1.Options.Fields.HighlightMode = FieldsHighlightMode.Always;
richEditControl1.Options.DocumentSaveOptions.CurrentFileName = Path.Combine(Application.StartupPath, "Template.rtf");
PrepareDefaultMergeTable();
}
private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
using (NewFieldsForm newFieldsForm = new NewFieldsForm()) {
PrepareFieldNamesByMergeTable(newFieldsForm);
if (newFieldsForm.ShowDialog() == DialogResult.OK) {
PrepareMergeTableByFieldNames(newFieldsForm);
}
}
ribbonControl1.SelectedPage = ribbonControl1.Pages["Mailings"];
}
private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
richEditControl1.Document.Fields.Create(richEditControl1.Document.CaretPosition, " DOCVARIABLE dvField ");
}
private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
richEditControl1.Document.Fields.Create(richEditControl1.Document.CaretPosition, " INCLUDEPICTURE \"image.jpg\" ");
}
private void barButtonItem4_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
CreateFieldCommand createFieldCommand = new CreateFieldCommand(richEditControl1);
createFieldCommand.Execute();
}
private void richEditControl1_CalculateDocumentVariable(object sender, DevExpress.XtraRichEdit.CalculateDocumentVariableEventArgs e) {
e.Value = e.VariableName + "'s value";
e.Handled = true;
}
private void PrepareDefaultMergeTable() {
string[] fieldNames = new string[] { "CategoryID", "CategoryName", "Description" };
DataTable dataTable = new DataTable("MergeTable");
foreach (string fieldName in fieldNames) {
dataTable.Columns.Add(fieldName);
}
CreateDefaultData(dataTable);
richEditControl1.Options.MailMerge.DataSource = dataTable;
}
private void PrepareMergeTableByFieldNames(NewFieldsForm newFieldsForm) {
DataTable dataTable = new DataTable("MergeTable");
foreach (string fieldName in newFieldsForm.FieldNames) {
dataTable.Columns.Add(fieldName);
}
CreateDefaultData(dataTable);
richEditControl1.Options.MailMerge.DataSource = dataTable;
}
private void PrepareFieldNamesByMergeTable(NewFieldsForm newFieldsForm) {
DataTable dataTable = (DataTable)richEditControl1.Options.MailMerge.DataSource;
string[] fieldNames = new string[dataTable.Columns.Count];
for (int i = 0; i < fieldNames.Length; i++) {
fieldNames[i] = dataTable.Columns[i].ColumnName;
}
newFieldsForm.FieldNames = fieldNames;
}
private void CreateDefaultData(DataTable dataTable) {
DataRow dataRow = dataTable.NewRow();
foreach (DataColumn dataColumn in dataTable.Columns) {
dataRow[dataColumn] = dataColumn.ColumnName + "'s value";
}
dataTable.Rows.Add(dataRow);
}
}
}