This repository has been archived by the owner on Jun 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.vb
67 lines (54 loc) · 2.09 KB
/
Form1.vb
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
Imports Microsoft.VisualBasic
Imports System
Imports System.Data
Imports System.Windows.Forms
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.API.Native
Namespace RichEditCalculatedField
Partial Public Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
richEditControl1.Options.MailMerge.DataSource = ProductsTable.CreateData()
RestoreTemplate()
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
Dim mailMergeOptions As MailMergeOptions = richEditControl1.Document.CreateMailMergeOptions()
mailMergeOptions.MergeMode = MergeMode.JoinTables
Dim server As New RichEditDocumentServer()
AddHandler server.CalculateDocumentVariable, AddressOf server_CalculateDocumentVariable
richEditControl1.Document.MailMerge(mailMergeOptions, server.Document)
richEditControl1.LoadDocument("HeaderTemplate.rtf")
richEditControl1.Document.AppendDocumentContent(server.Document.Range)
End Sub
Private Sub server_CalculateDocumentVariable(ByVal sender As Object, ByVal e As CalculateDocumentVariableEventArgs)
If e.VariableName = "Prod" Then
Dim productId As Integer = -1
If Int32.TryParse(e.Arguments(0).Value, productId) Then
Dim row As DataRow = (CType(richEditControl1.Options.MailMerge.DataSource, DataTable)).Rows.Find(productId)
Dim unitsInStock As Integer = Convert.ToInt32(row(e.Arguments(1).Value))
Dim unitPrice As Decimal = Convert.ToDecimal(row(e.Arguments(2).Value))
e.Value = unitsInStock * unitPrice
e.Handled = True
End If
End If
End Sub
#Region "Helper Methods"
Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button2.Click
RestoreTemplate()
End Sub
Private Sub RestoreTemplate()
richEditControl1.LoadDocument("DetailTemplate.rtf")
ShowFieldCodes()
End Sub
Private Sub ShowFieldCodes()
Dim doc As Document = richEditControl1.Document
doc.BeginUpdate()
For Each f As Field In doc.Fields
f.ShowCodes = True
Next f
doc.EndUpdate()
End Sub
#End Region ' Helper Methods
End Class
End Namespace