Back to Devexpress

DocVariableValue Class

officefileapi-devexpress-dot-xtrarichedit-0024d40d.md

latest5.2 KB
Original Source

DocVariableValue Class

A class which allows you to specify the current value of a DOCVARIABLE field in the Document.CalculateDocumentVariable event handler.

Namespace : DevExpress.XtraRichEdit

Assembly : DevExpress.RichEdit.v25.2.Core.dll

NuGet Package : DevExpress.RichEdit.Core

Declaration

csharp
public class DocVariableValue
vb
Public Class DocVariableValue

Remarks

When the Document.CalculateDocumentVariable event occurs, the current value of the DOCVARIABLE field is lost by default. You should explicitly specify the field value using the CalculateDocumentVariableEventArgs.Value property within the Document.CalculateDocumentVariable event handler.

To retain the current value of the field, set the DocVariableValue’s DocVariableValue.Current value to the CalculateDocumentVariableEventArgs.Value property.

Example

This code snippet demonstrates how to handle the RichEditControl.CalculateDocumentVariable event to insert dynamic content into the document. In this example, it is used to get weather conditions. A variable name specified in the DOCVARIABLE field indicates a choice between location and weather, while the location itself is specified by the field argument.

View Example

csharp
void eventHandler_CalculateDocumentVariable(object sender, CalculateDocumentVariableEventArgs e) {
    if (e.Arguments.Count > 0) {
        string location = e.Arguments[0].Value.ToString();
        if ((location.Trim() == String.Empty) || (location.Contains("<"))) {
            e.Value = " ";
            e.Handled = true;
            return;
        }
        switch (e.VariableName) {
            case "Weather":
                Conditions conditions = new Conditions();
                conditions = Weather.GetCurrentConditions(location);
                e.Value = String.Format("Weather for {0}: \nConditions: {1}\nTemperature (C) :{2}\nHumidity: {3}\nWind: {4}\n",
                    location, conditions.Condition, conditions.TempC, conditions.Humidity, conditions.Wind);
                break;
            case "LOCATION":
                if (location == "DO NOT CHANGE!") e.Value = DocVariableValue.Current;
                break;
            default:
                e.Value = "LOCKED FIELD UPDATED";
                break;
        }
    }
    else {
        e.Value = "LOCKED FIELD UPDATED";
    }
    e.Handled = true;
}
vb
Private Sub eventHandler_CalculateDocumentVariable(ByVal sender As Object, ByVal e As CalculateDocumentVariableEventArgs) Handles richEditControl1.CalculateDocumentVariable
    If e.Arguments.Count > 0 Then
        Dim location As String = e.Arguments(0).Value.ToString()
        If (location.Trim() = String.Empty) OrElse (location.Contains("<")) Then
            e.Value = " "
            e.Handled = True
            Return
        End If
        Select Case e.VariableName
            Case "Weather"
                Dim conditions As New Conditions()
                conditions = Weather.GetCurrentConditions(location)
                e.Value = String.Format("Weather for {0}: " & vbLf & "Conditions: {1}" & vbLf & "Temperature (C) :{2}" & vbLf & "Humidity: {3}" & vbLf & "Wind: {4}" & vbLf, location, conditions.Condition, conditions.TempC, conditions.Humidity, conditions.Wind)
            Case "LOCATION"
                If location = "DO NOT CHANGE!" Then
                    e.Value = DocVariableValue.Current
                End If
            Case Else
                e.Value = "LOCKED FIELD UPDATED"
        End Select
    Else
        e.Value = "LOCKED FIELD UPDATED"
    End If
    e.Handled = True
End Sub

Inheritance

Object DocVariableValue

See Also

DocVariableValue Members

DevExpress.XtraRichEdit Namespace