425-882-1032 info@3sharp.com

During the question-and-answer session of my InfoPath presentation last week at the Office System Developer Conference, someone asked if it was possible to suppress the custom constraint alert that appears when an event (for example, the OnBeforeChange event) fails. Unfortunately, when you instruct such an event to fail by setting the event object’s ReturnStatus property to false, the alert will always appear. However, it is possible to change the text of the alert using the event object’s ReturnMessage property. The following code example, an extension of my presentation demo, shows how this property can be used in an OnBeforeChange event to prevent users from entering negative numbers in the form view’s Price field:

public void Price_OnBeforeChange(DataDOMEvent e)
{
    // We are only interested in Insert operations.
    if (e.Operation == "Insert")
    {
        // Save the inserted text to a variable.
        double PriceValue = double.Parse(e.Source.text);
        // If the value is less than 0, fail the event.
        if (PriceValue < 0)
        {
            e.ReturnMessage = "Negative numbers are not allowed.";
            e.ReturnStatus = false;
        }
    }
}